AuthUtils.java 5.5 KB

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