| | |
| | | */ |
| | | public class SecurityUtils { |
| | | |
| | | public static UserDetails getUserDetails() { |
| | | public static LoginUserInfo getUserDetails() { |
| | | final Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); |
| | | if (authentication == null) { |
| | | // throw new BusinessException(ResponseStatusEnums.TOKEN_INVALID.getCode(), ResponseStatusEnums.TOKEN_INVALID.getMessage()); |
| | | return null; |
| | | } |
| | | if (authentication.getPrincipal() instanceof UserDetails) { |
| | | UserDetails userDetails = (UserDetails) authentication.getPrincipal(); |
| | | UserDetailsService userDetailsService = SpringContextHolder.getBean(UserDetailsService.class); |
| | | return userDetailsService.loadUserByUsername(userDetails.getUsername()); |
| | | if (authentication.getPrincipal() instanceof LoginUserInfo) { |
| | | LoginUserInfo loginUserInfo = (LoginUserInfo) authentication.getPrincipal(); |
| | | return loginUserInfo; |
| | | } |
| | | return null; |
| | | // throw new BusinessException(ResponseStatusEnums.TOKEN_INVALID.getCode(), ResponseStatusEnums.TOKEN_INVALID.getMessage()); |
| | | } |
| | | |
| | | /** |
| | |
| | | public static String getUsername() { |
| | | final Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); |
| | | if (authentication == null) { |
| | | // throw new BusinessException(ResponseStatusEnums.TOKEN_INVALID.getCode(), ResponseStatusEnums.TOKEN_INVALID.getMessage()); |
| | | return "无登录"; |
| | | } |
| | | // UserDetails userDetails = (UserDetails) authentication.getPrincipal(); |
| | | // return userDetails.getUsername(); |
| | | return (String)authentication.getPrincipal(); |
| | | if (authentication.getPrincipal() instanceof LoginUserInfo) { |
| | | LoginUserInfo loginUserInfo = (LoginUserInfo) authentication.getPrincipal(); |
| | | return loginUserInfo.getUsername(); |
| | | } |
| | | return "无登录"; |
| | | } |
| | | |
| | | /** |
| | |
| | | public static Long getUserId() { |
| | | SecurityProperties properties = SpringContextHolder.getBean(SecurityProperties.class); |
| | | if (!properties.isCacheOnline()) { |
| | | Object obj = getUserDetails(); |
| | | LoginUserInfo obj = getUserDetails(); |
| | | if (obj == null) { |
| | | return null; |
| | | } |
| | | JSONObject json = new JSONObject(obj); |
| | | return json.getJSONObject("user").get("id", Long.class); |
| | | return obj.getUserId(); |
| | | } else { |
| | | HttpServletRequest httpServletRequest = RequestHolder.getHttpServletRequest(); |
| | | JwtTokenProvider tokenProvider = SpringContextHolder.getBean(JwtTokenProvider.class); |