差点
This commit is contained in:
parent
b77c422309
commit
9c49ced6ed
|
@ -15,51 +15,50 @@ import lombok.extern.slf4j.Slf4j;
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Component
|
|
||||||
@SpringBootConfiguration
|
@SpringBootConfiguration
|
||||||
@Order(-200)
|
@Order(-200)
|
||||||
public class VerifyFilter implements GlobalFilter {
|
public class VerifyFilter {
|
||||||
|
|
||||||
@Override
|
// @Override
|
||||||
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
// public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
||||||
|
|
||||||
if (true)
|
// if (true)
|
||||||
return chain.filter(exchange);
|
// return chain.filter(exchange);
|
||||||
// TODO: 添加权限
|
// // TODO: 添加权限
|
||||||
ServerHttpRequest request = exchange.getRequest();
|
// ServerHttpRequest request = exchange.getRequest();
|
||||||
HttpHeaders header = request.getHeaders();
|
// HttpHeaders header = request.getHeaders();
|
||||||
|
|
||||||
ServerHttpResponse response = exchange.getResponse();
|
// ServerHttpResponse response = exchange.getResponse();
|
||||||
HttpHeaders respHeader = response.getHeaders();
|
// HttpHeaders respHeader = response.getHeaders();
|
||||||
respHeader.add("token-expire", String.valueOf(System.currentTimeMillis() / 1000));
|
// respHeader.add("token-expire", String.valueOf(System.currentTimeMillis() / 1000));
|
||||||
respHeader.add("version", "v0.0.1");
|
// respHeader.add("version", "v0.0.1");
|
||||||
|
|
||||||
String token = header.getFirst("token");
|
// String token = header.getFirst("token");
|
||||||
if (token != null) {
|
// if (token != null) {
|
||||||
//
|
// //
|
||||||
if (VerifyToken(token)) {
|
// if (VerifyToken(token)) {
|
||||||
log.info("request token is Verify Successed: {}", token);
|
// log.info("request token is Verify Successed: {}", token);
|
||||||
return chain.filter(exchange);
|
// return chain.filter(exchange);
|
||||||
}
|
// }
|
||||||
log.info("request token is Verify Fail: {} != {}", token, TEST_TOKEN);
|
// log.info("request token is Verify Fail: {} != {}", token, TEST_TOKEN);
|
||||||
} else {
|
// } else {
|
||||||
log.warn("request token is empty: {}", request.getURI());
|
// log.warn("request token is empty: {}", request.getURI());
|
||||||
}
|
// }
|
||||||
|
|
||||||
if (!response.setStatusCode(HttpStatus.UNAUTHORIZED)) {
|
// if (!response.setStatusCode(HttpStatus.UNAUTHORIZED)) {
|
||||||
log.error("if the status code has not been set because the HTTP response is already committed");
|
// log.error("if the status code has not been set because the HTTP response is already committed");
|
||||||
}
|
// }
|
||||||
|
|
||||||
return response.setComplete();
|
// return response.setComplete();
|
||||||
}
|
// }
|
||||||
|
|
||||||
public static final String TEST_TOKEN = "yame";
|
// public static final String TEST_TOKEN = "yame";
|
||||||
|
|
||||||
public boolean VerifyToken(String token) {
|
// public boolean VerifyToken(String token) {
|
||||||
if (token.equals(TEST_TOKEN)) {
|
// if (token.equals(TEST_TOKEN)) {
|
||||||
return true;
|
// return true;
|
||||||
}
|
// }
|
||||||
return false;
|
// return false;
|
||||||
}
|
// }
|
||||||
|
|
||||||
}
|
}
|
|
@ -1,7 +1,9 @@
|
||||||
package cn.ecpark.service.usergw.biz.filters.factory;
|
package cn.ecpark.service.usergw.biz.filters.factory;
|
||||||
|
|
||||||
import java.lang.annotation.Retention;
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.reflect.Field;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
|
@ -15,18 +17,23 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.cloud.gateway.filter.GatewayFilter;
|
import org.springframework.cloud.gateway.filter.GatewayFilter;
|
||||||
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
||||||
import org.springframework.cloud.gateway.filter.NettyWriteResponseFilter;
|
import org.springframework.cloud.gateway.filter.NettyWriteResponseFilter;
|
||||||
|
import org.springframework.cloud.gateway.filter.OrderedGatewayFilter;
|
||||||
import org.springframework.cloud.gateway.filter.RouteToRequestUrlFilter;
|
import org.springframework.cloud.gateway.filter.RouteToRequestUrlFilter;
|
||||||
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
|
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
|
||||||
|
import org.springframework.cloud.gateway.filter.factory.SetStatusGatewayFilterFactory;
|
||||||
|
import org.springframework.cloud.gateway.handler.FilteringWebHandler;
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.core.Ordered;
|
import org.springframework.core.Ordered;
|
||||||
import org.springframework.core.annotation.Order;
|
import org.springframework.core.annotation.Order;
|
||||||
import org.springframework.core.io.buffer.DataBuffer;
|
import org.springframework.core.io.buffer.DataBuffer;
|
||||||
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.server.reactive.ServerHttpRequest;
|
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||||
import org.springframework.http.server.reactive.ServerHttpResponse;
|
import org.springframework.http.server.reactive.ServerHttpResponse;
|
||||||
import org.springframework.http.server.reactive.ServerHttpResponseDecorator;
|
import org.springframework.http.server.reactive.ServerHttpResponseDecorator;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.web.server.ServerWebExchange;
|
import org.springframework.web.server.ServerWebExchange;
|
||||||
|
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.setResponseStatus;
|
||||||
|
|
||||||
import cn.ecpark.service.usergw.biz.filters.bean.GenericServicePool;
|
import cn.ecpark.service.usergw.biz.filters.bean.GenericServicePool;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
@ -34,7 +41,7 @@ import reactor.core.publisher.Mono;;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class DubboGatewayFilterFactory extends AbstractGatewayFilterFactory<DubboGatewayFilterFactory.Config> {
|
public class DubboGatewayFilterFactory extends AbstractGatewayFilterFactory<DubboGatewayFilterFactory.Config> {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private ApplicationContext appContext;
|
private ApplicationContext appContext;
|
||||||
|
@ -45,8 +52,6 @@ public class DubboGatewayFilterFactory extends AbstractGatewayFilterFactory<Dub
|
||||||
public static final String DUBBO_URI = "dubbo_uri";
|
public static final String DUBBO_URI = "dubbo_uri";
|
||||||
public static HashMap<String, List<String>> mehtods = new HashMap<>();
|
public static HashMap<String, List<String>> mehtods = new HashMap<>();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public DubboGatewayFilterFactory() {
|
public DubboGatewayFilterFactory() {
|
||||||
super(Config.class);
|
super(Config.class);
|
||||||
}
|
}
|
||||||
|
@ -58,7 +63,8 @@ public class DubboGatewayFilterFactory extends AbstractGatewayFilterFactory<Dub
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public GatewayFilter apply(Config config) {
|
public GatewayFilter apply(Config config) {
|
||||||
return new DubboGatewayFilter(config);
|
DubboGatewayFilter dubboGatewayFilter = new DubboGatewayFilter(config);
|
||||||
|
return new OrderedGatewayFilter(dubboGatewayFilter, Ordered.HIGHEST_PRECEDENCE);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class Config {
|
public static class Config {
|
||||||
|
@ -75,29 +81,45 @@ public class DubboGatewayFilterFactory extends AbstractGatewayFilterFactory<Dub
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public class DubboGatewayFilter implements GatewayFilter, Ordered {
|
public class DubboGatewayFilter implements GatewayFilter {
|
||||||
|
|
||||||
private final Config config;
|
private final Config config;
|
||||||
private List<String> emptyList = new LinkedList<String>();
|
private List<String> emptyList = new LinkedList<String>();
|
||||||
|
|
||||||
public DubboGatewayFilter(Config config) {
|
public DubboGatewayFilter(Config config) {
|
||||||
this.config = config;
|
this.config = config;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
// @SuppressWarnings("unchecked")
|
||||||
|
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
||||||
|
|
||||||
|
// TODO: 侵入私有变量, 但是这样不如直接改源码了, 效率低.
|
||||||
|
// try {
|
||||||
|
|
||||||
|
// Field filtersField = chain.getClass().getDeclaredField("filters");
|
||||||
|
// filtersField.setAccessible(true);
|
||||||
|
// ArrayList<GatewayFilter> filters =
|
||||||
|
// (ArrayList<GatewayFilter>)filtersField.get(chain);
|
||||||
|
// log.info(filters.toString());
|
||||||
|
// } catch (Exception e) {
|
||||||
|
// log.error(e.getMessage());
|
||||||
|
// setResponseStatus(exchange, HttpStatus.INTERNAL_SERVER_ERROR);
|
||||||
|
// return chain.filter(exchange);
|
||||||
|
// }
|
||||||
|
|
||||||
|
return chain.filter(exchange).then(Mono.fromRunnable(() -> {
|
||||||
|
|
||||||
@Override
|
|
||||||
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
|
||||||
|
|
||||||
return chain.filter(exchange).then(Mono.fromRunnable( ()->{
|
|
||||||
ServerHttpRequest req = exchange.getRequest();
|
ServerHttpRequest req = exchange.getRequest();
|
||||||
HttpHeaders headers = req.getHeaders();
|
HttpHeaders headers = req.getHeaders();
|
||||||
|
|
||||||
List<String> methodString = headers.get("method");
|
List<String> methodString = headers.get("method");
|
||||||
List<String> params = headers.get("params");
|
List<String> params = headers.get("params");
|
||||||
|
|
||||||
Object result = null;
|
Object result = null;
|
||||||
ServerHttpResponse response = exchange.getResponse();
|
ServerHttpResponse response = exchange.getResponse();
|
||||||
|
HttpStatus statusCode = response.getStatusCode();
|
||||||
if (methodString.size() != 0) {
|
if (statusCode.value() == 200 && methodString.size() != 0) {
|
||||||
List<String> paramTypes;
|
List<String> paramTypes;
|
||||||
// 判断全部函数允许, 必须带参数类型, 而且要匹配, 否则报错
|
// 判断全部函数允许, 必须带参数类型, 而且要匹配, 否则报错
|
||||||
if (config.dubboUri.charAt(5) == '-') {
|
if (config.dubboUri.charAt(5) == '-') {
|
||||||
|
@ -108,18 +130,18 @@ public class DubboGatewayFilterFactory extends AbstractGatewayFilterFactory<Dub
|
||||||
} else {
|
} else {
|
||||||
paramTypes = mehtods.get(methodString.get(0));
|
paramTypes = mehtods.get(methodString.get(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (paramTypes != null) {
|
if (paramTypes != null) {
|
||||||
int paramsSize = 0;
|
int paramsSize = 0;
|
||||||
if (params != null) {
|
if (params != null) {
|
||||||
paramsSize = params.size();
|
paramsSize = params.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (paramTypes.size() == paramsSize) {
|
if (paramTypes.size() == paramsSize) {
|
||||||
|
|
||||||
GenericServicePool gsPool = appContext.getBean(GenericServicePool.class);
|
GenericServicePool gsPool = appContext.getBean(GenericServicePool.class);
|
||||||
GenericService gs = gsPool.get(config.dubboUri);
|
GenericService gs = gsPool.get(config.dubboUri);
|
||||||
|
|
||||||
if (paramsSize == 0) {
|
if (paramsSize == 0) {
|
||||||
result = gs.$invoke(methodString.get(0), new String[] {}, new Object[] {});
|
result = gs.$invoke(methodString.get(0), new String[] {}, new Object[] {});
|
||||||
} else {
|
} else {
|
||||||
|
@ -127,32 +149,31 @@ public class DubboGatewayFilterFactory extends AbstractGatewayFilterFactory<Dub
|
||||||
Arrays.copyOf(paramTypes.toArray(), paramTypes.size(), String[].class),
|
Arrays.copyOf(paramTypes.toArray(), paramTypes.size(), String[].class),
|
||||||
params.toArray());
|
params.toArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (result == null) {
|
if (result == null) {
|
||||||
response.setComplete();
|
response.setComplete().block();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (result.getClass() == String.class) {
|
if (result.getClass() == String.class) {
|
||||||
response.writeWith(Mono.just(
|
response.writeWith(Mono.just(
|
||||||
response.bufferFactory().wrap(ByteBuffer.wrap(((String) result).getBytes()))));
|
response.bufferFactory().wrap(ByteBuffer.wrap(((String) result).getBytes()))));
|
||||||
} else {
|
} else {
|
||||||
response.writeWith(Mono.just(
|
response.writeWith(Mono.just(response.bufferFactory()
|
||||||
response.bufferFactory().wrap(ByteBuffer.wrap(JSON.toJSONString(result).getBytes()))));
|
.wrap(ByteBuffer.wrap(JSON.toJSONString(result).getBytes()))));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
|
response.setStatusCode(statusCode);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
// response.setComplete().block();
|
||||||
|
// return chain.filter(exchange);
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getOrder() {
|
|
||||||
return RouteToRequestUrlFilter.ROUTE_TO_URL_FILTER_ORDER - 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
|
@ -29,6 +29,14 @@ restful:
|
||||||
# filters:
|
# filters:
|
||||||
# - RedirectTo=302, http://httpbin.org:80/get
|
# - RedirectTo=302, http://httpbin.org:80/get
|
||||||
|
|
||||||
|
# - id: path_route14
|
||||||
|
# uri: http://httpbin.org:80/*
|
||||||
|
# order: 10
|
||||||
|
# predicates:
|
||||||
|
# - Path=/dubbo/hello
|
||||||
|
# filters:
|
||||||
|
# - SetStatus=404
|
||||||
|
|
||||||
dubbo:
|
dubbo:
|
||||||
routes:
|
routes:
|
||||||
- id: test
|
- id: test
|
||||||
|
|
Loading…
Reference in New Issue
Block a user