| | |
| | | /** |
| | | * Copyright (C) 2018-2020 |
| | | * All rights reserved, Designed By www.yixiang.co |
| | | * 注意: |
| | | * 本软件为www.yixiang.co开发研制 |
| | | */ |
| | | package com.sandu.ximon.admin.security; |
| | | |
| | | import cn.hutool.json.JSONObject; |
| | | import com.sandu.common.enums.ResponseStatusEnums; |
| | | import com.sandu.common.execption.BusinessException; |
| | | import com.sandu.common.redis.online.OnlineUser; |
| | | import com.sandu.common.redis.online.OnlineUserService; |
| | | import com.sandu.common.security.config.SecurityProperties; |
| | | 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.common.security.LoginUserInfo; |
| | | 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; |
| | | import org.springframework.security.core.userdetails.UserDetailsService; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | |
| | | /** |
| | | * 获取当前登录的用户 |
| | |
| | | */ |
| | | 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()); |
| | | } |
| | | if (authentication.getPrincipal() instanceof UserDetails) { |
| | | UserDetails userDetails = (UserDetails) authentication.getPrincipal(); |
| | | UserDetailsService userDetailsService = SpringContextHolder.getBean(UserDetailsService.class); |
| | | return userDetailsService.loadUserByUsername(userDetails.getUsername()); |
| | | } |
| | | return null; |
| | | // throw new BusinessException(ResponseStatusEnums.TOKEN_INVALID.getCode(), ResponseStatusEnums.TOKEN_INVALID.getMessage()); |
| | | } |
| | | |
| | | /** |
| | | * 获取系统用户名称 登录名 |
| | | * |
| | | * @return 系统用户名称 |
| | | */ |
| | | public static String getUsername() { |
| | | final Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); |
| | | if (authentication == null) { |
| | | throw new BusinessException(ResponseStatusEnums.TOKEN_INVALID.getCode(), ResponseStatusEnums.TOKEN_INVALID.getMessage()); |
| | | } |
| | | UserDetails userDetails = (UserDetails) authentication.getPrincipal(); |
| | | return userDetails.getUsername(); |
| | | } |
| | | |
| | | /** |
| | | * 获取系统用户id |
| | | * |
| | | * @return 系统用户id |
| | | */ |
| | | public static Long getUserId() { |
| | | SecurityProperties properties = SpringContextHolder.getBean(SecurityProperties.class); |
| | | if (!properties.isCacheOnline()) { |
| | | UserDetails obj = getUserDetails(); |
| | | if (obj == null) { |
| | | return null; |
| | | } |
| | | if (!obj.isEnabled()) { |
| | | if (authentication.getPrincipal() instanceof LoginUserInfo) { |
| | | LoginUserInfo loginUserInfo = (LoginUserInfo) authentication.getPrincipal(); |
| | | if (!loginUserInfo.isEnabled()) { |
| | | throw new BusinessException(ResponseStatusEnums.DISABLE_ACCOUNT.getCode(), ResponseStatusEnums.DISABLE_ACCOUNT.getMessage()); |
| | | } |
| | | JSONObject json = new JSONObject(obj); |
| | | return json.getJSONObject("user").get("id", Long.class); |
| | | } else { |
| | | HttpServletRequest httpServletRequest = RequestHolder.getHttpServletRequest(); |
| | | JwtTokenProvider tokenProvider = SpringContextHolder.getBean(JwtTokenProvider.class); |
| | | OnlineUserService onlineUserService = SpringContextHolder.getBean(OnlineUserService.class); |
| | | String username = tokenProvider.getSubjectForce(tokenProvider.getToken(httpServletRequest)); |
| | | OnlineUser one = onlineUserService.getOne(properties.getOnlineKey() + username); |
| | | if (one != null) { |
| | | return one.getUserId(); |
| | | } else { |
| | | return null; |
| | | } |
| | | return loginUserInfo; |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * 获取当前登录用户登录名 |
| | | * |
| | | * @return 当前登录用户登录名 |
| | | */ |
| | | public static String getUsername() { |
| | | LoginUserInfo loginUserInfo = getUserDetails(); |
| | | if (loginUserInfo != null) { |
| | | return loginUserInfo.getUsername(); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * 获取管理员身份, 类型可见 {{@link AdministratorEnums}} |
| | | * |
| | | * @return |
| | | */ |
| | | public static Integer getAdministratorIdentity() { |
| | | LoginUserInfo loginUserInfo = getUserDetails(); |
| | | if (loginUserInfo == null) { |
| | | throw new BusinessException(ResponseStatusEnums.TOKEN_INVALID.getCode(), ResponseStatusEnums.TOKEN_INVALID.getMessage()); |
| | | } |
| | | return loginUserInfo.getAdministratorType(); |
| | | } |
| | | |
| | | /** |
| | | * 获取当前登录账号的id |
| | | * |
| | | * @return 当前登录账号的id |
| | | */ |
| | | public static Long getUserId() { |
| | | LoginUserInfo loginUserInfo = getUserDetails(); |
| | | if (loginUserInfo != null) { |
| | | return loginUserInfo.getUserId(); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * 获取客户账号的id 如果当前身份是运营人员,返回null |
| | | * |
| | | * @return |
| | | */ |
| | | public static Long getClientId() { |
| | | LoginUserInfo loginUserInfo = getUserDetails(); |
| | | if (loginUserInfo == null) { |
| | | return null; |
| | | } |
| | | if (!AdministratorEnums.ADMIN.getCode().equals(loginUserInfo.getAdministratorType())) { |
| | | return loginUserInfo.getUserId(); |
| | | } |
| | | return null; |
| | | } |
| | | } |