|
|
@@ -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
|
*/
|