From b9537077efdc8fab29d016b0ac2b9166ff15be55 Mon Sep 17 00:00:00 2001 From: shijie <648688341@qq.com> Date: Fri, 9 Oct 2020 14:19:48 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=B3=BB=E7=BB=9F=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E9=83=A8=E5=88=86=E6=95=B0=E6=8D=AE=E5=BA=93=E6=93=8D?= =?UTF-8?q?=E4=BD=9C=E6=94=B9=E4=B8=BAredis=E8=AF=BB=E5=8F=96=E6=95=B0?= =?UTF-8?q?=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../financial/hrauth/aspect/Operation.java | 9 +++++++-- .../financial/hrauth/aspect/SysLogAspect.java | 10 ++++++++-- .../service/impl/UserLoginServiceImpl.java | 7 +++++++ .../src/main/resources/bootstrap.properties | 18 +++++++++--------- .../com/daqing/framework/util/JwtUtils.java | 9 +++++++++ 5 files changed, 40 insertions(+), 13 deletions(-) diff --git a/dq-financial-hrms-auth/src/main/java/com/daqing/financial/hrauth/aspect/Operation.java b/dq-financial-hrms-auth/src/main/java/com/daqing/financial/hrauth/aspect/Operation.java index 3ffb2533..f60bf3f5 100644 --- a/dq-financial-hrms-auth/src/main/java/com/daqing/financial/hrauth/aspect/Operation.java +++ b/dq-financial-hrms-auth/src/main/java/com/daqing/financial/hrauth/aspect/Operation.java @@ -1,5 +1,6 @@ package com.daqing.financial.hrauth.aspect; +import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.daqing.financial.hrauth.annotation.Log; @@ -10,6 +11,7 @@ import com.daqing.financial.hrauth.util.IpUtils; import com.daqing.framework.domain.hrms.SystemLog; import com.daqing.framework.domain.hrms.Token; import com.daqing.framework.domain.hrms.UserEntity; +import com.daqing.framework.util.RedisUtil; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.aspectj.lang.JoinPoint; @@ -100,8 +102,11 @@ public class Operation { //User systemUser = (User) SecurityUtils.getSubject().getPrincipal(); HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); String token = request.getHeader("token"); - Token userToken = tokenService.getOne(new QueryWrapper().eq("token", token)); - UserEntity systemUser = userLoginService.getOne(new QueryWrapper().eq("id",userToken.getUserId())); + String userId = RedisUtil.get("dq:token:"+token); + String userEntityStr = RedisUtil.get("dq:userId:"+userId); + UserEntity systemUser = JSON.parseObject(userEntityStr,UserEntity.class); + //Token userToken = tokenService.getOne(new QueryWrapper().eq("token", token)); + //UserEntity systemUser = userLoginService.getOne(new QueryWrapper().eq("id",userToken.getUserId())); Map map = new HashMap<>(4); for (int i = 0; i < argNames.length; i++) { diff --git a/dq-financial-hrms-auth/src/main/java/com/daqing/financial/hrauth/aspect/SysLogAspect.java b/dq-financial-hrms-auth/src/main/java/com/daqing/financial/hrauth/aspect/SysLogAspect.java index 5944c7d4..4b55e76d 100644 --- a/dq-financial-hrms-auth/src/main/java/com/daqing/financial/hrauth/aspect/SysLogAspect.java +++ b/dq-financial-hrms-auth/src/main/java/com/daqing/financial/hrauth/aspect/SysLogAspect.java @@ -1,5 +1,6 @@ package com.daqing.financial.hrauth.aspect; +import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.daqing.financial.hrauth.annotation.Log; @@ -7,6 +8,7 @@ import com.daqing.financial.hrauth.service.TokenService; import com.daqing.financial.hrauth.service.UserLoginService; import com.daqing.framework.domain.hrms.Token; import com.daqing.framework.domain.hrms.UserEntity; +import com.daqing.framework.util.RedisUtil; import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; @@ -63,8 +65,12 @@ public class SysLogAspect { //User systemUser = (User) SecurityUtils.getSubject().getPrincipal(); HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); String token = request.getHeader("token"); - Token userToken = tokenService.getOne(new QueryWrapper().eq("token", token)); - UserEntity systemUser = userLoginService.getOne(new QueryWrapper().eq("id",userToken.getUserId())); + String userId = RedisUtil.get("dq:token:"+token); + String userEntityStr = RedisUtil.get("dq:userId:"+userId); + UserEntity systemUser = JSON.parseObject(userEntityStr,UserEntity.class); + + //Token userToken = tokenService.getOne(new QueryWrapper().eq("token", token)); + //UserEntity systemUser = userLoginService.getOne(new QueryWrapper().eq("id",userId)); operation.addOperationLog(joinPoint,res,time,systemUser); //方法执行完成后增加日志 // addOperationLog(joinPoint, res, time); diff --git a/dq-financial-hrms-auth/src/main/java/com/daqing/financial/hrauth/service/impl/UserLoginServiceImpl.java b/dq-financial-hrms-auth/src/main/java/com/daqing/financial/hrauth/service/impl/UserLoginServiceImpl.java index e6566726..bff1c7fc 100644 --- a/dq-financial-hrms-auth/src/main/java/com/daqing/financial/hrauth/service/impl/UserLoginServiceImpl.java +++ b/dq-financial-hrms-auth/src/main/java/com/daqing/financial/hrauth/service/impl/UserLoginServiceImpl.java @@ -18,6 +18,7 @@ import com.daqing.framework.model.response.CommonCode; import com.daqing.framework.model.response.ResponseResult; import com.daqing.framework.util.JwtUtils; import com.daqing.framework.util.Md5Util; +import com.daqing.framework.util.RedisUtil; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -120,6 +121,10 @@ public class UserLoginServiceImpl extends ServiceImpl public ResponseResult login(LoginRequest loginRequest) { //判断用户登录方式 UserEntity userEntity = baseMapper.login(loginRequest); + if(userEntity != null){ + //删除redis里面的userEntity + JwtUtils.removeUserEntityByUserId(userEntity.getId()); + } if(loginRequest.getType() == 1){//手机号码登录 if(userEntity == null){ @@ -156,6 +161,8 @@ public class UserLoginServiceImpl extends ServiceImpl } //设置最新的token到redis JwtUtils.putTokenToRedis(userEntity.getId(), token, times); + //设置userEntity到redis + JwtUtils.putUserEntityToRedis(userEntity.getId(),userEntity,times); //新增登录日志 LoginLog loginLog = loginLogService.getOne(new QueryWrapper().eq("user_id", userEntity.getId())); diff --git a/dq-financial-hrms/src/main/resources/bootstrap.properties b/dq-financial-hrms/src/main/resources/bootstrap.properties index b5a7eb56..c22d4f26 100644 --- a/dq-financial-hrms/src/main/resources/bootstrap.properties +++ b/dq-financial-hrms/src/main/resources/bootstrap.properties @@ -17,12 +17,12 @@ spring.cloud.nacos.config.ext-config[2].data-id=other.yml spring.cloud.nacos.config.ext-config[2].group=dev spring.cloud.nacos.config.ext-config[2].refresh=true -#spring.redis.host=127.0.0.1 -#spring.redis.port=6379 -#spring.redis.password=123456 -#spring.redis.database=0 -#spring.redis.timeout=30000 -#spring.redis.jedis.pool.max-active=8 -#spring.redis.jedis.pool.max-wait=-1 -#spring.redis.jedis.pool.max-idle=8 -#spring.redis.jedis.pool.min-idle=0 \ No newline at end of file +spring.redis.host=127.0.0.1 +spring.redis.port=6379 +spring.redis.password=123456 +spring.redis.database=0 +spring.redis.timeout=30000 +spring.redis.jedis.pool.max-active=8 +spring.redis.jedis.pool.max-wait=-1 +spring.redis.jedis.pool.max-idle=8 +spring.redis.jedis.pool.min-idle=0 \ No newline at end of file diff --git a/dq-framework-common/src/main/java/com/daqing/framework/util/JwtUtils.java b/dq-framework-common/src/main/java/com/daqing/framework/util/JwtUtils.java index 1af3149f..54d228de 100644 --- a/dq-framework-common/src/main/java/com/daqing/framework/util/JwtUtils.java +++ b/dq-framework-common/src/main/java/com/daqing/framework/util/JwtUtils.java @@ -1,6 +1,7 @@ package com.daqing.framework.util; +import com.alibaba.fastjson.JSONObject; import io.jsonwebtoken.Claims; import io.jsonwebtoken.ExpiredJwtException; import io.jsonwebtoken.JwtBuilder; @@ -91,6 +92,14 @@ public class JwtUtils { RedisUtil.setEx("dq:token:"+token, String.valueOf(userId), times); } + public static void putUserEntityToRedis(Long userId, UserEntity userEntity, long times) { + RedisUtil.setEx("dq:userId:"+userId, JSONObject.toJSONString(userEntity), times); + } + + public static void removeUserEntityByUserId(Long userId) { + RedisUtil.del("dq:userId:"+userId); + } + public static void removeTokenByToken(String token) { RedisUtil.del("dq:token:"+token); }