Spring Cloud Gateway GlobalFilter全局过滤失效问题
Gateway GlobalFilter全局过滤失效问题本地环境:SpringBoot_2.1.4SpringCould_Greenwich.SR1网关的yml配置很简单,主要是玩一下全局过滤的,所以就没有配置路由啥的server:port: 9003eureka:client:serviceUrl:defaultZone: "http://localhost:9001/eureka"@Compo
·
Gateway GlobalFilter全局过滤失效问题
本地环境:
SpringBoot_2.1.4
SpringCould_Greenwich.SR1
网关的yml配置很简单,主要是玩一下全局过滤的,所以就没有配置路由啥的
server:
port: 9003
eureka:
client:
serviceUrl:
defaultZone: "http://localhost:9001/eureka"
@Component
public class AuthFilter implements GlobalFilter, Ordered {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
String token = exchange.getRequest().getHeaders().getFirst("token");
if ("token".equals(token)) {
return chain.filter(exchange);
}
ServerHttpResponse response = exchange.getResponse();
JSONObject jsonObject = new JSONObject();
jsonObject.put("code","401");
jsonObject.put("message","非法请求");
byte[] datas = JSON.toJSONString(jsonObject).getBytes(StandardCharsets.UTF_8);
DataBuffer buffer = response.bufferFactory().wrap(datas);
response.setStatusCode(HttpStatus.UNAUTHORIZED);
response.getHeaders().add("Content-Type", "application/json;charset=UTF-8");
return response.writeWith(Mono.just(buffer));
}
@Override
public int getOrder() {
return 0;
}
}
还有一个简单的controller验证拦截
@RestController
@RequestMapping("/test")
@Slf4j
public class TestController {
@GetMapping("/fcy")
public String test(){
log.error("ok");
return "okgatway";
}
}
然后调用一下后发现直接输出了"okgatway"没有拦下来
作为资深的面向百度开发者 百度了半天发现好像没结果
最后发现还是需要匹配到路由才会触发
server:
port: 9003
eureka:
client:
serviceUrl:
defaultZone: "http://localhost:9001/eureka"
spring:
cloud:
gateway:
routes:
- id: first_route
uri: https://127.0.0.1:9002
predicates:
- Path=/test/**
重新调用后发现ok了
网上相关GlobalFilter文章不多,也是自己试了一天瞎猫碰死耗子碰出来的
更多推荐
已为社区贡献1条内容
所有评论(0)