为了保证JWT
随机生成的密钥一致,我设计了一个token服务,专门获取JWT
,和生成token。在网关使用client
调用服务时,出现了bean
循环依赖
The dependencies of some of the beans in the application context form a cycle:
┌─────┐
| gateWayGlobalFilter defined in file [C:\Users\yinchrn\Desktop\Graduation project\mall-post\springcloud\gateway\target\classes\org\example\filter\GateWayGlobalFilter.class]
↑ ↓
| org.example.client.TokenClient
↑ ↓
| corsGatewayFilterApplicationListener defined in class path resource [org/springframework/cloud/gateway/config/GatewayAutoConfiguration.class]
↑ ↓
| routePredicateHandlerMapping defined in class path resource [org/springframework/cloud/gateway/config/GatewayAutoConfiguration.class]
↑ ↓
| filteringWebHandler defined in class path resource [org/springframework/cloud/gateway/config/GatewayAutoConfiguration.class]
└─────┘
通过在依赖注入时添加@Lazy
解决
@Component
@Slf4j
public class GateWayGlobalFilter implements GlobalFilter, Ordered {
private final AntPathMatcher antPathMatcher = new AntPathMatcher();
@Autowired
private AuthProperties authProperties;
@Lazy
@Autowired
private TokenClient tokenClient;
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
System.out.println("GateWayGlobalFilter---------");
ServerHttpRequest request = exchange.getRequest();
List<String> excludePath = authProperties.getExcludePath();
for (String s : excludePath) {
System.out.println(s);
}
/*
获取头文件验证实例,及拦截
*/
if (isAllowPath(request)) {
log.info("allowPath");
return chain.filter(exchange);
}
/*
* token验证
* */
String token;
Long uid;
List<String> strings = request.getHeaders().get("token");
if (strings != null) {
token = strings.get(0);
log.info("token:{}",token);
try{
uid = tokenClient.getUid(new TokenDTO("0",0L,token)).getData();
//更改传递给微服务的 header
}catch (Exception e){
log.error("token验证失败");
log.error(e.getMessage());
ServerHttpResponse response = exchange.getResponse();
response.setStatusCode(HttpStatus.UNAUTHORIZED);
return response.setComplete();
}
}else {
log.error("token为空");
ServerHttpResponse response = exchange.getResponse();
response.setStatusCode(HttpStatus.UNAUTHORIZED);
return response.setComplete();
}
Long finalUid = uid;
ServerWebExchange webExchange = exchange.mutate().request(
builder -> builder.header("uid", String.valueOf(finalUid)))
.build();
return chain.filter(webExchange);
}
@Override
public int getOrder() {
return 0;
}
public Boolean isAllowPath(ServerHttpRequest request){
// HttpMethod method = request.getMethod();
String path = request.getPath().toString();
System.out.println(path);
List<String> excludePathList = authProperties.getExcludePath();
for (String excludePath : excludePathList) {
if (antPathMatcher.match(excludePath,path)){
return true;
}
}
return false;
}
}
标签:return,OpenFeign,exchange,request,class,token,过滤器,path,Gateway From: https://www.cnblogs.com/yinchrn/p/18075160https://github.com/spring-cloud/spring-cloud-openfeign/issues/142
https://github.com/progress0407/e-commerce-with-msa/issues/1