Display the list in setDetails on the screen with spring-security

Display the property of the list of arbitrary objects set by UsernamePasswordAuthenticationToken # setDetails in spring-security with Thymeleaf.

Source code

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.

Reference URL

Recommended Posts

Display the list in setDetails on the screen with spring-security
Display an image on the base64 screen
Fill the screen with buttons in TableLayout
How to display products by category on the same list screen
[Android] List all setting items on the setting screen
[Java] Get the file path in the folder with List
Switch the display screen when hovering the tab with jQuery
[Ruby on Rails] Quickly display the page title in the browser
Implemented a strong API for "I want to display ~~ on the screen" with simple CQRS
Draw screen with Thymeleaf in SpringBoot
[Android] Try to display the effect on the touched part of the screen, which is common in games.
Programming with ruby (on the way)
Place in the middle with css
Display user-defined characters on the I2C 1602 LCD with Raspberry Pi 3 & Java
[Java] Display the bit string stored in the byte type variable on the console
Put Zabbix in Ubuntu with Docker and monitor Docker on the same host
How to display the text entered in text_area in Rails with line breaks
Display text on top of the image
Scraping with puppeteer in Nuxt on Docker.
How the website is displayed on the screen
Pre-processing to display on the browser (compiler)
Display the background image in the production environment
Let's make a calculator application with Java ~ Create a display area in the window
When the login screen times out with spring-security, the CSRF token expires and login fails.
Perform SSL encryption settings in accordance with the "TLS encryption setting guidelines" on CentOS8
Login fails because the redirect URL of the self-login screen is incorrect in spring-security
[No.004] Corrected the order list screen of the orderer
Using templates on the classpath with Apache Velocity
Use JDBC Manager with the settings in jdbc.dicon.
[Android] Get the tapped position (coordinates) on the screen
Display the System Monitor applet on the Ubuntu 20.04 taskbar
Display around fonts in Japanese with Swift: macOS
Publish the app made with ruby on rails
Display Mat type image on GUI with Swing
Matters worked on in the November 2017 study session
Come out with a suffix on the method 2
Matches annotations on the interface with Spring AOP
Display "Hello World" in the browser using Java
Implement the Like feature in Ajax with Rails.
Display "Hello World" in the browser using Java
Determine the current page with Ruby on Rails
How to save a file with the specified extension under the directory specified in Java to the list
I want to place RadioButtons in the same RadioGroup at any position on the screen.