content: package com.xzll.gateway.filter;
  
  import com.xzll.gateway.config.nacos.IgnoreUrlsConfig;
  import lombok.extern.slf4j.Slf4j;
  import org.springframework.core.annotation.Order;
  import org.springframework.http.server.reactive.ServerHttpRequest;
  import org.springframework.stereotype.Component;
  import org.springframework.util.AntPathMatcher;
  import org.springframework.util.PathMatcher;
  import org.springframework.web.server.ServerWebExchange;
  import org.springframework.web.server.WebFilter;
  import org.springframework.web.server.WebFilterChain;
  import reactor.core.publisher.Mono;
  
  import javax.annotation.Resource;
  import java.net.URI;
  import java.util.List;
  
  /**
   * @Author: hzz
   * @Date: 2024/6/10 09:55:49
   * @Description: 移除白名单url的 Authorization信息
   */
  @Order(5)
  @Slf4j
  @Component
  public class IgnoreUrlsRemoveJwtFilter implements WebFilter {
      @Resource
      private IgnoreUrlsConfig ignoreUrlsConfig;
      @Override
      public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
          ServerHttpRequest request = exchange.getRequest();
          URI uri = request.getURI();
          PathMatcher pathMatcher = new AntPathMatcher();
          //白名单路径移除JWT请求头
          List<String> ignoreUrls = ignoreUrlsConfig.getUrls();
          for (String ignoreUrl : ignoreUrls) {
              if (pathMatcher.match(ignoreUrl, uri.getPath())) {
                  request = exchange.getRequest().mutate().header("Authorization", "").build();
                  exchange = exchange.mutate().request(request).build();
                  return chain.filter(exchange);
              }
          }
          return chain.filter(exchange);
      }
  }
  