0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

spring-securityでsetDetailsの中のリストを画面表示する

Posted at

spring-securityでUsernamePasswordAuthenticationToken#setDetailsでセットした任意オブジェクトのリストのプロパティを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);
  }
}

とりあえず、認証はなんもせずに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-password入力すると、以下のhtmlを表示する。

/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>

https://github.com/thymeleaf/thymeleaf-extras-springsecurity にあるように、#authenticationでspring-securityの認証オブジェクト(ここではMyAuthenticationProviderが返すUsernamePasswordAuthenticationTokenの事)が取れる。なので、そのプロパティdetailsのプロパティhogeListでリストが取れる。あとはThymeleafのforeachなりなんなりで処理すれば良い。

参考URL

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?