| | |
| | | import com.sandu.common.security.jwt.JwtTokenProvider; |
| | | import com.sandu.common.util.RequestHolder; |
| | | import com.sandu.common.util.SpringContextHolder; |
| | | import com.sandu.ximon.admin.dto.AdminJwtUser; |
| | | import com.sandu.ximon.dao.domain.Admin; |
| | | import com.sandu.ximon.dao.enums.AdministratorEnums; |
| | | import org.springframework.security.core.Authentication; |
| | | import org.springframework.security.core.context.SecurityContextHolder; |
| | | import org.springframework.security.core.userdetails.UserDetails; |
| | |
| | | */ |
| | | public class SecurityUtils { |
| | | |
| | | |
| | | public static UserDetails getUserDetails() { |
| | | /** |
| | | * 获取用户信息 |
| | | */ |
| | | private static UserDetails getUserDetails() { |
| | | final Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); |
| | | if (authentication == null) { |
| | | throw new BusinessException(ResponseStatusEnums.TOKEN_INVALID.getCode(), ResponseStatusEnums.TOKEN_INVALID.getMessage()); |
| | | } |
| | | if (authentication.getPrincipal() instanceof UserDetails) { |
| | | Integer administratorIdentity = getAdministratorIdentity(); |
| | | if (AdministratorEnums.ADMIN.getCode().equals(administratorIdentity) || AdministratorEnums.INSPECT_CUSTOMER.getCode().equals(administratorIdentity)) { |
| | | UserDetails userDetails = (UserDetails) authentication.getPrincipal(); |
| | | UserDetailsService userDetailsService = SpringContextHolder.getBean(UserDetailsService.class); |
| | | UserDetailsService userDetailsService = SpringContextHolder.getBean("adminDetailsService"); |
| | | return userDetailsService.loadUserByUsername(userDetails.getUsername()); |
| | | } else if (AdministratorEnums.CUSTOMER.getCode().equals(administratorIdentity)) { |
| | | UserDetails userDetails = (UserDetails) authentication.getPrincipal(); |
| | | UserDetailsService userDetailsService = SpringContextHolder.getBean("customerDetailsService"); |
| | | return userDetailsService.loadUserByUsername(userDetails.getUsername()); |
| | | } |
| | | |
| | | return null; |
| | | // throw new BusinessException(ResponseStatusEnums.TOKEN_INVALID.getCode(), ResponseStatusEnums.TOKEN_INVALID.getMessage()); |
| | | } |
| | | |
| | | /** |
| | |
| | | } |
| | | |
| | | /** |
| | | * 获取管理员身份, 类型可见 {{@link AdministratorEnums}} |
| | | * |
| | | * @return |
| | | */ |
| | | public static Integer getAdministratorIdentity() { |
| | | final Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); |
| | | if (authentication == null) { |
| | | throw new BusinessException(ResponseStatusEnums.TOKEN_INVALID.getCode(), ResponseStatusEnums.TOKEN_INVALID.getMessage()); |
| | | } |
| | | return (Integer) authentication.getCredentials(); |
| | | } |
| | | |
| | | /** |
| | | * 获取系统用户id |
| | | * |
| | | * @return 系统用户id |