网关filter、自定义filter和网关路由查看

1.常用的Filter

- AddRequestHeader=X-Request-red, blue		增加请求头的filter
- AddRequestParameter=red, blue			增加请求参数的filter
- AddResponseHeader=X-Response-Red, AAA		增加响应头filter
- PrefixPath=/category		                增加前缀的filter
- StripPrefix=2		                        去掉前缀的filter

官网:https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.2.3.RELEASE/reference/html/#gatewayfilter-factories

2.使用自定义filter

@Configuration
public class CustomGlobalFilter implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        log.info("进入自定义的filter");
        if(exchange.getRequest().getQueryParams().get("username")!=null){
            log.info("用户身份信息合法,放行请求继续执行!!!");
            return chain.filter(exchange);
        }
        log.info("非法用户,拒绝访问!!!");
       return exchange.getResponse().setComplete();
    }

    //返回值表示自定义filter执行的顺序,自然数排序,-1表示在所有filter之前最先执行的filter
    @Override
    public int getOrder() {
        return -1;
    }
}

3.查看网关路由规则列表

gateway提供路由访问规则列表的web界面,但是默认是关闭的,如果想要查看服务路由规则可以在配置文件中开启

yml中配置
management:
  endpoints:
    web:
      exposure:
        include: "*"   #开启所有web端点暴露

访问:- http://localhost:port/actuator/gateway/routes
posted @ 2021-10-01 22:55  code-G  阅读(296)  评论(0编辑  收藏  举报