优化系统日志部分数据库操作改为redis读取数据

master
shijie 4 years ago
parent 39f77de163
commit b9537077ef
  1. 9
      dq-financial-hrms-auth/src/main/java/com/daqing/financial/hrauth/aspect/Operation.java
  2. 10
      dq-financial-hrms-auth/src/main/java/com/daqing/financial/hrauth/aspect/SysLogAspect.java
  3. 7
      dq-financial-hrms-auth/src/main/java/com/daqing/financial/hrauth/service/impl/UserLoginServiceImpl.java
  4. 18
      dq-financial-hrms/src/main/resources/bootstrap.properties
  5. 9
      dq-framework-common/src/main/java/com/daqing/framework/util/JwtUtils.java

@ -1,5 +1,6 @@
package com.daqing.financial.hrauth.aspect; package com.daqing.financial.hrauth.aspect;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.daqing.financial.hrauth.annotation.Log; 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.SystemLog;
import com.daqing.framework.domain.hrms.Token; import com.daqing.framework.domain.hrms.Token;
import com.daqing.framework.domain.hrms.UserEntity; import com.daqing.framework.domain.hrms.UserEntity;
import com.daqing.framework.util.RedisUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint; import org.aspectj.lang.JoinPoint;
@ -100,8 +102,11 @@ public class Operation {
//User systemUser = (User) SecurityUtils.getSubject().getPrincipal(); //User systemUser = (User) SecurityUtils.getSubject().getPrincipal();
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
String token = request.getHeader("token"); String token = request.getHeader("token");
Token userToken = tokenService.getOne(new QueryWrapper<Token>().eq("token", token)); String userId = RedisUtil.get("dq:token:"+token);
UserEntity systemUser = userLoginService.getOne(new QueryWrapper<UserEntity>().eq("id",userToken.getUserId())); String userEntityStr = RedisUtil.get("dq:userId:"+userId);
UserEntity systemUser = JSON.parseObject(userEntityStr,UserEntity.class);
//Token userToken = tokenService.getOne(new QueryWrapper<Token>().eq("token", token));
//UserEntity systemUser = userLoginService.getOne(new QueryWrapper<UserEntity>().eq("id",userToken.getUserId()));
Map<Object, Object> map = new HashMap<>(4); Map<Object, Object> map = new HashMap<>(4);
for (int i = 0; i < argNames.length; i++) { for (int i = 0; i < argNames.length; i++) {

@ -1,5 +1,6 @@
package com.daqing.financial.hrauth.aspect; package com.daqing.financial.hrauth.aspect;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.daqing.financial.hrauth.annotation.Log; 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.financial.hrauth.service.UserLoginService;
import com.daqing.framework.domain.hrms.Token; import com.daqing.framework.domain.hrms.Token;
import com.daqing.framework.domain.hrms.UserEntity; import com.daqing.framework.domain.hrms.UserEntity;
import com.daqing.framework.util.RedisUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.ProceedingJoinPoint;
@ -63,8 +65,12 @@ public class SysLogAspect {
//User systemUser = (User) SecurityUtils.getSubject().getPrincipal(); //User systemUser = (User) SecurityUtils.getSubject().getPrincipal();
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
String token = request.getHeader("token"); String token = request.getHeader("token");
Token userToken = tokenService.getOne(new QueryWrapper<Token>().eq("token", token)); String userId = RedisUtil.get("dq:token:"+token);
UserEntity systemUser = userLoginService.getOne(new QueryWrapper<UserEntity>().eq("id",userToken.getUserId())); String userEntityStr = RedisUtil.get("dq:userId:"+userId);
UserEntity systemUser = JSON.parseObject(userEntityStr,UserEntity.class);
//Token userToken = tokenService.getOne(new QueryWrapper<Token>().eq("token", token));
//UserEntity systemUser = userLoginService.getOne(new QueryWrapper<UserEntity>().eq("id",userId));
operation.addOperationLog(joinPoint,res,time,systemUser); operation.addOperationLog(joinPoint,res,time,systemUser);
//方法执行完成后增加日志 //方法执行完成后增加日志
// addOperationLog(joinPoint, res, time); // addOperationLog(joinPoint, res, time);

@ -18,6 +18,7 @@ import com.daqing.framework.model.response.CommonCode;
import com.daqing.framework.model.response.ResponseResult; import com.daqing.framework.model.response.ResponseResult;
import com.daqing.framework.util.JwtUtils; import com.daqing.framework.util.JwtUtils;
import com.daqing.framework.util.Md5Util; import com.daqing.framework.util.Md5Util;
import com.daqing.framework.util.RedisUtil;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -120,6 +121,10 @@ public class UserLoginServiceImpl extends ServiceImpl<UserLoginDao, UserEntity>
public ResponseResult login(LoginRequest loginRequest) { public ResponseResult login(LoginRequest loginRequest) {
//判断用户登录方式 //判断用户登录方式
UserEntity userEntity = baseMapper.login(loginRequest); UserEntity userEntity = baseMapper.login(loginRequest);
if(userEntity != null){
//删除redis里面的userEntity
JwtUtils.removeUserEntityByUserId(userEntity.getId());
}
if(loginRequest.getType() == 1){//手机号码登录 if(loginRequest.getType() == 1){//手机号码登录
if(userEntity == null){ if(userEntity == null){
@ -156,6 +161,8 @@ public class UserLoginServiceImpl extends ServiceImpl<UserLoginDao, UserEntity>
} }
//设置最新的token到redis //设置最新的token到redis
JwtUtils.putTokenToRedis(userEntity.getId(), token, times); JwtUtils.putTokenToRedis(userEntity.getId(), token, times);
//设置userEntity到redis
JwtUtils.putUserEntityToRedis(userEntity.getId(),userEntity,times);
//新增登录日志 //新增登录日志
LoginLog loginLog = loginLogService.getOne(new QueryWrapper<LoginLog>().eq("user_id", userEntity.getId())); LoginLog loginLog = loginLogService.getOne(new QueryWrapper<LoginLog>().eq("user_id", userEntity.getId()));

@ -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].group=dev
spring.cloud.nacos.config.ext-config[2].refresh=true spring.cloud.nacos.config.ext-config[2].refresh=true
#spring.redis.host=127.0.0.1 spring.redis.host=127.0.0.1
#spring.redis.port=6379 spring.redis.port=6379
#spring.redis.password=123456 spring.redis.password=123456
#spring.redis.database=0 spring.redis.database=0
#spring.redis.timeout=30000 spring.redis.timeout=30000
#spring.redis.jedis.pool.max-active=8 spring.redis.jedis.pool.max-active=8
#spring.redis.jedis.pool.max-wait=-1 spring.redis.jedis.pool.max-wait=-1
#spring.redis.jedis.pool.max-idle=8 spring.redis.jedis.pool.max-idle=8
#spring.redis.jedis.pool.min-idle=0 spring.redis.jedis.pool.min-idle=0

@ -1,6 +1,7 @@
package com.daqing.framework.util; package com.daqing.framework.util;
import com.alibaba.fastjson.JSONObject;
import io.jsonwebtoken.Claims; import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException; import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.JwtBuilder; import io.jsonwebtoken.JwtBuilder;
@ -91,6 +92,14 @@ public class JwtUtils {
RedisUtil.setEx("dq:token:"+token, String.valueOf(userId), times); RedisUtil.setEx("dq:token:"+token, String.valueOf(userId), times);
} }
public static <UserEntity> 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) { public static void removeTokenByToken(String token) {
RedisUtil.del("dq:token:"+token); RedisUtil.del("dq:token:"+token);
} }

Loading…
Cancel
Save