自定义Filter

说明

  • 本文介绍如何对 org.springframework.web.server.WebFliter 进行扩展。

跨域支持

  • 新增 org.apache.shenyu.web.filter.CrossFilter 实现 WebFilter

    ``` public class CrossFilter implements WebFilter {

  1. private static final String ALLOWED_HEADERS = "x-requested-with, authorization, Content-Type, Authorization, credential, X-XSRF-TOKEN,token,username,client";
  2. private static final String ALLOWED_METHODS = "*";
  3. private static final String ALLOWED_ORIGIN = "*";
  4. private static final String ALLOWED_EXPOSE = "*";
  5. private static final String MAX_AGE = "18000";
  6. @Override
  7. @SuppressWarnings("all")
  8. public Mono<Void> filter(final ServerWebExchange exchange, final WebFilterChain chain) {
  9. ServerHttpRequest request = exchange.getRequest();
  10. if (CorsUtils.isCorsRequest(request)) {
  11. ServerHttpResponse response = exchange.getResponse();
  12. HttpHeaders headers = response.getHeaders();
  13. headers.add("Access-Control-Allow-Origin", ALLOWED_ORIGIN);
  14. headers.add("Access-Control-Allow-Methods", ALLOWED_METHODS);
  15. headers.add("Access-Control-Max-Age", MAX_AGE);
  16. headers.add("Access-Control-Allow-Headers", ALLOWED_HEADERS);
  17. headers.add("Access-Control-Expose-Headers", ALLOWED_EXPOSE);
  18. headers.add("Access-Control-Allow-Credentials", "true");
  19. if (request.getMethod() == HttpMethod.OPTIONS) {
  20. response.setStatusCode(HttpStatus.OK);
  21. return Mono.empty();
  22. }
  23. }
  24. return chain.filter(exchange);
  25. }
  26. }
  27. ```
  1. * `CrossFilter` 注册成为 `Spring``bean`
  2. ## 网关过滤 springboot健康检查
  3. * 注意顺序,使用 `@Order` 注解
  4. ```java
  5. @Component
  6. @Order(-99)
  7. public final class HealthFilter implements WebFilter {
  8. private static final String[] FILTER_TAG = {"/actuator/health", "/health_check"};
  9. @Override
  10. public Mono<Void> filter(@Nullable final ServerWebExchange exchange, @Nullable final WebFilterChain chain) {
  11. ServerHttpRequest request = Objects.requireNonNull(exchange).getRequest();
  12. String urlPath = request.getURI().getPath();
  13. for (String check : FILTER_TAG) {
  14. if (check.equals(urlPath)) {
  15. String result = JsonUtils.toJson(new Health.Builder().up().build());
  16. DataBuffer dataBuffer = exchange.getResponse().bufferFactory().wrap(result.getBytes());
  17. return exchange.getResponse().writeWith(Mono.just(dataBuffer));
  18. }
  19. }
  20. return Objects.requireNonNull(chain).filter(exchange);
  21. }
  22. }

继承 org.apache.shenyu.web.filter.AbstractWebFilter

  • 新增一个类继承AbstractWebFilter,并实现它的两个方法。
  1. /**
  2. * this is Template Method ,children Implement your own filtering logic.
  3. *
  4. * @param exchange the current server exchange
  5. * @param chain provides a way to delegate to the next filter
  6. * @return {@code Mono<Boolean>} result:TRUE (is pass),and flow next filter;FALSE (is not pass) execute doDenyResponse(ServerWebExchange exchange)
  7. */
  8. protected abstract Mono<Boolean> doFilter(ServerWebExchange exchange, WebFilterChain chain);
  9. /**
  10. * this is Template Method ,children Implement your own And response client.
  11. *
  12. * @param exchange the current server exchange.
  13. * @return {@code Mono<Void>} response msg.
  14. */
  15. protected abstract Mono<Void> doDenyResponse(ServerWebExchange exchange);
  • doFilter 方法返回 Mono<true> 表示通过,反之则不通过,不通过的时候,会调用 doDenyResponse输出相关信息到前端。