optimize web util
This commit is contained in:
@@ -24,17 +24,14 @@ import java.util.Map;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
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;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.core.io.buffer.DataBufferUtils;
|
||||
import org.springframework.core.io.buffer.NettyDataBufferFactory;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||
import org.springframework.http.server.reactive.ServerHttpResponse;
|
||||
import org.springframework.stereotype.Component;
|
||||
@@ -55,7 +52,6 @@ import we.fizz.ConfigLoader;
|
||||
import we.fizz.Pipeline;
|
||||
import we.fizz.input.Input;
|
||||
import we.flume.clients.log4j2appender.LogService;
|
||||
import we.plugin.auth.ApiConfig;
|
||||
import we.util.Constants;
|
||||
import we.util.MapUtil;
|
||||
import we.util.WebUtils;
|
||||
@@ -81,7 +77,7 @@ public class FizzGatewayFilter implements WebFilter {
|
||||
@Override
|
||||
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
|
||||
|
||||
String serviceId = WebUtils.getServiceId(exchange);
|
||||
String serviceId = WebUtils.getClientService(exchange);
|
||||
// if (serviceId == null || (ApiConfig.Type.SERVICE_ARRANGE != WebUtils.getApiConfigType(exchange) && !needAuth) ) {;
|
||||
// return chain.filter(exchange);
|
||||
// }
|
||||
@@ -93,7 +89,7 @@ public class FizzGatewayFilter implements WebFilter {
|
||||
ServerHttpRequest request = exchange.getRequest();
|
||||
ServerHttpResponse serverHttpResponse = exchange.getResponse();
|
||||
|
||||
String path = WebUtils.getPathPrefix(exchange) + serviceId + WebUtils.getReqPath(exchange);
|
||||
String path = WebUtils.getClientReqPathPrefix(exchange) + serviceId + WebUtils.getClientReqPath(exchange);
|
||||
String method = request.getMethodValue();
|
||||
AggregateResource aggregateResource = configLoader.matchAggregateResource(method, path);
|
||||
if (aggregateResource == null) {
|
||||
|
||||
@@ -92,6 +92,7 @@ public class PreFilter extends ProxyAggrFilter {
|
||||
Mono m;
|
||||
if (authRes instanceof ApiConfig) {
|
||||
ApiConfig ac = (ApiConfig) authRes;
|
||||
afterAuth(exchange, ac);
|
||||
m = executeFixedPluginFilters(exchange);
|
||||
m = m.defaultIfEmpty(ReactorUtils.NULL);
|
||||
if (ac.pluginConfigs == null || ac.pluginConfigs.isEmpty()) {
|
||||
@@ -101,6 +102,7 @@ public class PreFilter extends ProxyAggrFilter {
|
||||
.defaultIfEmpty(ReactorUtils.NULL).flatMap(func(exchange, chain));
|
||||
}
|
||||
} else if (authRes == ApiConfigService.Access.YES) {
|
||||
afterAuth(exchange, null);
|
||||
m = executeFixedPluginFilters(exchange);
|
||||
return m.defaultIfEmpty(ReactorUtils.NULL).flatMap(func(exchange, chain));
|
||||
} else {
|
||||
@@ -111,6 +113,19 @@ public class PreFilter extends ProxyAggrFilter {
|
||||
);
|
||||
}
|
||||
|
||||
private void afterAuth(ServerWebExchange exchange, ApiConfig ac) {
|
||||
String bs, bp;
|
||||
if (ac == null) {
|
||||
bs = WebUtils.getClientService(exchange);
|
||||
bp = WebUtils.getClientReqPath(exchange);
|
||||
} else {
|
||||
bs = ac.backendService;
|
||||
bp = ac.transform(WebUtils.getClientReqPath(exchange));
|
||||
}
|
||||
WebUtils.setBackendService(exchange, bs);
|
||||
WebUtils.setBackendPath(exchange, bp);
|
||||
}
|
||||
|
||||
private Mono chain(ServerWebExchange exchange, Mono m, PluginFilter pf) {
|
||||
return m.defaultIfEmpty(ReactorUtils.NULL).flatMap(
|
||||
v -> {
|
||||
|
||||
@@ -32,7 +32,7 @@ public abstract class ProxyAggrFilter implements WebFilter {
|
||||
|
||||
@Override
|
||||
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
|
||||
String serviceId = WebUtils.getServiceId(exchange);
|
||||
String serviceId = WebUtils.getClientService(exchange);
|
||||
if (serviceId == null) {
|
||||
return chain.filter(exchange);
|
||||
} else {
|
||||
|
||||
@@ -22,7 +22,6 @@ import org.slf4j.LoggerFactory;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||
import org.springframework.http.server.reactive.ServerHttpResponse;
|
||||
@@ -35,7 +34,6 @@ import reactor.core.publisher.Mono;
|
||||
import we.flume.clients.log4j2appender.LogService;
|
||||
import we.legacy.RespEntity;
|
||||
import we.plugin.auth.ApiConfig;
|
||||
import we.plugin.auth.AuthPluginFilter;
|
||||
import we.proxy.FizzWebClient;
|
||||
import we.util.Constants;
|
||||
import we.util.ThreadContext;
|
||||
@@ -106,21 +104,19 @@ public class RouteFilter extends ProxyAggrFilter {
|
||||
);
|
||||
}
|
||||
|
||||
String reqPath = WebUtils.getReqPath(exchange);
|
||||
String rid = clientReq.getId();
|
||||
|
||||
ApiConfig ac = WebUtils.getApiConfig(exchange);
|
||||
if (ac == null) {
|
||||
String relativeUri = WebUtils.getRelativeUri(exchange);
|
||||
return send(exchange, WebUtils.getServiceId(exchange), relativeUri, hdrs);
|
||||
String pathQuery = WebUtils.getClientReqPathQuery(exchange);
|
||||
return send(exchange, WebUtils.getClientService(exchange), pathQuery, hdrs);
|
||||
|
||||
} else if (ac.type == ApiConfig.Type.SERVICE_DISCOVERY) {
|
||||
String relativeUri = WebUtils.appendQuery(ac.transform(reqPath), exchange);
|
||||
return send(exchange, ac.backendService, relativeUri, hdrs);
|
||||
String pathQuery = WebUtils.appendQuery(WebUtils.getBackendPath(exchange), exchange);
|
||||
return send(exchange, WebUtils.getBackendService(exchange), pathQuery, hdrs);
|
||||
|
||||
} else if (ac.type == ApiConfig.Type.REVERSE_PROXY) {
|
||||
String relativeUri = ac.getNextHttpHostPort() + WebUtils.appendQuery(ac.transform(reqPath), exchange);
|
||||
return fizzWebClient.send(rid, clientReq.getMethod(), relativeUri, hdrs, clientReq.getBody()).flatMap(genServerResponse(exchange));
|
||||
String uri = ac.getNextHttpHostPort() + WebUtils.appendQuery(WebUtils.getBackendPath(exchange), exchange);
|
||||
return fizzWebClient.send(rid, clientReq.getMethod(), uri, hdrs, clientReq.getBody()).flatMap(genServerResponse(exchange));
|
||||
|
||||
} else {
|
||||
String err = "cant handle api config type " + ac.type;
|
||||
|
||||
@@ -279,8 +279,8 @@ public class ApiConfigService {
|
||||
ServerHttpRequest req = exchange.getRequest();
|
||||
HttpHeaders hdrs = req.getHeaders();
|
||||
LogService.setBizId(req.getId());
|
||||
return canAccess(exchange, WebUtils.getAppId(exchange), WebUtils.getOriginIp(exchange), hdrs.getFirst(timestampHeader), hdrs.getFirst(signHeader), hdrs.getFirst(secretKeyHeader),
|
||||
WebUtils.getServiceId(exchange), req.getMethod(), WebUtils.getReqPath(exchange));
|
||||
return canAccess(exchange, WebUtils.getAppId(exchange), WebUtils.getOriginIp(exchange), hdrs.getFirst(timestampHeader), hdrs.getFirst(signHeader), hdrs.getFirst(secretKeyHeader),
|
||||
WebUtils.getClientService(exchange), req.getMethod(), WebUtils.getClientReqPath(exchange));
|
||||
}
|
||||
|
||||
private Mono<Object> canAccess(ServerWebExchange exchange, String app, String ip, String timestamp, String sign, String secretKey,
|
||||
|
||||
@@ -104,7 +104,7 @@ public class StatPluginFilter extends PluginFilter {
|
||||
b.append(Constants.Symbol.LEFT_BRACE);
|
||||
b.append(ip); toJsonStringValue(b, WebUtils.getOriginIp(exchange)); b.append(Constants.Symbol.COMMA);
|
||||
b.append(gatewayGroup); toJsonStringValue(b, currentGatewayGroups); b.append(Constants.Symbol.COMMA);
|
||||
b.append(service); toJsonStringValue(b, WebUtils.getServiceId(exchange)); b.append(Constants.Symbol.COMMA);
|
||||
b.append(service); toJsonStringValue(b, WebUtils.getClientService(exchange)); b.append(Constants.Symbol.COMMA);
|
||||
|
||||
String appId = WebUtils.getAppId(exchange);
|
||||
if (appId != null) {
|
||||
@@ -112,7 +112,7 @@ public class StatPluginFilter extends PluginFilter {
|
||||
}
|
||||
|
||||
b.append(apiMethod); toJsonStringValue(b, exchange.getRequest().getMethodValue()); b.append(Constants.Symbol.COMMA);
|
||||
b.append(apiPath); toJsonStringValue(b, WebUtils.getReqPath(exchange)); b.append(Constants.Symbol.COMMA);
|
||||
b.append(apiPath); toJsonStringValue(b, WebUtils.getClientReqPath(exchange)); b.append(Constants.Symbol.COMMA);
|
||||
b.append(reqTime) .append(System.currentTimeMillis());
|
||||
b.append(Constants.Symbol.RIGHT_BRACE);
|
||||
|
||||
|
||||
@@ -45,39 +45,45 @@ import java.util.*;
|
||||
|
||||
public abstract class WebUtils {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(WebUtils.class);
|
||||
private static final Logger log = LoggerFactory.getLogger(WebUtils.class);
|
||||
|
||||
private static final String SERVICE_ID = "serviceId";
|
||||
private static final String clientService = "clientService";
|
||||
|
||||
private static final String xForwardedFor = "X-FORWARDED-FOR";
|
||||
public static final String BACKEND_SERVICE = "backendService";
|
||||
|
||||
private static final String unknown = "unknown";
|
||||
private static final String xForwardedFor = "X-FORWARDED-FOR";
|
||||
|
||||
private static final String loopBack = "127.0.0.1";
|
||||
private static final String unknown = "unknown";
|
||||
|
||||
private static final String binaryAddress = "0:0:0:0:0:0:0:1";
|
||||
private static final String loopBack = "127.0.0.1";
|
||||
|
||||
private static final String directResponse = "directResponse";
|
||||
private static final String binaryAddress = "0:0:0:0:0:0:0:1";
|
||||
|
||||
private static final String response = " response ";
|
||||
private static final String directResponse = "directResponse";
|
||||
|
||||
private static final String originIp = "originIp";
|
||||
private static final String response = " response ";
|
||||
|
||||
public static final String APP_HEADER = "fizz-appid";
|
||||
private static final String originIp = "originIp";
|
||||
|
||||
public static final String FILTER_CONTEXT = "filterContext";
|
||||
public static final String APP_HEADER = "fizz-appid";
|
||||
|
||||
public static final String APPEND_HEADERS = "appendHeaders";
|
||||
public static final String FILTER_CONTEXT = "filterContext";
|
||||
|
||||
public static final String PREV_FILTER_RESULT = "prevFilterResult";
|
||||
public static final String APPEND_HEADERS = "appendHeaders";
|
||||
|
||||
public static final String request_path = "reqPath";
|
||||
public static final String PREV_FILTER_RESULT = "prevFilterResult";
|
||||
|
||||
public static boolean logResponseBody = false;
|
||||
private static final String CLIENT_REQUEST_PATH = "clientRequestPath";
|
||||
|
||||
public static Set<String> logHeaderSet = Collections.EMPTY_SET;
|
||||
private static final String CLIENT_REQUEST_QUERY = "clientRequestQuery";
|
||||
|
||||
public static final String PATH_PREFIX = "/proxy/";
|
||||
public static final String BACKEND_PATH = "backendPath";
|
||||
|
||||
public static boolean logResponseBody = false;
|
||||
|
||||
public static Set<String> logHeaderSet = Collections.EMPTY_SET;
|
||||
|
||||
public static final String PATH_PREFIX = "/proxy/";
|
||||
|
||||
public static String getHeaderValue(ServerWebExchange exchange, String header) {
|
||||
return exchange.getRequest().getHeaders().getFirst(header);
|
||||
@@ -91,8 +97,8 @@ public abstract class WebUtils {
|
||||
return exchange.getAttribute(APP_HEADER);
|
||||
}
|
||||
|
||||
public static String getServiceId(ServerWebExchange exchange) {
|
||||
String svc = exchange.getAttribute(SERVICE_ID);
|
||||
public static String getClientService(ServerWebExchange exchange) {
|
||||
String svc = exchange.getAttribute(clientService);
|
||||
if (svc == null) {
|
||||
String p = exchange.getRequest().getPath().value();
|
||||
int pl = p.length();
|
||||
@@ -117,13 +123,21 @@ public abstract class WebUtils {
|
||||
break;
|
||||
}
|
||||
}
|
||||
exchange.getAttributes().put(SERVICE_ID, svc);
|
||||
exchange.getAttributes().put(clientService, svc);
|
||||
}
|
||||
}
|
||||
}
|
||||
return svc;
|
||||
}
|
||||
|
||||
public static void setBackendService(ServerWebExchange exchange, String service) {
|
||||
exchange.getAttributes().put(BACKEND_SERVICE, service);
|
||||
}
|
||||
|
||||
public static String getBackendService(ServerWebExchange exchange) {
|
||||
return exchange.getAttribute(BACKEND_SERVICE);
|
||||
}
|
||||
|
||||
public static byte getApiConfigType(ServerWebExchange exchange) {
|
||||
ApiConfig ac = getApiConfig(exchange);
|
||||
if (ac == null) {
|
||||
@@ -142,11 +156,6 @@ public abstract class WebUtils {
|
||||
}
|
||||
}
|
||||
|
||||
public static String getPathPrefix(ServerWebExchange exchange) {
|
||||
String p = exchange.getRequest().getPath().value();
|
||||
return p.substring(0, p.indexOf(getServiceId(exchange)));
|
||||
}
|
||||
|
||||
public static Mono<Void> getDirectResponse(ServerWebExchange exchange) {
|
||||
return (Mono<Void>) exchange.getAttributes().get(WebUtils.directResponse);
|
||||
}
|
||||
@@ -238,30 +247,53 @@ public abstract class WebUtils {
|
||||
return getFilterContext(exchange).get(PREV_FILTER_RESULT);
|
||||
}
|
||||
|
||||
public static String getReqPath(ServerWebExchange exchange) {
|
||||
String path = exchange.getAttribute(request_path);
|
||||
public static String getClientReqPath(ServerWebExchange exchange) {
|
||||
String path = exchange.getAttribute(CLIENT_REQUEST_PATH);
|
||||
if (path == null) {
|
||||
path = exchange.getRequest().getPath().value();
|
||||
path = path.substring(path.indexOf(Constants.Symbol.FORWARD_SLASH, 11), path.length());
|
||||
exchange.getAttributes().put(request_path, path);
|
||||
exchange.getAttributes().put(CLIENT_REQUEST_PATH, path);
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
public static String getQuery(ServerWebExchange exchange) {
|
||||
URI uri = exchange.getRequest().getURI();
|
||||
String qry = uri.getQuery();
|
||||
if (qry != null) {
|
||||
if (StringUtils.indexOfAny(qry, Constants.Symbol.LEFT_BRACE, Constants.Symbol.FORWARD_SLASH, Constants.Symbol.HASH) > 0) {
|
||||
qry = uri.getRawQuery();
|
||||
}
|
||||
}
|
||||
return qry;
|
||||
public static void setBackendPath(ServerWebExchange exchange, String path) {
|
||||
exchange.getAttributes().put(BACKEND_PATH, path);
|
||||
}
|
||||
|
||||
public static String getRelativeUri(ServerWebExchange exchange) {
|
||||
String relativeUri = getReqPath(exchange);
|
||||
String qry = getQuery(exchange);
|
||||
public static String getBackendPath(ServerWebExchange exchange) {
|
||||
return exchange.getAttribute(BACKEND_PATH);
|
||||
}
|
||||
|
||||
public static String getClientReqPathPrefix(ServerWebExchange exchange) {
|
||||
String p = exchange.getRequest().getPath().value();
|
||||
return p.substring(0, p.indexOf(getClientService(exchange)));
|
||||
}
|
||||
|
||||
public static String getClientReqQuery(ServerWebExchange exchange) {
|
||||
String qry = exchange.getAttribute(CLIENT_REQUEST_QUERY);
|
||||
if (qry != null && StringUtils.EMPTY.equals(qry)) {
|
||||
return null;
|
||||
} else {
|
||||
if (qry == null) {
|
||||
URI uri = exchange.getRequest().getURI();
|
||||
qry = uri.getQuery();
|
||||
if (qry == null) {
|
||||
exchange.getAttributes().put(CLIENT_REQUEST_QUERY, StringUtils.EMPTY);
|
||||
} else {
|
||||
if (StringUtils.indexOfAny(qry, Constants.Symbol.LEFT_BRACE, Constants.Symbol.FORWARD_SLASH, Constants.Symbol.HASH) > 0) {
|
||||
qry = uri.getRawQuery();
|
||||
}
|
||||
exchange.getAttributes().put(CLIENT_REQUEST_QUERY, qry);
|
||||
}
|
||||
}
|
||||
return qry;
|
||||
}
|
||||
}
|
||||
|
||||
public static String getClientReqPathQuery(ServerWebExchange exchange) {
|
||||
String relativeUri = getClientReqPath(exchange);
|
||||
String qry = getClientReqQuery(exchange);
|
||||
if (qry != null) {
|
||||
relativeUri = relativeUri + Constants.Symbol.QUESTION + qry;
|
||||
}
|
||||
@@ -269,7 +301,7 @@ public abstract class WebUtils {
|
||||
}
|
||||
|
||||
public static String appendQuery(String path, ServerWebExchange exchange) {
|
||||
String qry = getQuery(exchange);
|
||||
String qry = getClientReqQuery(exchange);
|
||||
if (qry != null) {
|
||||
return path + Constants.Symbol.QUESTION + qry;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user