Przeglądaj źródła

token续期调整到后端&默认有效期延长

RuoYi 5 lat temu
rodzic
commit
0e21fab978

+ 1 - 1
ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/constant/Constants.java

@@ -90,7 +90,7 @@ public class Constants
90 90
     /**
91 91
      * 令牌有效期(分钟)
92 92
      */
93
-    public final static long TOKEN_EXPIRE = 30;
93
+    public final static long TOKEN_EXPIRE = 720;
94 94
 
95 95
     /**
96 96
      * 参数管理 cache key

+ 16 - 1
ruoyi-gateway/src/main/java/com/ruoyi/gateway/filter/AuthFilter.java

@@ -18,8 +18,10 @@ import org.springframework.web.server.ServerWebExchange;
18 18
 import com.alibaba.fastjson.JSON;
19 19
 import com.alibaba.fastjson.JSONObject;
20 20
 import com.ruoyi.common.core.constant.CacheConstants;
21
+import com.ruoyi.common.core.constant.Constants;
21 22
 import com.ruoyi.common.core.domain.R;
22 23
 import com.ruoyi.common.core.utils.StringUtils;
24
+import com.ruoyi.common.redis.service.RedisService;
23 25
 import com.ruoyi.gateway.config.properties.IgnoreWhiteProperties;
24 26
 import reactor.core.publisher.Mono;
25 27
 
@@ -32,6 +34,8 @@ import reactor.core.publisher.Mono;
32 34
 public class AuthFilter implements GlobalFilter, Ordered
33 35
 {
34 36
     private static final Logger log = LoggerFactory.getLogger(AuthFilter.class);
37
+    
38
+    private final static long EXPIRE_TIME = Constants.TOKEN_EXPIRE * 60;
35 39
 
36 40
     // 排除过滤的 uri 地址,nacos自行添加
37 41
     @Autowired
@@ -39,6 +43,9 @@ public class AuthFilter implements GlobalFilter, Ordered
39 43
 
40 44
     @Resource(name = "stringRedisTemplate")
41 45
     private ValueOperations<String, String> sops;
46
+    
47
+    @Autowired
48
+    private RedisService redisService;
42 49
 
43 50
     @Override
44 51
     public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain)
@@ -54,7 +61,7 @@ public class AuthFilter implements GlobalFilter, Ordered
54 61
         {
55 62
             return setUnauthorizedResponse(exchange, "令牌不能为空");
56 63
         }
57
-        String userStr = sops.get(CacheConstants.LOGIN_TOKEN_KEY + token);
64
+        String userStr = sops.get(getTokenKey(token));
58 65
         if (StringUtils.isNull(userStr))
59 66
         {
60 67
             return setUnauthorizedResponse(exchange, "登录状态已过期");
@@ -66,6 +73,9 @@ public class AuthFilter implements GlobalFilter, Ordered
66 73
         {
67 74
             return setUnauthorizedResponse(exchange, "令牌验证失败");
68 75
         }
76
+        
77
+        // 设置过期时间
78
+        redisService.expire(getTokenKey(token), EXPIRE_TIME);
69 79
         // 设置用户信息到请求
70 80
         ServerHttpRequest mutableReq = exchange.getRequest().mutate().header(CacheConstants.DETAILS_USER_ID, userid)
71 81
                 .header(CacheConstants.DETAILS_USERNAME, username).build();
@@ -88,6 +98,11 @@ public class AuthFilter implements GlobalFilter, Ordered
88 98
         }));
89 99
     }
90 100
 
101
+    private String getTokenKey(String token)
102
+    {
103
+        return CacheConstants.LOGIN_TOKEN_KEY + token;
104
+    }
105
+
91 106
     /**
92 107
      * 获取请求token
93 108
      */

+ 0 - 28
ruoyi-ui/src/views/index.vue

@@ -35,7 +35,6 @@ import LineChart from './dashboard/LineChart'
35 35
 import RaddarChart from './dashboard/RaddarChart'
36 36
 import PieChart from './dashboard/PieChart'
37 37
 import BarChart from './dashboard/BarChart'
38
-import { getToken, getExpiresIn, setExpiresIn } from '@/utils/auth'
39 38
 
40 39
 const lineChartData = {
41 40
   newVisitis: {
@@ -67,39 +66,12 @@ export default {
67 66
   },
68 67
   data() {
69 68
     return {
70
-      //刷新token锁
71
-      refreshLock: false,
72
-      //刷新token的时间
73
-      refreshTime: '',
74 69
       lineChartData: lineChartData.newVisitis
75 70
     }
76 71
   },
77
-  created() {
78
-    this.refreshToken()
79
-  },
80 72
   methods: {
81 73
     handleSetLineChartData(type) {
82 74
       this.lineChartData = lineChartData[type]
83
-    },
84
-    // 实时检测刷新token
85
-    refreshToken() {
86
-      this.refreshTime = setInterval(() => {
87
-        if (null === getToken()) {
88
-          return;
89
-        }
90
-        const expires_in = getExpiresIn();
91
-        if (expires_in <= 1200 && !this.refreshLock) {
92
-          this.refreshLock = true
93
-          this.$store
94
-            .dispatch('RefreshToken')
95
-            .catch(() => {
96
-              clearInterval(this.refreshTime)
97
-            });
98
-          this.refreshLock = false
99
-        }
100
-        this.$store.commit("SET_EXPIRES_IN", expires_in - 10);
101
-        setExpiresIn(expires_in - 10);
102
-      }, 10000);
103 75
     }
104 76
   }
105 77
 }