官网地址:https://docs.spring.io/spring-cloud-gateway/docs/2.2.9.RELEASE/reference/html/#writing-custom-route-predicate-factories
参考地址:https://blog.csdn.net/myli92/article/details/127328893
import com.ruoyi.common.core.utils.StringUtils; import org.springframework.cloud.gateway.filter.GatewayFilter; import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory; import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.stereotype.Component; /** * 验证码过滤器 * * @author ruoyi */ @Component public class ValidateCodeFilter extends AbstractGatewayFilterFactory<ValidateCodeFilter.Config> { public ValidateCodeFilter() { super(Config.class); } @Override public GatewayFilter apply(Config config) { return (exchange, chain) -> { ServerHttpRequest request = exchange.getRequest(); // 非登录/注册请求或验证码关闭,不处理 if (!StringUtils.containsAnyIgnoreCase(request.getURI().getPath(), config.getValidateUrls()) ) { return chain.filter(exchange); } // TODO 做验证处理逻辑 return chain.filter(exchange); }; } static class Config { private String[] validateUrls ; public String[] getValidateUrls() { return validateUrls; } public void setValidateUrls(String[] validateUrls) { this.validateUrls = validateUrls; } } }
配置:
spring: cloud: gateway: discovery: locator: lowerCaseServiceId: true enabled: true routes: # 认证中心 - id: ruoyi-auth uri: lb://ruoyi-auth predicates: - Path=/auth/** filters: # 验证码处理 - name: ValidateCodeFilter args: validateUrls: - /auth/login - /auth/register - StripPrefix=1
标签:自定义,validateUrls,spring,public,GatewayFilterFactory,import,gateway,cloud From: https://www.cnblogs.com/zcjyzh/p/17451719.html