从春云网关调用微服务



在spring云网关中,添加了一个过滤器,检查身份验证和授权,以便进一步处理请求。我正在使用伪客户端调用身份验证服务,但通过spring云网关调用我的服务时,我得到以下错误。

. lang。IllegalStateException: block()/blockFirst()/blockLast()正在阻塞,这在线程reactor-http-epoll-3中不支持n the reactor.core.publisher. blockingsinglessubscriberer . blockingget (blockingsinglessubscriberer .java:83)ntSuppressed: reactor.core.publisher。FluxOnAssembly$OnAssemblyException: nError已在以下站点观察到:nt|_ checkpoint"org.springframework.cloud.gateway.filter.WeightCalculatorWebFilter .....">

我想知道我使用的是错误的架构。如何继续?我被这个错误卡住了

@Autowired
private AuthenticationService authService;
// route validator
@Autowired
private RouterValidator routerValidator;
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
ServerHttpRequest request = exchange.getRequest();
if (routerValidator.isSecured.test(request)) {
log.info("Accessing the restricted path");
if (this.isAuthMissing(request))
return this.onError(exchange, "Authorization header is missing in request", HttpStatus.UNAUTHORIZED);

final String token = this.getAuthHeader(request);
log.info("before authservice call");
AuthenticationResponse user = authService.isTokenValid(token);
log.info("after authservice call");
if (!user.isValid())
return this.onError(exchange, "Authorization header is invalid", HttpStatus.UNAUTHORIZED);
log.info("before calling populatedRequest");
this.populateRequestWithHeaders(exchange, user);
}
return chain.filter(exchange);
}
private Mono<Void> onError(ServerWebExchange exchange, String err, HttpStatus httpStatus) {
ServerHttpResponse response = exchange.getResponse();
response.setStatusCode(httpStatus);
return response.setComplete();
}
private String getAuthHeader(ServerHttpRequest request) {
return request.getHeaders().getOrEmpty("Authorization").get(0);
}
private boolean isAuthMissing(ServerHttpRequest request) {
log.info("inside auth missing");
return !request.getHeaders().containsKey("Authorization");
}
private void populateRequestWithHeaders(ServerWebExchange exchange, AuthenticationResponse authRes) {
log.info("About to mutate the request->{}",exchange);
exchange.getRequest().mutate()
.header("id",Integer.toString(authRes.getUserId()))
.build();
}

假装接口

@Autowired
private AuthenticationFeign auth;
public AuthenticationResponse isTokenValid(String token) {
return auth.getValidity(token);
}

我看不清楚。但问题是:在过滤器管道中不能进行阻塞调用。电流无功冲击。就像这样。如果你愿意,你可以使用WebClient的.then()方法。你应该使用web客户端。因为它是反应性的

这个链接可以帮助你:https://github.com/spring-cloud/spring-cloud-gateway/issues/980

有很长一段时间,但我想给答案。我希望,这对你有帮助,请回复,它是否有效。

相关内容

  • 没有找到相关文章

最新更新