| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- package com.dk.mdm.infrastructure.util;
- import com.alibaba.fastjson.JSONObject;
- import com.dk.common.exception.BaseBusinessException;
- import com.dk.common.infrastructure.constant.Constant;
- import com.dk.common.model.vo.core.StaffEntity;
- import com.dk.common.response.ResponseCodeEnum;
- import com.dk.common.util.oauth.AESSecurityUtil;
- import com.dk.common.util.oauth.JwtUtil;
- import com.dk.mdm.infrastructure.convert.mst.StaffConvert;
- import com.dk.mdm.mapper.mst.StaffMapper;
- import com.dk.common.model.response.mst.StaffResponse;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.data.redis.core.StringRedisTemplate;
- import org.springframework.stereotype.Component;
- import javax.servlet.http.HttpServletRequest;
- import java.util.concurrent.TimeUnit;
- /**
- * @desc : AuthUtils
- * @author : 洪旭东
- * @date : 2022-06-07 16:13
- */
- @Component("MdmAuthUtils")
- @Slf4j
- public class AuthUtils {
- @Value("${aes-key}")
- private String AESKey;
- @Autowired
- private StaffMapper staffMapper;
- @Autowired
- private StaffConvert staffConvert;
- @Autowired
- private StringRedisTemplate stringRedisTemplate;
- @Autowired
- private HttpServletRequest httpServletRequest;
- /**
- * @desc : 查询当前用户
- * @author : 洪旭东
- * @date : 2022-06-07 16:19
- */
- public StaffEntity getStaff(){
- return getStaff(httpServletRequest.getHeader("Authorization"));
- }
- /**
- * @date_time 2021-12-23 09:25
- * @author H_x_d
- * @description 通过token获取用户信息,如果redis中失效,重新查询
- * @return com.dongke.auth.pojo.User
- */
- public StaffEntity getStaff(String token){
- if (token==null) {
- return null;
- }
- try {
- String[] tokens = token.split(" ");
- String decrypt = AESSecurityUtil.decrypt(AESKey, tokens[1]);
- String staffId = JwtUtil.getStaffId(decrypt);
- log.info("staffId=>{}",staffId);
- String appCode = JwtUtil.getAppCode(decrypt);
- log.info("appCode=>{}",appCode);
- String cpId = JwtUtil.getCpId(decrypt);
- log.info("cpId=>{}",cpId);
- String cpCode = JwtUtil.getCpCode(decrypt);
- log.info("cpCode=>{}",cpCode);
- 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);
- if (json==null){
- return saveStaff(staffId, cpCode, appCode);
- }else{
- return JSONObject.parseObject(json, StaffEntity.class);
- }
- } catch (Exception e) {
- log.error(e.toString(),e);
- throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(),"解析accessToken失败");
- }
- }
- /**
- * @desc : 清除redis用户信息
- * @author : 周兴
- * @date : 2023/1/6 15:46
- */
- public void cleanStaff(){
- String appCode = JwtUtil.getAppCode(httpServletRequest);
- cleanStaff(Constant.RedisConstant.REDIS_STAFF.getName() + appCode + Constant.StringConstant.SYSTEM_MAGIN_LINE.getName() + getStaff().getStaffId());
- }
- /**
- * @desc : 清除redis用户信息
- * @author : 周兴
- * @date : 2023/1/6 15:46
- */
- public void cleanStaff(Long userId){
- String appCode = JwtUtil.getAppCode(httpServletRequest);
- stringRedisTemplate.delete(Constant.RedisConstant.REDIS_STAFF.getName() + appCode + Constant.StringConstant.SYSTEM_MAGIN_LINE.getName() + userId);
- }
- /**
- * @desc : 清除redis用户信息
- * @author : 周兴
- * @date : 2023/1/6 15:46
- */
- public void cleanStaff(String key){
- stringRedisTemplate.delete(key);
- }
- /**
- * @desc : 清除登录用户信息
- * @author : 周兴
- * @date : 2023/1/5 13:08
- */
- public void cleanLoginStaff(){
- StaffEntity staffEntity = getStaff();
- cleanLoginStaff(Constant.RedisConstant.REDIS_LOGIN.getName() + Constant.StringConstant.SYSTEM_MAGIN_LINE.getName() + staffEntity.getStaffCode());
- }
- /**
- * @desc : 清除缓存
- * @author : 周兴
- * @date : 2023/1/5 13:09
- */
- public void cleanLoginStaff(String key){
- stringRedisTemplate.delete(key);
- }
- /**
- * @date_time 2021-12-23 09:22
- * @author H_x_d
- * @description 保存用户信息 + 权限ID 到Redis
- * @return User
- */
- public StaffEntity saveStaff(String staffId, String cpCode, String appCode){
- StaffResponse staff = staffMapper.selectById(staffId);
- StaffEntity staffEntity = staffConvert.convertToEntity(staff);
- if (staffEntity!=null) {
- staffEntity.setAppCode(appCode);
- staffEntity.setCpCode(cpCode);
- saveStaff(staffEntity);
- }
- return staffEntity;
- }
- /**
- * @desc : 保存员工
- * @author : 周兴
- * @date : 2023/1/6 15:18
- */
- public StaffEntity saveStaff(StaffEntity staff){
- if (staff!=null) {
- stringRedisTemplate.opsForValue().set(Constant.RedisConstant.REDIS_STAFF.getName() + staff.getCpId() + Constant.StringConstant.SYSTEM_MAGIN_LINE.getName()
- + staff.getAppCode() + Constant.StringConstant.SYSTEM_MAGIN_LINE.getName() + staff.getStaffCode(), JSONObject.toJSONString(staff), 1, TimeUnit.HOURS);
- }
- return staff;
- }
- }
|