From 9e98a4a6fb9baa4be70933827dd92d23d921b602 Mon Sep 17 00:00:00 2001 From: hongqiaowei Date: Mon, 7 Dec 2020 19:15:08 +0800 Subject: [PATCH] feat: auth switch --- src/main/java/we/filter/FizzGatewayFilter.java | 12 ++++++------ src/main/java/we/plugin/auth/ApiConfigService.java | 10 +++++----- src/main/java/we/proxy/FizzWebClient.java | 4 +++- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/main/java/we/filter/FizzGatewayFilter.java b/src/main/java/we/filter/FizzGatewayFilter.java index 537b78d..74a6012 100644 --- a/src/main/java/we/filter/FizzGatewayFilter.java +++ b/src/main/java/we/filter/FizzGatewayFilter.java @@ -71,15 +71,15 @@ public class FizzGatewayFilter implements WebFilter { @Resource private ConfigLoader configLoader; - @NacosValue(value = "${auth.compatible-wh:false}", autoRefreshed = true) - @Value("${auth.compatible-wh:false}") - private boolean compatibleWh; + @NacosValue(value = "${need-auth:false}", autoRefreshed = true) + @Value("${need-auth:false}") + private boolean needAuth; @Override public Mono filter(ServerWebExchange exchange, WebFilterChain chain) { - if (ApiConfig.Type.SERVICE_ARRANGE == WebUtils.getApiConfigType(exchange) || compatibleWh) { - } else { + String serviceId = WebUtils.getServiceId(exchange); + if (serviceId == null || (ApiConfig.Type.SERVICE_ARRANGE != WebUtils.getApiConfigType(exchange) && needAuth) ) { return chain.filter(exchange); } @@ -87,7 +87,7 @@ public class FizzGatewayFilter implements WebFilter { ServerHttpRequest request = exchange.getRequest(); ServerHttpResponse serverHttpResponse = exchange.getResponse(); - String path = WebUtils.getPathPrefix(exchange) + WebUtils.getServiceId(exchange) + WebUtils.getReqPath(exchange); + String path = WebUtils.getPathPrefix(exchange) + serviceId + WebUtils.getReqPath(exchange); String method = request.getMethodValue(); AggregateResource aggregateResource = configLoader.matchAggregateResource(method, path); if (aggregateResource == null) { diff --git a/src/main/java/we/plugin/auth/ApiConfigService.java b/src/main/java/we/plugin/auth/ApiConfigService.java index 879d294..7cdb696 100644 --- a/src/main/java/we/plugin/auth/ApiConfigService.java +++ b/src/main/java/we/plugin/auth/ApiConfigService.java @@ -113,9 +113,9 @@ public class ApiConfigService { } } - @NacosValue(value = "${auth.compatible-wh:false}", autoRefreshed = true) - @Value("${auth.compatible-wh:false}") - private boolean compatibleWh; + @NacosValue(value = "${need-auth:false}", autoRefreshed = true) + @Value("${need-auth:false}") + private boolean needAuth; @Resource(name = AggregateRedisConfig.AGGREGATE_REACTIVE_REDIS_TEMPLATE) private ReactiveStringRedisTemplate rt; @@ -293,7 +293,7 @@ public class ApiConfigService { } ServiceConfig sc = serviceConfigMap.get(service); if (sc == null) { - if (compatibleWh) { + if (!needAuth) { return Mono.just(Access.YES); } else { return logWarnAndResult(service + Constants.Symbol.BLANK + Access.NO_SERVICE_CONFIG.getReason(), Access.NO_SERVICE_CONFIG); @@ -309,7 +309,7 @@ public class ApiConfigService { } ApiConfig ac = ac0; if (ac == null) { - if (compatibleWh) { + if (needAuth) { return Mono.just(Access.YES); } else { return logWarnAndResult(api + " no api config", Access.NO_API_CONFIG); diff --git a/src/main/java/we/proxy/FizzWebClient.java b/src/main/java/we/proxy/FizzWebClient.java index bcecd60..c9e8cac 100644 --- a/src/main/java/we/proxy/FizzWebClient.java +++ b/src/main/java/we/proxy/FizzWebClient.java @@ -18,6 +18,7 @@ package we.proxy; import com.alibaba.nacos.api.config.annotation.NacosValue; +import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; @@ -224,7 +225,8 @@ public class FizzWebClient { } private boolean isService(String s) { - if (s.indexOf(Constants.Symbol.DOT) > 0 || s.equals(localhost)) { + if (StringUtils.indexOfAny(s, Constants.Symbol.DOT, Constants.Symbol.COLON) > 0 + || StringUtils.indexOfIgnoreCase(s, localhost) > 0) { return false; } else { return true;