enhancement: wehotel(#164)
This commit is contained in:
@@ -12,10 +12,10 @@
|
|||||||
|
|
||||||
<groupId>com.fizzgate</groupId>
|
<groupId>com.fizzgate</groupId>
|
||||||
<artifactId>fizz-bootstrap</artifactId>
|
<artifactId>fizz-bootstrap</artifactId>
|
||||||
<version>2.0.0-beta2</version>
|
<version>2.0.0-beta3</version>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<fizz.version>2.0.0-beta2</fizz.version>
|
<fizz.version>2.0.0-beta3</fizz.version>
|
||||||
<java.version>1.8</java.version>
|
<java.version>1.8</java.version>
|
||||||
<spring-framework.version>5.2.14.RELEASE</spring-framework.version>
|
<spring-framework.version>5.2.14.RELEASE</spring-framework.version>
|
||||||
<reactor-bom.version>Dysprosium-SR19</reactor-bom.version>
|
<reactor-bom.version>Dysprosium-SR19</reactor-bom.version>
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>fizz-gateway-community</artifactId>
|
<artifactId>fizz-gateway-community</artifactId>
|
||||||
<groupId>com.fizzgate</groupId>
|
<groupId>com.fizzgate</groupId>
|
||||||
<version>2.0.0-beta2</version>
|
<version>2.0.0-beta3</version>
|
||||||
<relativePath>../pom.xml</relativePath>
|
<relativePath>../pom.xml</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>fizz-gateway-community</artifactId>
|
<artifactId>fizz-gateway-community</artifactId>
|
||||||
<groupId>com.fizzgate</groupId>
|
<groupId>com.fizzgate</groupId>
|
||||||
<version>2.0.0-beta2</version>
|
<version>2.0.0-beta3</version>
|
||||||
<relativePath>../pom.xml</relativePath>
|
<relativePath>../pom.xml</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|||||||
@@ -48,6 +48,8 @@ public class SystemConfig {
|
|||||||
|
|
||||||
public static final String DEFAULT_GATEWAY_TEST_PREFIX = "/_proxytest";
|
public static final String DEFAULT_GATEWAY_TEST_PREFIX = "/_proxytest";
|
||||||
|
|
||||||
|
public static final String DEFAULT_GATEWAY_TEST_PREFIX0 = "/_proxytest/";
|
||||||
|
|
||||||
public String gatewayPrefix = DEFAULT_GATEWAY_PREFIX;
|
public String gatewayPrefix = DEFAULT_GATEWAY_PREFIX;
|
||||||
|
|
||||||
public List<String> appHeaders = Stream.of("fizz-appid").collect(Collectors.toList());
|
public List<String> appHeaders = Stream.of("fizz-appid").collect(Collectors.toList());
|
||||||
@@ -58,6 +60,8 @@ public class SystemConfig {
|
|||||||
|
|
||||||
public List<String> proxySetHeaders = new ArrayList<>();
|
public List<String> proxySetHeaders = new ArrayList<>();
|
||||||
|
|
||||||
|
public boolean aggregateTestAuth = false;
|
||||||
|
|
||||||
|
|
||||||
@NacosValue(value = "${gateway.aggr.proxy_set_headers:}", autoRefreshed = true)
|
@NacosValue(value = "${gateway.aggr.proxy_set_headers:}", autoRefreshed = true)
|
||||||
@Value("${gateway.aggr.proxy_set_headers:}")
|
@Value("${gateway.aggr.proxy_set_headers:}")
|
||||||
@@ -112,6 +116,13 @@ public class SystemConfig {
|
|||||||
log.info("timestamp headers: " + timestampHeaders);
|
log.info("timestamp headers: " + timestampHeaders);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NacosValue(value = "${aggregate-test-auth:false}", autoRefreshed = true)
|
||||||
|
@Value( "${aggregate-test-auth:false}" )
|
||||||
|
public void setAggregateTestAuth(boolean b) {
|
||||||
|
aggregateTestAuth = b;
|
||||||
|
log.info("aggregate test auth: " + aggregateTestAuth);
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: below to X
|
// TODO: below to X
|
||||||
|
|
||||||
@Value("${log.response-body:false}")
|
@Value("${log.response-body:false}")
|
||||||
|
|||||||
@@ -17,17 +17,9 @@
|
|||||||
|
|
||||||
package we.filter;
|
package we.filter;
|
||||||
|
|
||||||
import java.nio.charset.StandardCharsets;
|
import com.alibaba.fastjson.JSON;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Map.Entry;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
|
||||||
|
|
||||||
import com.alibaba.nacos.api.config.annotation.NacosValue;
|
import com.alibaba.nacos.api.config.annotation.NacosValue;
|
||||||
|
import io.netty.buffer.UnpooledByteBufAllocator;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
@@ -44,13 +36,10 @@ import org.springframework.stereotype.Component;
|
|||||||
import org.springframework.web.server.ServerWebExchange;
|
import org.springframework.web.server.ServerWebExchange;
|
||||||
import org.springframework.web.server.WebFilter;
|
import org.springframework.web.server.WebFilter;
|
||||||
import org.springframework.web.server.WebFilterChain;
|
import org.springframework.web.server.WebFilterChain;
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSON;
|
|
||||||
|
|
||||||
import io.netty.buffer.UnpooledByteBufAllocator;
|
|
||||||
import reactor.core.publisher.Flux;
|
import reactor.core.publisher.Flux;
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
import reactor.core.scheduler.Schedulers;
|
import reactor.core.scheduler.Schedulers;
|
||||||
|
import we.config.SystemConfig;
|
||||||
import we.constants.CommonConstants;
|
import we.constants.CommonConstants;
|
||||||
import we.fizz.AggregateResource;
|
import we.fizz.AggregateResource;
|
||||||
import we.fizz.AggregateResult;
|
import we.fizz.AggregateResult;
|
||||||
@@ -63,6 +52,14 @@ import we.util.Constants;
|
|||||||
import we.util.MapUtil;
|
import we.util.MapUtil;
|
||||||
import we.util.WebUtils;
|
import we.util.WebUtils;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Map.Entry;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Francis Dong
|
* @author Francis Dong
|
||||||
*/
|
*/
|
||||||
@@ -81,26 +78,44 @@ public class AggregateFilter implements WebFilter {
|
|||||||
@Value("${need-auth:true}")
|
@Value("${need-auth:true}")
|
||||||
private boolean needAuth;
|
private boolean needAuth;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private SystemConfig systemConfig;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
|
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
|
||||||
|
|
||||||
String serviceId = WebUtils.getBackendService(exchange);
|
String serviceId = WebUtils.getBackendService(exchange);
|
||||||
if ( serviceId == null || (ApiConfig.Type.SERVICE_AGGREGATE != WebUtils.getApiConfigType(exchange) && needAuth) ) {
|
if (serviceId == null) {
|
||||||
return chain.filter(exchange);
|
return chain.filter(exchange);
|
||||||
|
} else {
|
||||||
|
byte act = WebUtils.getApiConfigType(exchange);
|
||||||
|
if (act == ApiConfig.Type.UNDEFINED) {
|
||||||
|
String p = exchange.getRequest().getPath().value();
|
||||||
|
if (StringUtils.startsWith(p, SystemConfig.DEFAULT_GATEWAY_TEST_PREFIX0)) {
|
||||||
|
if (systemConfig.aggregateTestAuth) {
|
||||||
|
return chain.filter(exchange);
|
||||||
|
}
|
||||||
|
} else if (needAuth) {
|
||||||
|
return chain.filter(exchange);
|
||||||
|
}
|
||||||
|
} else if (act != ApiConfig.Type.SERVICE_AGGREGATE) {
|
||||||
|
return chain.filter(exchange);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
long start = System.currentTimeMillis();
|
long start = System.currentTimeMillis();
|
||||||
ServerHttpRequest request = exchange.getRequest();
|
ServerHttpRequest request = exchange.getRequest();
|
||||||
ServerHttpResponse serverHttpResponse = exchange.getResponse();
|
ServerHttpResponse serverHttpResponse = exchange.getResponse();
|
||||||
|
|
||||||
String path = WebUtils.getClientReqPathPrefix(exchange) + serviceId + WebUtils.getBackendPath(exchange);
|
String clientReqPathPrefix = WebUtils.getClientReqPathPrefix(exchange);
|
||||||
|
String path = clientReqPathPrefix + serviceId + WebUtils.getBackendPath(exchange);
|
||||||
String method = request.getMethodValue();
|
String method = request.getMethodValue();
|
||||||
if (HttpMethod.HEAD.matches(method.toUpperCase())) {
|
if (HttpMethod.HEAD.matches(method.toUpperCase())) {
|
||||||
method = HttpMethod.GET.name();
|
method = HttpMethod.GET.name();
|
||||||
}
|
}
|
||||||
AggregateResource aggregateResource = configLoader.matchAggregateResource(method, path);
|
AggregateResource aggregateResource = configLoader.matchAggregateResource(method, path);
|
||||||
if (aggregateResource == null) {
|
if (aggregateResource == null) {
|
||||||
if (WebUtils.getApiConfigType(exchange) == ApiConfig.Type.SERVICE_AGGREGATE) {
|
if (SystemConfig.DEFAULT_GATEWAY_TEST_PREFIX0.equals(clientReqPathPrefix) || WebUtils.getApiConfigType(exchange) == ApiConfig.Type.SERVICE_AGGREGATE) {
|
||||||
return WebUtils.responseError(exchange, HttpStatus.INTERNAL_SERVER_ERROR.value(), "no aggregate resource: " + path);
|
return WebUtils.responseError(exchange, HttpStatus.INTERNAL_SERVER_ERROR.value(), "no aggregate resource: " + path);
|
||||||
} else {
|
} else {
|
||||||
return chain.filter(exchange);
|
return chain.filter(exchange);
|
||||||
|
|||||||
@@ -199,12 +199,8 @@ public class ApiConfigService {
|
|||||||
|
|
||||||
YES (null),
|
YES (null),
|
||||||
|
|
||||||
NO_SERVICE_CONFIG ("no service config"),
|
|
||||||
|
|
||||||
ROUTE_NOT_FOUND ("route not found"),
|
ROUTE_NOT_FOUND ("route not found"),
|
||||||
|
|
||||||
GATEWAY_GROUP_CANT_PROXY_API ("gateway group cant proxy api"),
|
|
||||||
|
|
||||||
APP_NOT_IN_API_LEGAL_APPS ("app not in api legal apps"),
|
APP_NOT_IN_API_LEGAL_APPS ("app not in api legal apps"),
|
||||||
|
|
||||||
IP_NOT_IN_WHITE_LIST ("ip not in white list"),
|
IP_NOT_IN_WHITE_LIST ("ip not in white list"),
|
||||||
@@ -288,29 +284,26 @@ public class ApiConfigService {
|
|||||||
|
|
||||||
private Mono<Object> canAccess(ServerWebExchange exchange, String app, String ip, String timestamp, String sign, String service, HttpMethod method, String path) {
|
private Mono<Object> canAccess(ServerWebExchange exchange, String app, String ip, String timestamp, String sign, String service, HttpMethod method, String path) {
|
||||||
|
|
||||||
ServiceConfig sc = serviceConfigMap.get(service);
|
|
||||||
if (sc == null) {
|
|
||||||
if (!needAuth) {
|
|
||||||
ApiConfig ac = getApiConfig(app, service, method, path);
|
|
||||||
if (ac == null) {
|
|
||||||
return Mono.just(Access.YES);
|
|
||||||
} return Mono.just(ac);
|
|
||||||
} else {
|
|
||||||
return logAndResult(service + Constants.Symbol.BLANK + Access.NO_SERVICE_CONFIG.getReason(), Access.NO_SERVICE_CONFIG);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
String api = ThreadContext.getStringBuilder().append(service).append(Constants.Symbol.BLANK).append(method.name()).append(Constants.Symbol.BLANK + path).toString();
|
String api = ThreadContext.getStringBuilder().append(service).append(Constants.Symbol.BLANK).append(method.name()).append(Constants.Symbol.BLANK + path).toString();
|
||||||
ApiConfig ac = getApiConfig(app, service, method, path);
|
ApiConfig ac = getApiConfig(app, service, method, path);
|
||||||
if (ac == null) {
|
if (ac == null) {
|
||||||
|
if (SystemConfig.DEFAULT_GATEWAY_TEST_PREFIX0.equals(WebUtils.getClientReqPathPrefix(exchange))) {
|
||||||
|
if (systemConfig.aggregateTestAuth) {
|
||||||
|
return logAndResult(api + " no route config", Access.ROUTE_NOT_FOUND);
|
||||||
|
} else {
|
||||||
|
return Mono.just(Access.YES);
|
||||||
|
}
|
||||||
|
}
|
||||||
if (!needAuth) {
|
if (!needAuth) {
|
||||||
return Mono.just(Access.YES);
|
return Mono.just(Access.YES);
|
||||||
} else {
|
} else {
|
||||||
return logAndResult(api + " no route config", Access.ROUTE_NOT_FOUND);
|
return logAndResult(api + " no route config", Access.ROUTE_NOT_FOUND);
|
||||||
}
|
}
|
||||||
} else if (gatewayGroupService.currentGatewayGroupIn(ac.gatewayGroups)) {
|
|
||||||
if (!ac.checkApp) {
|
} else if (!ac.checkApp) {
|
||||||
return allow(api, ac);
|
return allow(api, ac);
|
||||||
} else if (app != null && apiConifg2appsService.contains(ac.id, app)) {
|
|
||||||
|
} else if (app != null) {
|
||||||
if (ac.access == ApiConfig.ALLOW) {
|
if (ac.access == ApiConfig.ALLOW) {
|
||||||
App a = appService.getApp(app);
|
App a = appService.getApp(app);
|
||||||
if (a.useWhiteList && !a.allow(ip)) {
|
if (a.useWhiteList && !a.allow(ip)) {
|
||||||
@@ -319,7 +312,7 @@ public class ApiConfigService {
|
|||||||
if (a.authType == App.AUTH_TYPE.SIGN) {
|
if (a.authType == App.AUTH_TYPE.SIGN) {
|
||||||
return authSign(ac, a, timestamp, sign);
|
return authSign(ac, a, timestamp, sign);
|
||||||
} else if (a.authType == App.AUTH_TYPE.SECRETKEY) {
|
} else if (a.authType == App.AUTH_TYPE.SECRETKEY) {
|
||||||
return authSecretkey(ac , a, sign);
|
return authSecretkey(ac, a, sign);
|
||||||
} else if (customAuth == null) {
|
} else if (customAuth == null) {
|
||||||
return logAndResult(app + " no custom auth", Access.NO_CUSTOM_AUTH);
|
return logAndResult(app + " no custom auth", Access.NO_CUSTOM_AUTH);
|
||||||
} else {
|
} else {
|
||||||
@@ -337,13 +330,10 @@ public class ApiConfigService {
|
|||||||
} else {
|
} else {
|
||||||
return logAndResult("cant access " + api, Access.CANT_ACCESS_SERVICE_API);
|
return logAndResult("cant access " + api, Access.CANT_ACCESS_SERVICE_API);
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
return logAndResult(app + " not in " + api + " legal apps", Access.APP_NOT_IN_API_LEGAL_APPS);
|
return logAndResult(app + " not in " + api + " legal apps", Access.APP_NOT_IN_API_LEGAL_APPS);
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
return logAndResult(gatewayGroupService.currentGatewayGroupSet + " cant proxy " + api, Access.GATEWAY_GROUP_CANT_PROXY_API);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Mono authSign(ApiConfig ac, App a, String timestamp, String sign) {
|
private Mono authSign(ApiConfig ac, App a, String timestamp, String sign) {
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>fizz-gateway-community</artifactId>
|
<artifactId>fizz-gateway-community</artifactId>
|
||||||
<groupId>com.fizzgate</groupId>
|
<groupId>com.fizzgate</groupId>
|
||||||
<version>2.0.0-beta2</version>
|
<version>2.0.0-beta3</version>
|
||||||
<relativePath>../pom.xml</relativePath>
|
<relativePath>../pom.xml</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>fizz-gateway-community</artifactId>
|
<artifactId>fizz-gateway-community</artifactId>
|
||||||
<groupId>com.fizzgate</groupId>
|
<groupId>com.fizzgate</groupId>
|
||||||
<version>2.0.0-beta2</version>
|
<version>2.0.0-beta3</version>
|
||||||
<relativePath>../pom.xml</relativePath>
|
<relativePath>../pom.xml</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|||||||
6
pom.xml
6
pom.xml
@@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<!--<project.root>${project.basedir}</project.root>-->
|
<!--<project.root>${project.basedir}</project.root>-->
|
||||||
<fizz.version>2.0.0-beta2</fizz.version>
|
<fizz.version>2.0.0-beta3</fizz.version>
|
||||||
<!--<java.version>1.8</java.version>-->
|
<!--<java.version>1.8</java.version>-->
|
||||||
<spring-boot.version>2.2.13.RELEASE</spring-boot.version>
|
<spring-boot.version>2.2.13.RELEASE</spring-boot.version>
|
||||||
<spring-framework.version>5.2.14.RELEASE</spring-framework.version>
|
<spring-framework.version>5.2.14.RELEASE</spring-framework.version>
|
||||||
@@ -32,7 +32,7 @@
|
|||||||
<artifactId>fizz-gateway-community</artifactId>
|
<artifactId>fizz-gateway-community</artifactId>
|
||||||
<name>${project.artifactId}</name>
|
<name>${project.artifactId}</name>
|
||||||
<description>fizz gateway community</description>
|
<description>fizz gateway community</description>
|
||||||
<version>2.0.0-beta2</version>
|
<version>2.0.0-beta3</version>
|
||||||
<packaging>pom</packaging>
|
<packaging>pom</packaging>
|
||||||
<modules>
|
<modules>
|
||||||
<module>fizz-common</module>
|
<module>fizz-common</module>
|
||||||
@@ -391,7 +391,7 @@
|
|||||||
<activeByDefault>true</activeByDefault>
|
<activeByDefault>true</activeByDefault>
|
||||||
</activation>
|
</activation>
|
||||||
<properties>
|
<properties>
|
||||||
<fizz.version>2.0.0-beta2</fizz.version>
|
<fizz.version>2.0.0-beta3</fizz.version>
|
||||||
</properties>
|
</properties>
|
||||||
</profile>-->
|
</profile>-->
|
||||||
<!-- mvn clean deploy -P release -->
|
<!-- mvn clean deploy -P release -->
|
||||||
|
|||||||
Reference in New Issue
Block a user