|
|
|
|
@ -1,13 +1,17 @@
|
|
|
|
|
package cn.iocoder.yudao.gateway.filter.security;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
|
|
import cn.iocoder.yudao.framework.common.core.KeyValue;
|
|
|
|
|
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
|
|
|
|
|
import cn.iocoder.yudao.framework.common.util.cache.CacheUtils;
|
|
|
|
|
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
|
|
|
|
|
import cn.iocoder.yudao.gateway.util.SecurityFrameworkUtils;
|
|
|
|
|
import cn.iocoder.yudao.gateway.util.WebFrameworkUtils;
|
|
|
|
|
import cn.iocoder.yudao.module.system.api.oauth2.OAuth2TokenApi;
|
|
|
|
|
import cn.iocoder.yudao.module.system.api.oauth2.dto.OAuth2AccessTokenCheckRespDTO;
|
|
|
|
|
import com.fasterxml.jackson.core.type.TypeReference;
|
|
|
|
|
import com.google.common.cache.CacheLoader;
|
|
|
|
|
import com.google.common.cache.LoadingCache;
|
|
|
|
|
import org.springframework.cloud.client.loadbalancer.reactive.ReactorLoadBalancerExchangeFilterFunction;
|
|
|
|
|
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
|
|
|
|
import org.springframework.cloud.gateway.filter.GlobalFilter;
|
|
|
|
|
@ -17,6 +21,7 @@ import org.springframework.web.reactive.function.client.WebClient;
|
|
|
|
|
import org.springframework.web.server.ServerWebExchange;
|
|
|
|
|
import reactor.core.publisher.Mono;
|
|
|
|
|
|
|
|
|
|
import java.time.Duration;
|
|
|
|
|
import java.util.function.Function;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
@ -34,6 +39,17 @@ public class TokenAuthenticationFilter implements GlobalFilter, Ordered {
|
|
|
|
|
|
|
|
|
|
private final WebClient webClient;
|
|
|
|
|
|
|
|
|
|
private final LoadingCache<KeyValue<Long, String>, LoginUser> loginUserCache = CacheUtils.buildAsyncReloadingCache(Duration.ofMinutes(1),
|
|
|
|
|
new CacheLoader<KeyValue<Long, String>, LoginUser>() {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public LoginUser load(KeyValue<Long, String> keyValue) {
|
|
|
|
|
String body = checkAccessToken(keyValue.getKey(), keyValue.getValue()).block();
|
|
|
|
|
return buildUser(body);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
public TokenAuthenticationFilter(ReactorLoadBalancerExchangeFilterFunction lbFunction) {
|
|
|
|
|
// Q:为什么不使用 OAuth2TokenApi 进行调用?
|
|
|
|
|
// A1:Spring Cloud OpenFeign 官方未内置 Reactive 的支持 https://docs.spring.io/spring-cloud-openfeign/docs/current/reference/html/#reactive-support
|
|
|
|
|
@ -54,24 +70,50 @@ public class TokenAuthenticationFilter implements GlobalFilter, Ordered {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 情况二,如果有 Token 令牌,则解析对应 userId、userType、tenantId 等字段,并通过 通过 Header 转发给服务
|
|
|
|
|
Long tenantId = WebFrameworkUtils.getTenantId(exchange);
|
|
|
|
|
KeyValue<Long, String> cacheKey = new KeyValue<Long, String>().setKey(tenantId).setValue(token);
|
|
|
|
|
LoginUser user = loginUserCache.getUnchecked(cacheKey);
|
|
|
|
|
if (user != null) {
|
|
|
|
|
SecurityFrameworkUtils.setLoginUser(exchange, user);
|
|
|
|
|
return chain.filter(exchange.mutate().request(builder -> SecurityFrameworkUtils.setLoginUserHeader(builder, user)).build());
|
|
|
|
|
}
|
|
|
|
|
return checkAccessToken(cacheKey.getKey(), token)
|
|
|
|
|
.flatMap((Function<String, Mono<Void>>) body -> chain.filter(buildNewServerWebExchange(exchange, cacheKey, body))); // 处理请求的结果
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Mono<String> checkAccessToken(Long tenantId, String token) {
|
|
|
|
|
return webClient.get()
|
|
|
|
|
.uri(OAuth2TokenApi.URL_CHECK, uriBuilder -> uriBuilder.queryParam("accessToken", token).build())
|
|
|
|
|
.headers(httpHeaders -> WebFrameworkUtils.setTenantIdHeader(exchange, httpHeaders)) // 设置租户的 Header
|
|
|
|
|
.retrieve().bodyToMono(String.class) // 发起请求,设置 body 为 String 结果
|
|
|
|
|
.flatMap((Function<String, Mono<Void>>) body -> chain.filter(buildNewServerWebExchange(exchange, body))); // 处理请求的结果
|
|
|
|
|
.headers(httpHeaders -> WebFrameworkUtils.setTenantIdHeader(tenantId, httpHeaders)) // 设置租户的 Header
|
|
|
|
|
.retrieve().bodyToMono(String.class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ServerWebExchange buildNewServerWebExchange(ServerWebExchange exchange, String body) {
|
|
|
|
|
// 校验 Token 令牌失败,则直接返回
|
|
|
|
|
CommonResult<OAuth2AccessTokenCheckRespDTO> result = JsonUtils.parseObject(body, CHECK_RESULT_TYPE_REFERENCE);
|
|
|
|
|
if (result == null || result.isError()) {
|
|
|
|
|
private ServerWebExchange buildNewServerWebExchange(ServerWebExchange exchange, KeyValue<Long, String> cacheKey, String body) {
|
|
|
|
|
// 1.1 解析 User
|
|
|
|
|
LoginUser user = buildUser(body);
|
|
|
|
|
// 1.2 校验 Token 令牌失败,则直接返回
|
|
|
|
|
if (user == null) {
|
|
|
|
|
return exchange;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置登录用户
|
|
|
|
|
SecurityFrameworkUtils.setLoginUser(exchange, result.getData());
|
|
|
|
|
// 将访问令牌封装成 LoginUser,并设置到 login-user 的请求头,使用 json 存储值
|
|
|
|
|
return exchange.mutate().request(builder -> SecurityFrameworkUtils.setLoginUserHeader(builder, result.getData())).build();
|
|
|
|
|
// 2. 设置到缓存
|
|
|
|
|
loginUserCache.put(cacheKey, user);
|
|
|
|
|
|
|
|
|
|
// 3.1 设置登录用户
|
|
|
|
|
SecurityFrameworkUtils.setLoginUser(exchange, user);
|
|
|
|
|
// 3.2 将 user 并设置到 login-user 的请求头,使用 json 存储值
|
|
|
|
|
return exchange.mutate().request(builder -> SecurityFrameworkUtils.setLoginUserHeader(builder, user)).build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private LoginUser buildUser(String body) {
|
|
|
|
|
CommonResult<OAuth2AccessTokenCheckRespDTO> result = JsonUtils.parseObject(body, CHECK_RESULT_TYPE_REFERENCE);
|
|
|
|
|
if (result == null || result.isError()) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
// 创建登录用户
|
|
|
|
|
OAuth2AccessTokenCheckRespDTO tokenInfo = result.getData();
|
|
|
|
|
return new LoginUser().setId(tokenInfo.getUserId()).setUserType(tokenInfo.getUserType())
|
|
|
|
|
.setTenantId(tokenInfo.getTenantId()).setScopes(tokenInfo.getScopes());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|