自定义Filter

说明

  • 本文是说明如何进行 org.springframework.web.server.WebFliter 的扩展。

跨域支持

  • 新增 org.dromara.soul.bootstrap.cors.CrossFilter 实现 WebFilter。

    1. public class CrossFilter implements WebFilter {
    2. private static final String ALLOWED_HEADERS = "x-requested-with, authorization, Content-Type, Authorization, credential, X-XSRF-TOKEN,token,username,client";
    3. private static final String ALLOWED_METHODS = "*";
    4. private static final String ALLOWED_ORIGIN = "*";
    5. private static final String ALLOWED_EXPOSE = "*";
    6. private static final String MAX_AGE = "18000";
    7. @Override
    8. @SuppressWarnings("all")
    9. public Mono<Void> filter(final ServerWebExchange exchange, final WebFilterChain chain) {
    10. ServerHttpRequest request = exchange.getRequest();
    11. if (CorsUtils.isCorsRequest(request)) {
    12. ServerHttpResponse response = exchange.getResponse();
    13. HttpHeaders headers = response.getHeaders();
    14. headers.add("Access-Control-Allow-Origin", ALLOWED_ORIGIN);
    15. headers.add("Access-Control-Allow-Methods", ALLOWED_METHODS);
    16. headers.add("Access-Control-Max-Age", MAX_AGE);
    17. headers.add("Access-Control-Allow-Headers", ALLOWED_HEADERS);
    18. headers.add("Access-Control-Expose-Headers", ALLOWED_EXPOSE);
    19. headers.add("Access-Control-Allow-Credentials", "true");
    20. if (request.getMethod() == HttpMethod.OPTIONS) {
    21. response.setStatusCode(HttpStatus.OK);
    22. return Mono.empty();
    23. }
    24. }
    25. return chain.filter(exchange);
    26. }
    27. }
  • 将 CrossFilter 注册成为 spring的bean,完事。

网关过滤 springboot健康检查

  • 注意顺序,使用 @Order 注解
  1. @Component
  2. @Order(-99)
  3. public final class HealthFilter implements WebFilter {
  4. private static final String[] FILTER_TAG = {"/actuator/health", "/health_check"};
  5. @Override
  6. public Mono<Void> filter(@Nullable final ServerWebExchange exchange, @Nullable final WebFilterChain chain) {
  7. ServerHttpRequest request = Objects.requireNonNull(exchange).getRequest();
  8. String urlPath = request.getURI().getPath();
  9. for (String check : FILTER_TAG) {
  10. if (check.equals(urlPath)) {
  11. String result = JsonUtils.toJson(new Health.Builder().up().build());
  12. DataBuffer dataBuffer = exchange.getResponse().bufferFactory().wrap(result.getBytes());
  13. return exchange.getResponse().writeWith(Mono.just(dataBuffer));
  14. }
  15. }
  16. return Objects.requireNonNull(chain).filter(exchange);
  17. }
  18. }

继承 org.dromara.soul.web.filter.AbstractWebFilter

  • 新增一个类,继承它。

  • 实现它的2个方法。

  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 方法返回 Monotrue 表示通过,反之则不通过,不通过的时候,会调用 doDenyResponse输出相关信息到前端。