【问题标题】:Throwing Custom Runtime exception in Spring Cloud Gateway Filters在 Spring Cloud Gateway 过滤器中引发自定义运行时异常
【发布时间】:2018-11-01 23:35:40
【问题描述】:

我们正在使用带有 Spring Boot 2 和响应式 webflux 模块的 Spring Cloud Gateway。 有一个为其中一条路由添加的身份验证过滤器。 现在,如果我们抛出一个带有特定状态码的 RuntimeException,它实际上并没有启动。 之前这个身份验证检查是 Spring 中 HandlerInterceptor 的一部分,现在我们不能将 web 模块与 webflux 一起使用(来自 Spring 云网关的冲突)。 例如:

@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");
   }
}

目前实际响应总是给出 500 内部服务器错误。 这是从哪里来的?我们可以在这里从过滤器中获取错误吗?

【问题讨论】:

    标签: spring spring-cloud-gateway


    【解决方案1】:

    您可以实现自定义错误处理程序,这里是spring boot document

    或者您可以简单地抛出一个 ResponseStatusException,默认错误处理程序将呈现特定状态。

    【讨论】:

    • 使用自定义 HTTP 状态代码抛出 ResponseStatusException 确实对我有用,但它仍然被 Spring Boot @ControllerAdvice 控制器绕过。
    猜你喜欢
    • 2020-11-04
    • 2018-06-21
    • 2021-07-31
    • 2021-12-29
    • 1970-01-01
    • 2021-02-13
    • 2020-11-14
    • 2021-02-23
    • 2020-04-13
    相关资源
    最近更新 更多