Spring Security后台获取用户信息

SecurityContextImpl securityContextImpl = (SecurityContextImpl) request.getSession().getAttribute("SPRING_SECURITY_CONTEXT");// 登录名
if (securityContextImpl != null) {
    System.out.println("Username:"+ securityContextImpl.getAuthentication().getName());
}
//
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
System.out.println(authentication);
if(authentication!=null && authentication.isAuthenticated()) {
    System.out.println(authentication.getName());
    System.out.println(authentication.getPrincipal());
}
//
UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext()
        .getAuthentication()
        .getPrincipal();

System.out.println(userDetails.getUsername());

Spring Security后台获取用户信息_第1张图片

  1. SecurityContextImpl securityContextImpl = (SecurityContextImpl) request

  2. .getSession().getAttribute("SPRING_SECURITY_CONTEXT");

  3. // 登录名

  4. System.out.println("Username:"

  5. + securityContextImpl.getAuthentication().getName());

  6. // 登录密码,未加密的

  7. System.out.println("Credentials:"

  8. + securityContextImpl.getAuthentication().getCredentials());

  9. WebAuthenticationDetails details = (WebAuthenticationDetails) securityContextImpl

  10. .getAuthentication().getDetails();

  11. // 获得访问地址

  12. System.out.println("RemoteAddress" + details.getRemoteAddress());

  13. // 获得sessionid

  14. System.out.println("SessionId" + details.getSessionId());

  15. // 获得当前用户所拥有的权限

  16. List authorities = (List) securityContextImpl

  17. .getAuthentication().getAuthorities();

  18. for (GrantedAuthority grantedAuthority : authorities) {

  19. System.out.println("Authority" + grantedAuthority.getAuthority());

  20. }

  21. spring技术学习,更多知识请访问https://www.itkc8.com

你可能感兴趣的:(Spring)