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

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

3、设置WebSecurityConfig

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>

参考资料: