Browse Source

4/10 登录修改

lwhhszx 1 year ago
parent
commit
83ce0cb3bf

+ 3 - 1
src/main/java/com/example/xiaoshiweixinback/business/utils/CacheUtil.java

@@ -6,6 +6,8 @@ import com.example.xiaoshiweixinback.entity.vo.PersonnelVO;
 import jakarta.annotation.Resource;
 import org.springframework.stereotype.Component;
 
+import java.util.concurrent.TimeUnit;
+
 @Component
 public class CacheUtil {
     @Resource
@@ -13,7 +15,7 @@ public class CacheUtil {
 
 
     public void setLoginUser(PersonnelVO personnelVO,String token) {
-        redisUtil.set(token, JsonUtils.objectToJson(personnelVO));
+        redisUtil.setEx(token,JsonUtils.objectToJson(personnelVO),3L,TimeUnit.HOURS);
     }
     public PersonnelVO getLoginUser(String token) {
         String json = redisUtil.get(token);

+ 10 - 4
src/main/java/com/example/xiaoshiweixinback/checkLogin/CheckLoginAop.java

@@ -7,7 +7,9 @@ import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.example.xiaoshiweixinback.business.common.Response;
 import com.example.xiaoshiweixinback.business.utils.CacheUtil;
+import com.example.xiaoshiweixinback.business.utils.JsonUtils;
 import com.example.xiaoshiweixinback.business.utils.LoginUtils;
+import com.example.xiaoshiweixinback.business.utils.RedisUtil;
 import com.example.xiaoshiweixinback.entity.vo.PersonnelVO;
 import io.swagger.v3.oas.annotations.Operation;
 import org.aspectj.lang.JoinPoint;
@@ -22,6 +24,7 @@ import javax.script.ScriptEngine;
 import javax.script.ScriptEngineManager;
 import java.lang.reflect.Method;
 import java.util.List;
+import java.util.concurrent.TimeUnit;
 
 @Order(2)
 @Aspect
@@ -29,6 +32,8 @@ import java.util.List;
 public class CheckLoginAop {
     @Autowired
     private CacheUtil cacheUtil;
+    @Autowired
+    private RedisUtil redisUtil;
 
     /**
      * 定义切点
@@ -52,11 +57,12 @@ public class CheckLoginAop {
     public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
         // 是否通过切面过滤标记
         Boolean isPass = true;
-        try { cacheUtil.getLoginUser(LoginUtils.getToken());
-        }
-    catch (Exception e){
+        try {
+            PersonnelVO personnelVO = cacheUtil.getLoginUser(LoginUtils.getToken());
+            redisUtil.setEx(LoginUtils.getToken(), JsonUtils.objectToJson(personnelVO), 3L, TimeUnit.HOURS);
+        } catch (Exception e) {
             return Response.unLogin("未登录");
-    }
+        }
         //判断不通过
         if (!isPass) {
             return Response.unLogin("未登录");