简介
过滤器开发
1.gateway 通过 GlobalFilter 编写全局过滤器, 通过Ordered 编写过滤器的优先级(越大,优先级越低)
package com.czxy.filter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.nio.charset.StandardCharsets;
/**
* @author 桐叔
* @email liangtong@itcast.cn
*/
@Component
public class DemoFilter implements GlobalFilter, Ordered {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//1 获得请求路径
ServerHttpRequest request = exchange.getRequest();
String path = request.getURI().getPath();
System.out.println(path);
// 2 处理异常
if("/service-consumer/feign/echo/abc".equals(path)) {
ServerHttpResponse response = exchange.getResponse();
response.setStatusCode(HttpStatus.UNAUTHORIZED);
response.getHeaders().add("Content-Type","application/json;charset=UTF-8");
DataBuffer wrap = response.bufferFactory().wrap("没有权限".getBytes(StandardCharsets.UTF_8));
return exchange.getResponse().writeWith(Flux.just(wrap));
}
// 3 放行
return chain.filter(exchange).then(Mono.fromRunnable(() -> {
System.out.println("最后处理");
}));
}
@Override
public int getOrder() {
return 1;
}
}
默认过滤器
默认路由的配置,将应用到所有路由上:
https://docs.spring.io/spring-cloud-gateway/docs/2.2.9.RELEASE/reference/html/#default-filters
默认路由配置:设置请求头
spring:
cloud:
gateway:
default-filters:
- AddRequestHeader=czxy,xxxx
修改TestFeignController打印请求头
package com.czxy.controller;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.czxy.feign.EchoFeign;
import org.checkerframework.checker.units.qual.C;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author 桐叔
* @email liangtong@itcast.cn
*/
@RestController
@RequestMapping("/feign")
public class TestFeignController {
@Resource
private EchoFeign echoFeign;
@Resource
private HttpServletRequest request;
@Resource
private HttpServletResponse response;
// @GetMapping("/echo/{str}")
// @PostMapping("/echo/{str}")
@RequestMapping(value = "/echo/{str}", method = {RequestMethod.GET, RequestMethod.POST})
@SentinelResource("/feign/echo")
public String echo(@PathVariable String str) {
// 打印cookie
Cookie[] cookies = request.getCookies();
if(cookies != null) {
for (Cookie cookie : cookies) {
System.out.println(cookie.getName() + " : " + cookie.getValue());
}
}
// 打印请求头
System.out.println("head:" + request.getHeader("czxy"));
// 设置cookie
Cookie cookie = new Cookie("czxy","jack");
response.addCookie(cookie);
return echoFeign.echo(str);
}
}