在Spring Security中,将Details中的列表显示在页面上
使用Thymeleaf显示通过UsernamePasswordAuthenticationToken#setDetails设置的任意对象列表的属性,其中spring-security是一种方法。
源代码
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.9.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
@SpringBootApplication
public class App {
public static void main(String[] args) {
new SpringApplicationBuilder(App.class).web(WebApplicationType.SERVLET).run(args);
}
}
首先,不进行任何认证,只需使用setDetails方法填入数值即可。
import java.util.Arrays;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.stereotype.Component;
@Component
public class MyAuthenticationProvider implements AuthenticationProvider {
public Authentication authenticate(Authentication arg0) throws AuthenticationException {
UsernamePasswordAuthenticationToken t = new UsernamePasswordAuthenticationToken("kagami", "hoge");
MyDetail d = new MyDetail();
d.hogeValue = "hoge";
d.hogeList = Arrays.asList("aaa", "bbb", "ccc");
t.setDetails(d);
return t;
}
public boolean supports(Class<?> arg0) {
return true;
}
}
import java.util.List;
import lombok.Data;
@Data
public class MyDetail {
String hogeValue;
List<String> hogeList;
}
在默认的登录页面上,输入任意的ID密码后会显示以下的HTML内容。
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<body>
<div th:text="${#authentication.details.hogeValue}"></div>
<table>
<tr th:each="hoge : ${#authentication.details.hogeList}">
<td th:text="${hoge}"></td>
</tr>
</table>
</body>
</html>
你只需要看一下这个链接:https://github.com/thymeleaf/thymeleaf-extras-springsecurity,就能获取到Spring Security认证对象(此处是指MyAuthenticationProvider返回的UsernamePasswordAuthenticationToken对象)的#authentication。因此,你可以通过该对象的details属性中的hogeList属性来获取列表。然后,你可以使用Thymeleaf的foreach或其他方法来处理它。
请参见以下链接
- https://github.com/thymeleaf/thymeleaf-extras-springsecurity