Display the property of the list of arbitrary objects set by UsernamePasswordAuthenticationToken # setDetails
in spring-security with Thymeleaf.
pom.xml
<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);
}
}
For the time being, do not authenticate and just fill in the values with `` `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;
}
If you enter an appropriate id-password on the default login screen, the following html will be displayed.
/springsecsample/src/main/resouces/templates/index.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>
As you can see at https://github.com/thymeleaf/thymeleaf-extras-springsecurity, `#authentication``` is the spring-security authentication object (here
MyAuthenticationProviderreturns
UsernamePasswordAuthenticationToken) can be taken. So you can get a list with the property `` `hogeList
of that property `` `details```. After that, you can process it with Thymeleaf foreach.
Recommended Posts