前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >聊聊spring cloud gateway的SetStatusGatewayFilter

聊聊spring cloud gateway的SetStatusGatewayFilter

作者头像
code4it
发布2018-09-17 16:50:59
6500
发布2018-09-17 16:50:59
举报
文章被收录于专栏:码匠的流水账码匠的流水账

本文主要研究下spring cloud gateway的SetStatusGatewayFilter

GatewayAutoConfiguration

spring-cloud-gateway-core-2.0.0.RC2-sources.jar!/org/springframework/cloud/gateway/config/GatewayAutoConfiguration.java

代码语言:javascript
复制
@Configuration
@ConditionalOnProperty(name = "spring.cloud.gateway.enabled", matchIfMissing = true)
@EnableConfigurationProperties
@AutoConfigureBefore(HttpHandlerAutoConfiguration.class)
@AutoConfigureAfter({GatewayLoadBalancerClientAutoConfiguration.class, GatewayClassPathWarningAutoConfiguration.class})
@ConditionalOnClass(DispatcherHandler.class)
public class GatewayAutoConfiguration {
    //......
    @Bean
    public SetStatusGatewayFilterFactory setStatusGatewayFilterFactory() {
        return new SetStatusGatewayFilterFactory();
    }
    //......
}

默认创建SetStatusGatewayFilterFactory

SetStatusGatewayFilterFactory

spring-cloud-gateway-core-2.0.0.RC2-sources.jar!/org/springframework/cloud/gateway/filter/factory/SetStatusGatewayFilterFactory.java

代码语言:javascript
复制
public class SetStatusGatewayFilterFactory extends AbstractGatewayFilterFactory<SetStatusGatewayFilterFactory.Config> {

    public static final String STATUS_KEY = "status";

    public SetStatusGatewayFilterFactory() {
        super(Config.class);
    }

    @Override
    public List<String> shortcutFieldOrder() {
        return Arrays.asList(STATUS_KEY);
    }

    @Override
    public GatewayFilter apply(Config config) {
        final HttpStatus status = ServerWebExchangeUtils.parse(config.status);
        final Integer intStatus;
        if (status == null) {
            intStatus = Integer.parseInt(config.status);
        } else {
            intStatus = null;
        }
        return (exchange, chain) -> {

            // option 1 (runs in filter order)
            /*exchange.getResponse().beforeCommit(() -> {
                exchange.getResponse().setStatusCode(finalStatus);
                return Mono.empty();
            });
            return chain.filter(exchange);*/

            // option 2 (runs in reverse filter order)
            return chain.filter(exchange).then(Mono.fromRunnable(() -> {
                // check not really needed, since it is guarded in setStatusCode,
                // but it's a good example
                if (!exchange.getResponse().isCommitted()) {
                    if (status != null) { // standard status
                        setResponseStatus(exchange, status);
                    } else if (intStatus != null && exchange.getResponse() instanceof AbstractServerHttpResponse) { //non-standard
                        ((AbstractServerHttpResponse)exchange.getResponse()).setStatusCodeValue(intStatus);
                    }
                }
            }));
        };
    }

    public static class Config {
        //TODO: relaxed HttpStatus converter
        private String status;

        public String getStatus() {
            return status;
        }

        public void setStatus(String status) {
            this.status = status;
        }
    }

}
  • 可以看到,就是简单地根据status来设置response的status
  • 注意这里有两个option,源码注释掉了option1,改为使用option2

实例

代码语言:javascript
复制
spring:
  cloud:
    gateway:
      routes:
      - id: setstatusstring_route
        uri: http://example.org
        predicates:
        - Path=/foo/**
        filters:
        - SetStatus=BAD_REQUEST
      - id: setstatusint_route
        uri: http://example.org
        predicates:
        - Path=/name/**
        filters:
        - SetStatus=401

小结

SetStatusGatewayFilter就是简单地设置response,使用option1方式的话,是在response commit之前设置的,而且一旦设置就直接return,不再继续走filter了,因而SetStatus可以生效;使用option2的话,是逆filter顺序执行的,会先判断是否commit,未commit才设置,实际不生效。

doc

  • 112.17 SetStatus GatewayFilter Factory
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-06-15,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 码匠的流水账 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • GatewayAutoConfiguration
  • SetStatusGatewayFilterFactory
  • 实例
  • 小结
  • doc
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档