赞
踩
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-gateway</artifactId>
- </dependency>
-
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
- </dependency>
- </dependencies>
- server:
- port: 10010
-
- spring:
- application:
- name: api-gateway
- cloud:
- gateway:
- routes:
- - id: user-service-route #路由id
- #uri: http://127.0.0.1:9091 #代理的服务地址
- uri: lb://user-service #lb 从Eureka中获取具体的服务
- predicates: #路由断言:可以匹配映射路径
- - Path=/user/**
-
- eureka:
- client:
- service-url:
- defaultZone: http://127.0.0.1:10086/eureka,http://127.0.0.1:10087/eureka
- instance:
- prefer-ip-address: true
- ip-address: 127.0.0.1
- @SpringBootApplication
- @EnableDiscoveryClient//启用Eureka注册中心
- public class GatewayApplication {
-
- public static void main(String[] args) {
- SpringApplication.run(GatewayApplication.class,args);
- }
- }
全局和局部如下
- spring:
- application:
- name: api-gateway
- cloud:
- gateway:
- routes:
- - id: user-service-route #路由id
- #uri: http://127.0.0.1:9091 #代理的服务地址
- uri: lb://user-service #lb 从Eureka中获取具体的服务
- predicates: #路由断言:可以匹配映射路径
- - Path=/**
- filters: #局部路由过滤器
- - PrefixPath=/user #添加请求路径的前缀
- default-filters: #全局路由过滤器
- - AddResponseHeader=X-Reponse-Foo,Bar
通过观察gateway服务自带的默认路由器,发现都是继承一个 AbstractGatewayFilterFactory的类,如下图所示
所以,我们的自定义局部过滤器也如此实现
- spring:
- application:
- name: api-gateway
- cloud:
- gateway:
- routes:
- - id: user-service-route #路由id
- #uri: http://127.0.0.1:9091 #代理的服务地址
- uri: lb://user-service #lb 从Eureka中获取具体的服务
- predicates: #路由断言:可以匹配映射路径
- - Path=/**
- filters: #局部路由过滤器
- - PrefixPath=/user #添加请求路径的前缀
- - MyParam=name #添加自定义过滤器
- default-filters: #全局路由过滤器
- - AddResponseHeader=X-Reponse-Foo,Bar
- package com.wayne.filter;
-
- import org.springframework.cloud.gateway.filter.GatewayFilter;
- import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
- import org.springframework.http.server.reactive.ServerHttpRequest;
- import org.springframework.stereotype.Component;
-
- import java.util.Arrays;
- import java.util.List;
-
- @Component
- public class MyParamGatewayFilterFactory extends AbstractGatewayFilterFactory<MyParamGatewayFilterFactory.Config> {
-
- final static String PARAM_NAME = "param";
-
- public MyParamGatewayFilterFactory(){
- super(Config.class);
- }
-
- public List<String> shortcutFieldOrder(){
- return Arrays.asList(PARAM_NAME);
- }
-
- @Override
- public GatewayFilter apply(Config config) {
- return ((exchange, chain) -> {
- ServerHttpRequest request = exchange.getRequest();
- if(request.getQueryParams().containsKey(config.param)){
- request.getQueryParams().get(config.param)
- .forEach(value -> System.out.printf("----局部过滤器---%s = %s---\n",config.param,value));
- }
-
- return chain.filter(exchange);
- });
- }
-
- public static class Config{
- private String param;
-
- public Config(){
-
- }
-
- public String getParam() {
- return param;
- }
-
- public void setParam(String param) {
- this.param = param;
- }
- }
- }
全局过滤器直接用即可,实现GlobalFilter接口。如果对过滤器的顺序有要求,可同时实现 Ordered接口
- package com.wayne.filter;
-
- import org.apache.commons.lang.StringUtils;
- import org.springframework.cloud.gateway.filter.GatewayFilterChain;
- import org.springframework.cloud.gateway.filter.GlobalFilter;
- import org.springframework.core.Ordered;
- import org.springframework.http.HttpStatus;
- import org.springframework.stereotype.Component;
- import org.springframework.web.server.ServerWebExchange;
- import reactor.core.publisher.Mono;
-
- @Component
- public class TokenCheckFilter implements GlobalFilter, Ordered {
- @Override
- public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
-
- System.out.println("--------这个是全局过滤器MyGlobalFilter-----");
- String token = exchange.getRequest().getQueryParams().getFirst("token");
-
- if(StringUtils.isBlank(token)){
- exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
- return exchange.getResponse().setComplete();
- }
-
- return chain.filter(exchange);
- }
-
- @Override
- public int getOrder() {
- //值越小,越优先
- return 1;
- }
- }
浏览器输入 http://127.0.0.1:10010/1?name=Wyane&token=abd
出现如下信息
控制台
如果没有加token,则浏览器出现401错误
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。