We are using Spring Cloud Gateway with Spring Boot 2 and reactive webflux module. There is an Authentication Filter which is added for one of the route. Now if we throw a RuntimeException with a particular status code, it is really not picking up. Earlier this auth check was part of the HandlerInterceptor in Spring, now we cannot use the web module along with webflux(Conflict from Spring cloud gateway). Ex:
@Override
public GatewayFilter apply(Object config) {
ServerHttpRequest httpRequest = exchange.getRequest();
if(!someUtil.validRequest(httpRequest) {
throw new RuntimeException("Throw 401 Unauthorized with Custom error code and message");
}
}
Currently the actual response always gives 500 internal server error. from where is this coming from? Can we get hold of the errors from Filters here?
You can implement a custom error handler, here is the spring boot document.
Or you can simply throw a ResponseStatusException, the default error handler will render the specific status.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With