|
|
@@ -7,19 +7,15 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|
7
|
7
|
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
|
8
|
8
|
import org.springframework.cloud.gateway.filter.GlobalFilter;
|
|
9
|
9
|
import org.springframework.core.Ordered;
|
|
10
|
|
-import org.springframework.core.io.buffer.DataBufferFactory;
|
|
11
|
10
|
import org.springframework.data.redis.core.ValueOperations;
|
|
12
|
|
-import org.springframework.http.HttpStatus;
|
|
13
|
|
-import org.springframework.http.MediaType;
|
|
14
|
11
|
import org.springframework.http.server.reactive.ServerHttpRequest;
|
|
15
|
|
-import org.springframework.http.server.reactive.ServerHttpResponse;
|
|
16
|
12
|
import org.springframework.stereotype.Component;
|
|
17
|
13
|
import org.springframework.web.server.ServerWebExchange;
|
|
18
|
|
-import com.alibaba.fastjson.JSON;
|
|
19
|
14
|
import com.alibaba.fastjson.JSONObject;
|
|
20
|
15
|
import com.ruoyi.common.core.constant.CacheConstants;
|
|
21
|
16
|
import com.ruoyi.common.core.constant.Constants;
|
|
22
|
|
-import com.ruoyi.common.core.domain.R;
|
|
|
17
|
+import com.ruoyi.common.core.constant.HttpStatus;
|
|
|
18
|
+import com.ruoyi.common.core.utils.SecurityUtils;
|
|
23
|
19
|
import com.ruoyi.common.core.utils.ServletUtils;
|
|
24
|
20
|
import com.ruoyi.common.core.utils.StringUtils;
|
|
25
|
21
|
import com.ruoyi.common.redis.service.RedisService;
|
|
|
@@ -35,7 +31,7 @@ import reactor.core.publisher.Mono;
|
|
35
|
31
|
public class AuthFilter implements GlobalFilter, Ordered
|
|
36
|
32
|
{
|
|
37
|
33
|
private static final Logger log = LoggerFactory.getLogger(AuthFilter.class);
|
|
38
|
|
-
|
|
|
34
|
+
|
|
39
|
35
|
private final static long EXPIRE_TIME = Constants.TOKEN_EXPIRE * 60;
|
|
40
|
36
|
|
|
41
|
37
|
// 排除过滤的 uri 地址,nacos自行添加
|
|
|
@@ -44,61 +40,68 @@ public class AuthFilter implements GlobalFilter, Ordered
|
|
44
|
40
|
|
|
45
|
41
|
@Resource(name = "stringRedisTemplate")
|
|
46
|
42
|
private ValueOperations<String, String> sops;
|
|
47
|
|
-
|
|
|
43
|
+
|
|
48
|
44
|
@Autowired
|
|
49
|
45
|
private RedisService redisService;
|
|
50
|
46
|
|
|
51
|
47
|
@Override
|
|
52
|
48
|
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain)
|
|
53
|
49
|
{
|
|
54
|
|
- String url = exchange.getRequest().getURI().getPath();
|
|
|
50
|
+ ServerHttpRequest request = exchange.getRequest();
|
|
|
51
|
+ ServerHttpRequest.Builder mutate = request.mutate();
|
|
|
52
|
+
|
|
|
53
|
+ String url = request.getURI().getPath();
|
|
55
|
54
|
// 跳过不需要验证的路径
|
|
56
|
55
|
if (StringUtils.matches(url, ignoreWhite.getWhites()))
|
|
57
|
56
|
{
|
|
58
|
57
|
return chain.filter(exchange);
|
|
59
|
58
|
}
|
|
60
|
|
- String token = getToken(exchange.getRequest());
|
|
61
|
|
- if (StringUtils.isBlank(token))
|
|
|
59
|
+ String token = getToken(request);
|
|
|
60
|
+ if (StringUtils.isEmpty(token))
|
|
62
|
61
|
{
|
|
63
|
|
- return setUnauthorizedResponse(exchange, "令牌不能为空");
|
|
|
62
|
+ return unauthorizedResponse(exchange, "令牌不能为空");
|
|
64
|
63
|
}
|
|
65
|
64
|
String userStr = sops.get(getTokenKey(token));
|
|
66
|
|
- if (StringUtils.isNull(userStr))
|
|
|
65
|
+ if (StringUtils.isEmpty(userStr))
|
|
67
|
66
|
{
|
|
68
|
|
- return setUnauthorizedResponse(exchange, "登录状态已过期");
|
|
|
67
|
+ return unauthorizedResponse(exchange, "登录状态已过期");
|
|
69
|
68
|
}
|
|
70
|
|
- JSONObject obj = JSONObject.parseObject(userStr);
|
|
71
|
|
- String userid = obj.getString("userid");
|
|
72
|
|
- String username = obj.getString("username");
|
|
73
|
|
- if (StringUtils.isBlank(userid) || StringUtils.isBlank(username))
|
|
|
69
|
+ JSONObject cacheObj = JSONObject.parseObject(userStr);
|
|
|
70
|
+ String userid = cacheObj.getString("userid");
|
|
|
71
|
+ String username = cacheObj.getString("username");
|
|
|
72
|
+ if (StringUtils.isEmpty(userid) || StringUtils.isEmpty(username))
|
|
74
|
73
|
{
|
|
75
|
|
- return setUnauthorizedResponse(exchange, "令牌验证失败");
|
|
|
74
|
+ return unauthorizedResponse(exchange, "令牌验证失败");
|
|
76
|
75
|
}
|
|
77
|
|
-
|
|
|
76
|
+
|
|
78
|
77
|
// 设置过期时间
|
|
79
|
78
|
redisService.expire(getTokenKey(token), EXPIRE_TIME);
|
|
80
|
79
|
// 设置用户信息到请求
|
|
81
|
|
- ServerHttpRequest mutableReq = exchange.getRequest().mutate().header(CacheConstants.DETAILS_USER_ID, userid)
|
|
82
|
|
- .header(CacheConstants.DETAILS_USERNAME, ServletUtils.urlEncode(username)).build();
|
|
83
|
|
- ServerWebExchange mutableExchange = exchange.mutate().request(mutableReq).build();
|
|
84
|
|
-
|
|
85
|
|
- return chain.filter(mutableExchange);
|
|
|
80
|
+ addHeader(mutate, CacheConstants.DETAILS_USER_ID, userid);
|
|
|
81
|
+ addHeader(mutate, CacheConstants.DETAILS_USERNAME, username);
|
|
|
82
|
+ return chain.filter(exchange.mutate().request(mutate.build()).build());
|
|
86
|
83
|
}
|
|
87
|
84
|
|
|
88
|
|
- private Mono<Void> setUnauthorizedResponse(ServerWebExchange exchange, String msg)
|
|
|
85
|
+ private void addHeader(ServerHttpRequest.Builder mutate, String name, Object value)
|
|
89
|
86
|
{
|
|
90
|
|
- ServerHttpResponse response = exchange.getResponse();
|
|
91
|
|
- response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
|
|
92
|
|
- response.setStatusCode(HttpStatus.OK);
|
|
|
87
|
+ if (value == null)
|
|
|
88
|
+ {
|
|
|
89
|
+ return;
|
|
|
90
|
+ }
|
|
|
91
|
+ String valueStr = value.toString();
|
|
|
92
|
+ String valueEncode = ServletUtils.urlEncode(valueStr);
|
|
|
93
|
+ mutate.header(name, valueEncode);
|
|
|
94
|
+ }
|
|
93
|
95
|
|
|
|
96
|
+ private Mono<Void> unauthorizedResponse(ServerWebExchange exchange, String msg)
|
|
|
97
|
+ {
|
|
94
|
98
|
log.error("[鉴权异常处理]请求路径:{}", exchange.getRequest().getPath());
|
|
95
|
|
-
|
|
96
|
|
- return response.writeWith(Mono.fromSupplier(() -> {
|
|
97
|
|
- DataBufferFactory bufferFactory = response.bufferFactory();
|
|
98
|
|
- return bufferFactory.wrap(JSON.toJSONBytes(R.fail(HttpStatus.UNAUTHORIZED.value(), msg)));
|
|
99
|
|
- }));
|
|
|
99
|
+ return ServletUtils.webFluxResponseWriter(exchange.getResponse(), msg, HttpStatus.UNAUTHORIZED);
|
|
100
|
100
|
}
|
|
101
|
101
|
|
|
|
102
|
+ /**
|
|
|
103
|
+ * 获取缓存key
|
|
|
104
|
+ */
|
|
102
|
105
|
private String getTokenKey(String token)
|
|
103
|
106
|
{
|
|
104
|
107
|
return CacheConstants.LOGIN_TOKEN_KEY + token;
|
|
|
@@ -109,12 +112,8 @@ public class AuthFilter implements GlobalFilter, Ordered
|
|
109
|
112
|
*/
|
|
110
|
113
|
private String getToken(ServerHttpRequest request)
|
|
111
|
114
|
{
|
|
112
|
|
- String token = request.getHeaders().getFirst(CacheConstants.HEADER);
|
|
113
|
|
- if (StringUtils.isNotEmpty(token) && token.startsWith(CacheConstants.TOKEN_PREFIX))
|
|
114
|
|
- {
|
|
115
|
|
- token = token.replace(CacheConstants.TOKEN_PREFIX, "");
|
|
116
|
|
- }
|
|
117
|
|
- return token;
|
|
|
115
|
+ String token = request.getHeaders().getFirst(CacheConstants.TOKEN_AUTHENTICATION);
|
|
|
116
|
+ return SecurityUtils.replaceTokenPrefix(token);
|
|
118
|
117
|
}
|
|
119
|
118
|
|
|
120
|
119
|
@Override
|