Spring Thymeleaf User Details

1、继承User或实现Spring org.springframework.security.core.userdetails.UserDetails

2、UserService 实现org.springframework.security.core.userdetails.UserDetailsService

3、设置WebSecurityConfig

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
// ...
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.eraseCredentials(true)
.userDetailsService(userService())
;
}
@Bean
public UserService userService() {
return new UserService();
}
}

服务端完工。

在html中,使用principal

例如访问用户昵称字段:<span sec:authentication="principal.nick">Bob</span>

参考资料: