Просмотр исходного кода

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

RuoYi лет назад: 5
Родитель
Сommit
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
      * 参数管理 cache key
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
 import com.alibaba.fastjson.JSON;
18
 import com.alibaba.fastjson.JSON;
19
 import com.alibaba.fastjson.JSONObject;
19
 import com.alibaba.fastjson.JSONObject;
20
 import com.ruoyi.common.core.constant.CacheConstants;
20
 import com.ruoyi.common.core.constant.CacheConstants;
21
+import com.ruoyi.common.core.constant.Constants;
21
 import com.ruoyi.common.core.domain.R;
22
 import com.ruoyi.common.core.domain.R;
22
 import com.ruoyi.common.core.utils.StringUtils;
23
 import com.ruoyi.common.core.utils.StringUtils;
24
+import com.ruoyi.common.redis.service.RedisService;
23
 import com.ruoyi.gateway.config.properties.IgnoreWhiteProperties;
25
 import com.ruoyi.gateway.config.properties.IgnoreWhiteProperties;
24
 import reactor.core.publisher.Mono;
26
 import reactor.core.publisher.Mono;
25
 
27
 
@@ -32,6 +34,8 @@ import reactor.core.publisher.Mono;
32
 public class AuthFilter implements GlobalFilter, Ordered
34
 public class AuthFilter implements GlobalFilter, Ordered
33
 {
35
 {
34
     private static final Logger log = LoggerFactory.getLogger(AuthFilter.class);
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
     // 排除过滤的 uri 地址,nacos自行添加
40
     // 排除过滤的 uri 地址,nacos自行添加
37
     @Autowired
41
     @Autowired
@@ -39,6 +43,9 @@ public class AuthFilter implements GlobalFilter, Ordered
39
 
43
 
40
     @Resource(name = "stringRedisTemplate")
44
     @Resource(name = "stringRedisTemplate")
41
     private ValueOperations<String, String> sops;
45
     private ValueOperations<String, String> sops;
46
+    
47
+    @Autowired
48
+    private RedisService redisService;
42
 
49
 
43
     @Override
50
     @Override
44
     public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain)
51
     public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain)
@@ -54,7 +61,7 @@ public class AuthFilter implements GlobalFilter, Ordered
54
         {
61
         {
55
             return setUnauthorizedResponse(exchange, "令牌不能为空");
62
             return setUnauthorizedResponse(exchange, "令牌不能为空");
56
         }
63
         }
57
-        String userStr = sops.get(CacheConstants.LOGIN_TOKEN_KEY + token);
64
+        String userStr = sops.get(getTokenKey(token));
58
         if (StringUtils.isNull(userStr))
65
         if (StringUtils.isNull(userStr))
59
         {
66
         {
60
             return setUnauthorizedResponse(exchange, "登录状态已过期");
67
             return setUnauthorizedResponse(exchange, "登录状态已过期");
@@ -66,6 +73,9 @@ public class AuthFilter implements GlobalFilter, Ordered
66
         {
73
         {
67
             return setUnauthorizedResponse(exchange, "令牌验证失败");
74
             return setUnauthorizedResponse(exchange, "令牌验证失败");
68
         }
75
         }
76
+        
77
+        // 设置过期时间
78
+        redisService.expire(getTokenKey(token), EXPIRE_TIME);
69
         // 设置用户信息到请求
79
         // 设置用户信息到请求
70
         ServerHttpRequest mutableReq = exchange.getRequest().mutate().header(CacheConstants.DETAILS_USER_ID, userid)
80
         ServerHttpRequest mutableReq = exchange.getRequest().mutate().header(CacheConstants.DETAILS_USER_ID, userid)
71
                 .header(CacheConstants.DETAILS_USERNAME, username).build();
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
      * 获取请求token
107
      * 获取请求token
93
      */
108
      */

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

@@ -35,7 +35,6 @@ import LineChart from './dashboard/LineChart'
35
 import RaddarChart from './dashboard/RaddarChart'
35
 import RaddarChart from './dashboard/RaddarChart'
36
 import PieChart from './dashboard/PieChart'
36
 import PieChart from './dashboard/PieChart'
37
 import BarChart from './dashboard/BarChart'
37
 import BarChart from './dashboard/BarChart'
38
-import { getToken, getExpiresIn, setExpiresIn } from '@/utils/auth'
39
 
38
 
40
 const lineChartData = {
39
 const lineChartData = {
41
   newVisitis: {
40
   newVisitis: {
@@ -67,39 +66,12 @@ export default {
67
   },
66
   },
68
   data() {
67
   data() {
69
     return {
68
     return {
70
-      //刷新token锁
71
-      refreshLock: false,
72
-      //刷新token的时间
73
-      refreshTime: '',
74
       lineChartData: lineChartData.newVisitis
69
       lineChartData: lineChartData.newVisitis
75
     }
70
     }
76
   },
71
   },
77
-  created() {
78
-    this.refreshToken()
79
-  },
80
   methods: {
72
   methods: {
81
     handleSetLineChartData(type) {
73
     handleSetLineChartData(type) {
82
       this.lineChartData = lineChartData[type]
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
 }