【问题标题】:Forbid Unauthenticated requests in Spring Cloud Gateway在 Spring Cloud Gateway 中禁止未经身份验证的请求
【发布时间】:2019-05-20 01:41:07
【问题描述】:

我已经在 Spring Cloud Gateway 中实现了自定义预过滤器,它允许经过身份验证的请求通过下游流程。我想要的是,如果请求未经身份验证,则返回 401 UNAUTHORIZE 状态响应并停止下游处理。能不能实现这个spring cloud gateway。

请帮忙。

我的过滤代码如下

public class ValidUserFilter implements GatewayFilterFactory {

  @Override
  public GatewayFilter apply(Object config) {
    return (exchange, chain) -> {
      ServerHttpRequest request = exchange.getRequest();

      if (isValidRequest(request)) {
        // Allow processing
      } else {
      // set UNAUTHORIZED 401 response and stop the processing
      }

      return chain.filter(exchange);
    };
  }
}

配置如下:

  - id: myroute
            uri: http://localhost:8080/bar
            predicates:
            - Path=/foo/**
            filters:
            - ValidUserFilter

【问题讨论】:

    标签: spring spring-boot spring-cloud spring-cloud-gateway


    【解决方案1】:

    SetStatusGatewayFilterFactory中的代码

    // set UNAUTHORIZED 401 response and stop the processing
    exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
    return exchange.getResponse().setComplete();
    

    【讨论】:

    猜你喜欢
    • 2021-02-05
    • 2017-05-10
    • 1970-01-01
    • 1970-01-01
    • 2017-03-28
    • 2021-07-05
    • 2019-08-30
    • 1970-01-01
    • 2018-10-19
    相关资源
    最近更新 更多