|
|
@@ -0,0 +1,70 @@
|
|
|
+package com.ruoyi.gateway.filter;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.regex.Pattern;
|
|
|
+import org.springframework.cloud.gateway.filter.GatewayFilter;
|
|
|
+import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
|
|
|
+import org.springframework.http.server.reactive.ServerHttpResponse;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.ruoyi.common.core.web.domain.AjaxResult;
|
|
|
+import reactor.core.publisher.Mono;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 黑名单过滤器
|
|
|
+ *
|
|
|
+ * @author ruoyi
|
|
|
+ */
|
|
|
+@Component
|
|
|
+public class BlackListUrlFilter extends AbstractGatewayFilterFactory<BlackListUrlFilter.Config>
|
|
|
+{
|
|
|
+ @Override
|
|
|
+ public GatewayFilter apply(Config config)
|
|
|
+ {
|
|
|
+ return (exchange, chain) -> {
|
|
|
+
|
|
|
+ String url = exchange.getRequest().getURI().getPath();
|
|
|
+ if (config.matchBlacklist(url))
|
|
|
+ {
|
|
|
+ ServerHttpResponse response = exchange.getResponse();
|
|
|
+ return exchange.getResponse().writeWith(
|
|
|
+ Mono.just(response.bufferFactory().wrap(JSON.toJSONBytes(AjaxResult.error("服务拒绝访问")))));
|
|
|
+ }
|
|
|
+
|
|
|
+ return chain.filter(exchange);
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
+ public BlackListUrlFilter()
|
|
|
+ {
|
|
|
+ super(Config.class);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static class Config
|
|
|
+ {
|
|
|
+ private List<String> blacklistUrl;
|
|
|
+
|
|
|
+ private List<Pattern> blacklistUrlPattern = new ArrayList<>();
|
|
|
+
|
|
|
+ public boolean matchBlacklist(String url)
|
|
|
+ {
|
|
|
+ return blacklistUrlPattern.isEmpty() ? false : blacklistUrlPattern.stream().filter(p -> p.matcher(url).find()).findAny().isPresent();
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<String> getBlacklistUrl()
|
|
|
+ {
|
|
|
+ return blacklistUrl;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setBlacklistUrl(List<String> blacklistUrl)
|
|
|
+ {
|
|
|
+ this.blacklistUrl = blacklistUrl;
|
|
|
+ this.blacklistUrlPattern.clear();
|
|
|
+ this.blacklistUrl.forEach(url -> {
|
|
|
+ this.blacklistUrlPattern.add(Pattern.compile(url.replaceAll("\\*\\*", "(.*?)"), Pattern.CASE_INSENSITIVE));
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|