禅道问题修复:1、组织套餐允许超管操作;2、角色权限修改触发角色下用户的token缓存删除;3、网关checktoken移除本地缓存,直接查feignapi;4、order_goods的goods_id转为字符串类型的相关代码适配;

This commit is contained in:
gaoqr
2024-09-10 18:28:34 +08:00
parent 433e6825e5
commit e7192ff6cd
8 changed files with 182 additions and 98 deletions
@@ -1,17 +1,13 @@
package com.cf.imes.gateway.filter.security;
import cn.hutool.core.util.StrUtil;
import com.cf.imes.framework.common.core.KeyValue;
import cn.hutool.core.text.CharSequenceUtil;
import com.cf.imes.framework.common.pojo.CommonResult;
import com.cf.imes.framework.common.util.cache.CacheUtils;
import com.cf.imes.framework.common.util.json.JsonUtils;
import com.cf.imes.gateway.util.SecurityFrameworkUtils;
import com.cf.imes.gateway.util.WebFrameworkUtils;
import com.cf.imes.module.system.api.oauth2.OAuth2TokenApi;
import com.cf.imes.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;
@@ -22,7 +18,6 @@ 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.Objects;
import java.util.function.Function;
@@ -53,23 +48,6 @@ public class TokenAuthenticationFilter implements GlobalFilter, Ordered {
private final WebClient webClient;
/**
* 登录用户的本地缓存
*
* key1:多组织的编号
* key2:访问令牌
*/
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> token) {
String body = checkAccessToken(token.getKey(), token.getValue()).block();
return buildUser(body);
}
});
public TokenAuthenticationFilter(ReactorLoadBalancerExchangeFilterFunction lbFunction) {
// Q:为什么不使用 OAuth2TokenApi 进行调用?
// A1Spring Cloud OpenFeign 官方未内置 Reactive 的支持 https://docs.spring.io/spring-cloud-openfeign/docs/current/reference/html/#reactive-support
@@ -85,7 +63,7 @@ public class TokenAuthenticationFilter implements GlobalFilter, Ordered {
// 情况一,如果没有 Token 令牌,则直接继续 filter
String token = SecurityFrameworkUtils.obtainAuthorization(exchange);
if (StrUtil.isEmpty(token)) {
if (CharSequenceUtil.isEmpty(token)) {
return chain.filter(exchange);
}
@@ -108,21 +86,11 @@ public class TokenAuthenticationFilter implements GlobalFilter, Ordered {
}
private Mono<LoginUser> getLoginUser(ServerWebExchange exchange, String token) {
//Long organId = organIdCache.getIfPresent(token);
// 从缓存中,获取 LoginUser
// 用token从checktoken api中获取当前用户
Long organId = WebFrameworkUtils.getOrganId(exchange);
KeyValue<Long, String> cacheKey = new KeyValue<Long, String>().setKey(organId).setValue(token);
LoginUser localUser = loginUserCache.getIfPresent(cacheKey);
if (localUser != null) {
return Mono.just(localUser);
}
// 缓存不存在,则请求远程服务
return checkAccessToken(organId, token).flatMap((Function<String, Mono<LoginUser>>) body -> {
LoginUser remoteUser = buildUser(body);
if (remoteUser != null) {
// 非空,则进行缓存
loginUserCache.put(cacheKey, remoteUser);
return Mono.just(remoteUser);
}
return Mono.empty();