Merge pull request #340 from wehotel/develop
This commit is contained in:
@@ -20,6 +20,7 @@ package we.util;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@@ -30,11 +31,17 @@ public abstract class ThreadContext {
|
||||
|
||||
private static ThreadLocal<Map<String, Object>> tl = new ThreadLocal<>();
|
||||
|
||||
private static final int mapCap = 32;
|
||||
private static final int mapCap = 32;
|
||||
|
||||
private static final String sb = "$sb";
|
||||
private static final String sb = "$sb";
|
||||
|
||||
private static final int sbCap = 256;
|
||||
private static final int sbCap = 256;
|
||||
|
||||
private static final String arrayListT = "arlstT";
|
||||
|
||||
private static final String hashMapT = "hsMapT";
|
||||
|
||||
private static final String hashSetT = "hsSetT";
|
||||
|
||||
private ThreadContext() {
|
||||
}
|
||||
@@ -116,6 +123,10 @@ public abstract class ThreadContext {
|
||||
return getMap().remove(key);
|
||||
}
|
||||
|
||||
public static <T> ArrayList<T> getArrayList() {
|
||||
return getArrayList(arrayListT, true);
|
||||
}
|
||||
|
||||
public static <T> ArrayList<T> getArrayList(String key) {
|
||||
return getArrayList(key, true);
|
||||
}
|
||||
@@ -131,6 +142,10 @@ public abstract class ThreadContext {
|
||||
return l;
|
||||
}
|
||||
|
||||
public static <K, V> HashMap<K, V> getHashMap() {
|
||||
return getHashMap(hashMapT, true);
|
||||
}
|
||||
|
||||
public static <K, V> HashMap<K, V> getHashMap(String key) {
|
||||
return getHashMap(key, true);
|
||||
}
|
||||
@@ -145,4 +160,23 @@ public abstract class ThreadContext {
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
public static <E> HashSet<E> getHashSet() {
|
||||
return getHashSet(hashSetT, true);
|
||||
}
|
||||
|
||||
public static <E> HashSet<E> getHashSet(String key) {
|
||||
return getHashSet(key, true);
|
||||
}
|
||||
|
||||
public static <E> HashSet<E> getHashSet(String key, boolean clear) {
|
||||
HashSet<E> s = (HashSet<E>) get(key);
|
||||
if (s == null) {
|
||||
s = new HashSet<E>();
|
||||
set(key ,s);
|
||||
} else if (clear) {
|
||||
s.clear();
|
||||
}
|
||||
return s;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,18 +28,19 @@ import org.springframework.web.server.WebFilterChain;
|
||||
import reactor.core.publisher.Mono;
|
||||
import we.plugin.FixedPluginFilter;
|
||||
import we.plugin.FizzPluginFilterChain;
|
||||
import we.plugin.PluginConfig;
|
||||
import we.plugin.auth.ApiConfig;
|
||||
import we.plugin.auth.AuthPluginFilter;
|
||||
import we.plugin.auth.GatewayGroupService;
|
||||
import we.plugin.stat.StatPluginFilter;
|
||||
import we.proxy.Route;
|
||||
import we.util.ReactorUtils;
|
||||
import we.util.Result;
|
||||
import we.util.ThreadContext;
|
||||
import we.util.WebUtils;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
@@ -57,10 +58,13 @@ public class PreprocessFilter extends FizzWebFilter {
|
||||
private static final FilterResult succFr = FilterResult.SUCCESS(PREPROCESS_FILTER);
|
||||
|
||||
@Resource(name = StatPluginFilter.STAT_PLUGIN_FILTER)
|
||||
private StatPluginFilter statPluginFilter;
|
||||
private StatPluginFilter statPluginFilter;
|
||||
|
||||
@Resource(name = AuthPluginFilter.AUTH_PLUGIN_FILTER)
|
||||
private AuthPluginFilter authPluginFilter;
|
||||
private AuthPluginFilter authPluginFilter;
|
||||
|
||||
@Resource
|
||||
private GatewayGroupService gatewayGroupService;
|
||||
|
||||
@Override
|
||||
public Mono<Void> doFilter(ServerWebExchange exchange, WebFilterChain chain) {
|
||||
@@ -78,16 +82,18 @@ public class PreprocessFilter extends FizzWebFilter {
|
||||
.thenReturn(ReactorUtils.Void)
|
||||
.flatMap(
|
||||
v -> {
|
||||
Result<ApiConfig> authRes = (Result<ApiConfig>) WebUtils.getFilterResultDataItem(exchange, AuthPluginFilter.AUTH_PLUGIN_FILTER, AuthPluginFilter.RESULT);
|
||||
if (authRes.code == Result.FAIL) {
|
||||
return WebUtils.responseError(exchange, HttpStatus.FORBIDDEN.value(), authRes.msg);
|
||||
Result<ApiConfig> auth = (Result<ApiConfig>) WebUtils.getFilterResultDataItem(exchange, AuthPluginFilter.AUTH_PLUGIN_FILTER, AuthPluginFilter.RESULT);
|
||||
if (auth.code == Result.FAIL) {
|
||||
return WebUtils.responseError(exchange, HttpStatus.FORBIDDEN.value(), auth.msg);
|
||||
}
|
||||
ApiConfig ac = authRes.data;
|
||||
ApiConfig ac = auth.data;
|
||||
if (ac == null) {
|
||||
afterAuth(exchange, null, null);
|
||||
return executeFixedPluginFilters(exchange).thenReturn(ReactorUtils.Void).flatMap(checkDirectRespOrChainFilter(exchange, chain));
|
||||
}
|
||||
Route route = ac.getRoute(exchange);
|
||||
|
||||
List<PluginConfig> gatewayGroupPluginConfigs = gatewayGroupService.get(ac.firstGatewayGroup).pluginConfigs;
|
||||
Route route = ac.getRoute(exchange, gatewayGroupPluginConfigs);
|
||||
eas.put(WebUtils.ROUTE, route);
|
||||
afterAuth(exchange, ac, route);
|
||||
|
||||
@@ -95,7 +101,7 @@ public class PreprocessFilter extends FizzWebFilter {
|
||||
executeFixedPluginFilters(exchange).thenReturn(ReactorUtils.Void)
|
||||
.flatMap(
|
||||
e -> {
|
||||
if (CollectionUtils.isEmpty(route.pluginConfigs)) {
|
||||
if (route.pluginConfigs.isEmpty()) {
|
||||
return checkDirectRespOrChainFilter(exchange, chain).apply(ReactorUtils.NULL);
|
||||
} else {
|
||||
eas.put(FizzPluginFilterChain.WEB_FILTER_CHAIN, chain);
|
||||
@@ -107,6 +113,7 @@ public class PreprocessFilter extends FizzWebFilter {
|
||||
);
|
||||
}
|
||||
|
||||
// ?
|
||||
private void afterAuth(ServerWebExchange exchange, ApiConfig ac, Route route) {
|
||||
String bs = null, bp = null;
|
||||
if (ac == null) {
|
||||
|
||||
@@ -28,17 +28,18 @@ import java.util.Map;
|
||||
* @author hongqiaowei
|
||||
*/
|
||||
|
||||
public class PluginConfig {
|
||||
public class PluginConfig implements Comparable<PluginConfig> {
|
||||
|
||||
public static final String CUSTOM_CONFIG = "fcK";
|
||||
|
||||
public String plugin; // tb_plugin.eng_name
|
||||
public String plugin; // tb_plugin.eng_name
|
||||
|
||||
public String fixedConfig;
|
||||
public String fixedConfig;
|
||||
|
||||
public Map<String/*tb_api_plugin_config.item*/, Object/*tb_api_plugin_config.value*/> config = Collections.emptyMap();
|
||||
public Map<String, Object> config = Collections.emptyMap();
|
||||
|
||||
public int order;
|
||||
|
||||
// @JsonProperty(value = "config", access = JsonProperty.Access.WRITE_ONLY)
|
||||
public void setConfig(String confJson) {
|
||||
if (StringUtils.isNotBlank(confJson)) {
|
||||
Map m = JacksonUtils.readValue(confJson, Map.class);
|
||||
@@ -63,4 +64,9 @@ public class PluginConfig {
|
||||
public String toString() {
|
||||
return JacksonUtils.writeValueAsString(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(PluginConfig that) {
|
||||
return this.order - that.order;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,6 +31,7 @@ public abstract class AbstractCustomAuth implements CustomAuth {
|
||||
/**
|
||||
* @deprecated
|
||||
*/
|
||||
@Deprecated
|
||||
@Override
|
||||
public Mono<ApiConfigService.Access> auth(ServerWebExchange exchange, String appId, String ip, String timestamp, String sign, App fizzAppConfig) {
|
||||
throw Utils.runtimeExceptionWithoutStack("don't implement me!");
|
||||
|
||||
@@ -29,9 +29,8 @@ import we.util.JacksonUtils;
|
||||
import we.util.UrlTransformUtils;
|
||||
import we.util.WebUtils;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@@ -72,9 +71,14 @@ public class ApiConfig {
|
||||
@JsonProperty(
|
||||
access = JsonProperty.Access.WRITE_ONLY
|
||||
)
|
||||
public int isDeleted = 0; // tb_api_auth.is_deleted
|
||||
public int isDeleted = 0; // tb_api_auth.is_deleted
|
||||
|
||||
public Set<String> gatewayGroups = Stream.of(GatewayGroup.DEFAULT).collect(Collectors.toSet());
|
||||
public Set<String> gatewayGroups = Stream.of(GatewayGroup.DEFAULT).collect(Collectors.toCollection(LinkedHashSet::new));
|
||||
|
||||
@JsonProperty(
|
||||
access = JsonProperty.Access.WRITE_ONLY
|
||||
)
|
||||
public String firstGatewayGroup;
|
||||
|
||||
public String service;
|
||||
|
||||
@@ -85,29 +89,29 @@ public class ApiConfig {
|
||||
)
|
||||
public HttpMethod method;
|
||||
|
||||
public Object fizzMethod = ALL_METHOD;
|
||||
public Object fizzMethod = ALL_METHOD;
|
||||
|
||||
public String path = match_all;
|
||||
public String path = match_all;
|
||||
|
||||
@JsonProperty(
|
||||
access = JsonProperty.Access.WRITE_ONLY
|
||||
)
|
||||
public boolean exactMatch = false;
|
||||
public boolean exactMatch = false;
|
||||
|
||||
public String backendPath;
|
||||
|
||||
@JsonProperty("proxyMode")
|
||||
public byte type = Type.SERVICE_DISCOVERY;
|
||||
public byte type = Type.SERVICE_DISCOVERY;
|
||||
|
||||
private int counter = 0;
|
||||
private int counter = 0;
|
||||
|
||||
public List<String> httpHostPorts;
|
||||
|
||||
public char access = ALLOW;
|
||||
public char access = ALLOW;
|
||||
|
||||
public List<PluginConfig> pluginConfigs;
|
||||
public List<PluginConfig> pluginConfigs = Collections.emptyList();
|
||||
|
||||
public boolean checkApp = false;
|
||||
public boolean checkApp = false;
|
||||
|
||||
public CallbackConfig callbackConfig;
|
||||
|
||||
@@ -119,11 +123,11 @@ public class ApiConfig {
|
||||
|
||||
public String rpcGroup;
|
||||
|
||||
public long timeout = 0;
|
||||
public long timeout = 0;
|
||||
|
||||
public int retryCount = 0;
|
||||
public int retryCount = 0;
|
||||
|
||||
public long retryInterval = 0;
|
||||
public long retryInterval = 0;
|
||||
|
||||
public static boolean isAntPathPattern(String path) {
|
||||
boolean uriVar = false;
|
||||
@@ -154,6 +158,7 @@ public class ApiConfig {
|
||||
}
|
||||
);
|
||||
}
|
||||
firstGatewayGroup = gatewayGroups.iterator().next();
|
||||
}
|
||||
|
||||
public void setPath(String p) {
|
||||
@@ -220,7 +225,7 @@ public class ApiConfig {
|
||||
return false;
|
||||
}
|
||||
|
||||
public Route getRoute(ServerWebExchange exchange) {
|
||||
public Route getRoute(ServerWebExchange exchange, @Nullable List<PluginConfig> gatewayGroupPluginConfigs) {
|
||||
ServerHttpRequest request = exchange.getRequest();
|
||||
Route r = new Route().type( this.type)
|
||||
.method( request.getMethod())
|
||||
@@ -236,7 +241,16 @@ public class ApiConfig {
|
||||
.retryCount( this.retryCount)
|
||||
.retryInterval( this.retryInterval);
|
||||
|
||||
r.pluginConfigs = this.pluginConfigs;
|
||||
if (gatewayGroupPluginConfigs == null || gatewayGroupPluginConfigs.isEmpty()) {
|
||||
r.pluginConfigs = this.pluginConfigs;
|
||||
} else {
|
||||
List<PluginConfig> pcs = new ArrayList<>(gatewayGroupPluginConfigs.size() + this.pluginConfigs.size());
|
||||
pcs.addAll(gatewayGroupPluginConfigs);
|
||||
pcs.addAll(this.pluginConfigs);
|
||||
pcs.sort(null);
|
||||
r.pluginConfigs = pcs;
|
||||
}
|
||||
|
||||
if (this.type == Type.REVERSE_PROXY) {
|
||||
r = r.nextHttpHostPort(getNextHttpHostPort());
|
||||
}
|
||||
|
||||
@@ -33,5 +33,6 @@ public interface CustomAuth {
|
||||
*
|
||||
* @deprecated
|
||||
*/
|
||||
@Deprecated
|
||||
Mono<ApiConfigService.Access> auth(ServerWebExchange exchange, String appId, String ip, String timestamp, String sign, App fizzAppConfig);
|
||||
}
|
||||
|
||||
@@ -20,11 +20,10 @@ package we.plugin.auth;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import we.plugin.PluginConfig;
|
||||
import we.util.JacksonUtils;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @author hongqiaowei
|
||||
@@ -38,15 +37,17 @@ public class GatewayGroup {
|
||||
|
||||
public static final int DELETED = 1;
|
||||
|
||||
public int id;
|
||||
public int id;
|
||||
|
||||
public int isDeleted = 0;
|
||||
public int isDeleted = 0;
|
||||
|
||||
public String group;
|
||||
public String group;
|
||||
|
||||
public String name;
|
||||
public String name;
|
||||
|
||||
public Set<String> gateways = new HashSet<>();
|
||||
public Set<String> gateways = new HashSet<>();
|
||||
|
||||
public List<PluginConfig> pluginConfigs = Collections.emptyList();
|
||||
|
||||
public void setGateways(String gateways) {
|
||||
if (StringUtils.isNotBlank(gateways)) {
|
||||
|
||||
@@ -209,4 +209,8 @@ public class GatewayGroupService {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public GatewayGroup get(String gg) {
|
||||
return gatewayGroupMap.get(gg);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user