AuthUtils.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package com.dk.mdm.infrastructure.util;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.dk.common.exception.BaseBusinessException;
  4. import com.dk.common.infrastructure.constant.Constant;
  5. import com.dk.common.model.vo.core.StaffEntity;
  6. import com.dk.common.response.ResponseCodeEnum;
  7. import com.dk.common.util.oauth.AESSecurityUtil;
  8. import com.dk.common.util.oauth.JwtUtil;
  9. import com.dk.mdm.infrastructure.convert.mst.StaffConvert;
  10. import com.dk.mdm.mapper.mst.StaffMapper;
  11. import com.dk.mdm.model.pojo.mst.Staff;
  12. import com.dk.mdm.model.response.mst.StaffResponse;
  13. import com.dk.mdm.service.mst.StaffService;
  14. import lombok.extern.slf4j.Slf4j;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.beans.factory.annotation.Value;
  17. import org.springframework.data.redis.core.StringRedisTemplate;
  18. import org.springframework.stereotype.Component;
  19. import javax.servlet.http.HttpServletRequest;
  20. import java.util.concurrent.TimeUnit;
  21. /**
  22. * @desc : AuthUtils
  23. * @author : 洪旭东
  24. * @date : 2022-06-07 16:13
  25. */
  26. @Component("MdmAuthUtils")
  27. @Slf4j
  28. public class AuthUtils {
  29. @Value("${aes-key}")
  30. private String AESKey;
  31. @Autowired
  32. private StaffMapper staffMapper;
  33. @Autowired
  34. private StaffConvert staffConvert;
  35. @Autowired
  36. private StringRedisTemplate stringRedisTemplate;
  37. @Autowired
  38. private HttpServletRequest httpServletRequest;
  39. /**
  40. * @desc : 查询当前用户
  41. * @author : 洪旭东
  42. * @date : 2022-06-07 16:19
  43. */
  44. public StaffEntity getStaff(){
  45. return getStaff(httpServletRequest.getHeader("Authorization"));
  46. }
  47. /**
  48. * @date_time 2021-12-23 09:25
  49. * @author H_x_d
  50. * @description 通过token获取用户信息,如果redis中失效,重新查询
  51. * @return com.dongke.auth.pojo.User
  52. */
  53. public StaffEntity getStaff(String token){
  54. if (token==null) {
  55. return null;
  56. }
  57. try {
  58. String[] tokens = token.split(" ");
  59. String decrypt = AESSecurityUtil.decrypt(AESKey, tokens[1]);
  60. String staffId = JwtUtil.getStaffId(decrypt);
  61. log.info("staffId=>{}",staffId);
  62. String appCode = JwtUtil.getAppCode(decrypt);
  63. log.info("appCode=>{}",appCode);
  64. String cpId = JwtUtil.getCpId(decrypt);
  65. log.info("cpId=>{}",cpId);
  66. String cpCode = JwtUtil.getCpCode(decrypt);
  67. log.info("cpCode=>{}",cpCode);
  68. String json = stringRedisTemplate.opsForValue().get(Constant.RedisConstant.REDIS_STAFF.getName() + cpId + Constant.StringConstant.SYSTEM_MAGIN_LINE.getName() + appCode + Constant.StringConstant.SYSTEM_MAGIN_LINE.getName() + staffId);
  69. if (json==null){
  70. return saveStaff(staffId, cpCode, appCode);
  71. }else{
  72. return JSONObject.parseObject(json, StaffEntity.class);
  73. }
  74. } catch (Exception e) {
  75. log.error(e.toString(),e);
  76. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(),"解析accessToken失败");
  77. }
  78. }
  79. /**
  80. * @desc : 清除redis用户信息
  81. * @author : 周兴
  82. * @date : 2023/1/6 15:46
  83. */
  84. public void cleanStaff(){
  85. String appCode = JwtUtil.getAppCode(httpServletRequest);
  86. cleanStaff(Constant.RedisConstant.REDIS_STAFF.getName() + appCode + Constant.StringConstant.SYSTEM_MAGIN_LINE.getName() + getStaff().getStaffId());
  87. }
  88. /**
  89. * @desc : 清除redis用户信息
  90. * @author : 周兴
  91. * @date : 2023/1/6 15:46
  92. */
  93. public void cleanStaff(Long userId){
  94. String appCode = JwtUtil.getAppCode(httpServletRequest);
  95. stringRedisTemplate.delete(Constant.RedisConstant.REDIS_STAFF.getName() + appCode + Constant.StringConstant.SYSTEM_MAGIN_LINE.getName() + userId);
  96. }
  97. /**
  98. * @desc : 清除redis用户信息
  99. * @author : 周兴
  100. * @date : 2023/1/6 15:46
  101. */
  102. public void cleanStaff(String key){
  103. stringRedisTemplate.delete(key);
  104. }
  105. /**
  106. * @desc : 清除登录用户信息
  107. * @author : 周兴
  108. * @date : 2023/1/5 13:08
  109. */
  110. public void cleanLoginStaff(){
  111. StaffEntity staffEntity = getStaff();
  112. cleanLoginStaff(Constant.RedisConstant.REDIS_LOGIN.getName() + Constant.StringConstant.SYSTEM_MAGIN_LINE.getName() + staffEntity.getStaffCode());
  113. }
  114. /**
  115. * @desc : 清除缓存
  116. * @author : 周兴
  117. * @date : 2023/1/5 13:09
  118. */
  119. public void cleanLoginStaff(String key){
  120. stringRedisTemplate.delete(key);
  121. }
  122. /**
  123. * @date_time 2021-12-23 09:22
  124. * @author H_x_d
  125. * @description 保存用户信息 + 权限ID 到Redis
  126. * @return User
  127. */
  128. public StaffEntity saveStaff(String staffId, String cpCode, String appCode){
  129. StaffResponse staff = staffMapper.selectById(staffId);
  130. StaffEntity staffEntity = staffConvert.convertToEntity(staff);
  131. if (staffEntity!=null) {
  132. staffEntity.setAppCode(appCode);
  133. staffEntity.setCpCode(cpCode);
  134. saveStaff(staffEntity);
  135. }
  136. return staffEntity;
  137. }
  138. /**
  139. * @desc : 保存员工
  140. * @author : 周兴
  141. * @date : 2023/1/6 15:18
  142. */
  143. public StaffEntity saveStaff(StaffEntity staff){
  144. if (staff!=null) {
  145. stringRedisTemplate.opsForValue().set(Constant.RedisConstant.REDIS_STAFF.getName() + staff.getCpId() + Constant.StringConstant.SYSTEM_MAGIN_LINE.getName()
  146. + staff.getAppCode() + Constant.StringConstant.SYSTEM_MAGIN_LINE.getName() + staff.getStaffCode(), JSONObject.toJSONString(staff), 1, TimeUnit.HOURS);
  147. }
  148. return staff;
  149. }
  150. }