feat: auth switch

This commit is contained in:
hongqiaowei
2020-12-07 19:15:08 +08:00
parent 7a7ea4e9b2
commit 9e98a4a6fb
3 changed files with 14 additions and 12 deletions

View File

@@ -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<Void> 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) {

View File

@@ -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);

View File

@@ -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;