直接从 Spring 网关发送来自前置过滤器的错误响应
Send error response from pre filter directly from Spring Gateway
我正在使用 Spring 网关和 Spring WebFlux 应用程序。在我的例子中,如果在调用任何下游服务之前不满足某些条件,我需要 return HTTP 417
错误。由于某种原因,它没有 returning 错误;事实上它没有 returning 任何东西.. 请求只是挂起。这是我的代码。
@Component
public class GlobalGatewayPreFilter extends AbstractGatewayFilterFactory<GlobalGatewayPreFilter.Config> {
public GlobalGatewayPreFilter() {
super(Config.class);
}
@Override
public GatewayFilter apply(Config config) {
return (exchange, chain) -> exchange
.getPrincipal()
.filter(principal -> principal instanceof UserAuthenticationToken)
.cast(UserAuthenticationToken.class)
.filter(token -> (token.getUser().isVerified()))
.defaultIfEmpty(setErrorResponse(exchange)).flatMap(chain::filter);
}
private ServerWebExchange setErrorResponse(ServerWebExchange exchange) {
exchange.getResponse().setStatusCode(HttpStatus.EXPECTATION_FAILED);
exchange.getResponse().setComplete();
return exchange;
}
public static class Config {
private String name;
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
}
有办法解决这个问题吗?
好的,我可以使用以下方法解决这个问题。我在这里发帖,希望有人能从中受益。
@Component
public class GlobalGatewayPreFilter extends AbstractGatewayFilterFactory<GlobalGatewayPreFilter.Config> {
public GlobalGatewayPreFilter() {
super(Config.class);
}
@Override
public GatewayFilter apply(Config config) {
return (exchange, chain) -> exchange
.getPrincipal()
.filter(principal -> principal instanceof UserAuthenticationToken)
.cast(UserAuthenticationToken.class)
.filter(token -> (token.getUser().isVerified()))
.switchIfEmpty(Mono.defer(() -> setErrorResponse(exchange.getResponse()).setComplete().then(Mono.empty())))
.flatMap(chain::filter);
}
private ServerHttpResponse setErrorResponse(ServerHttpResponse serverHttpResponse) {
serverHttpResponse.setStatusCode(HttpStatus.EXPECTATION_FAILED);
return serverHttpResponse;
}
public static class Config {
private String name;
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
}
我正在使用 Spring 网关和 Spring WebFlux 应用程序。在我的例子中,如果在调用任何下游服务之前不满足某些条件,我需要 return HTTP 417
错误。由于某种原因,它没有 returning 错误;事实上它没有 returning 任何东西.. 请求只是挂起。这是我的代码。
@Component
public class GlobalGatewayPreFilter extends AbstractGatewayFilterFactory<GlobalGatewayPreFilter.Config> {
public GlobalGatewayPreFilter() {
super(Config.class);
}
@Override
public GatewayFilter apply(Config config) {
return (exchange, chain) -> exchange
.getPrincipal()
.filter(principal -> principal instanceof UserAuthenticationToken)
.cast(UserAuthenticationToken.class)
.filter(token -> (token.getUser().isVerified()))
.defaultIfEmpty(setErrorResponse(exchange)).flatMap(chain::filter);
}
private ServerWebExchange setErrorResponse(ServerWebExchange exchange) {
exchange.getResponse().setStatusCode(HttpStatus.EXPECTATION_FAILED);
exchange.getResponse().setComplete();
return exchange;
}
public static class Config {
private String name;
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
}
有办法解决这个问题吗?
好的,我可以使用以下方法解决这个问题。我在这里发帖,希望有人能从中受益。
@Component
public class GlobalGatewayPreFilter extends AbstractGatewayFilterFactory<GlobalGatewayPreFilter.Config> {
public GlobalGatewayPreFilter() {
super(Config.class);
}
@Override
public GatewayFilter apply(Config config) {
return (exchange, chain) -> exchange
.getPrincipal()
.filter(principal -> principal instanceof UserAuthenticationToken)
.cast(UserAuthenticationToken.class)
.filter(token -> (token.getUser().isVerified()))
.switchIfEmpty(Mono.defer(() -> setErrorResponse(exchange.getResponse()).setComplete().then(Mono.empty())))
.flatMap(chain::filter);
}
private ServerHttpResponse setErrorResponse(ServerHttpResponse serverHttpResponse) {
serverHttpResponse.setStatusCode(HttpStatus.EXPECTATION_FAILED);
return serverHttpResponse;
}
public static class Config {
private String name;
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
}