為了保證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;
}
}
https://github.com/spring-cloud/spring-cloud-openfeign/issues/142
https://github.com/progress0407/e-commerce-with-msa/issues/1