我正在编写一个Spring Web应用程序。关于身份验证,我使用Spring Security,我需要检索用户的信息细节,如何避免?
答案 0 :(得分:0)
路径为 SecurityContextHolder.getContext()。getAuthentication()。getPrincipal()
以您为例:
public UserDetails getCurrentUserDetails() {
org.springframework.security.core.context.SecurityContext securityContext = SecurityContextHolder
.getContext();
Authentication authentication = securityContext.getAuthentication();
UserDetails user = null;
if (authentication != null) {
if (authentication.getPrincipal() instanceof UserDetails)
{
user = ((UserDetails) authentication.getPrincipal());
}
}
return user;
}