差点
This commit is contained in:
parent
b77c422309
commit
9c49ced6ed
|
@ -15,51 +15,50 @@ import lombok.extern.slf4j.Slf4j;
|
|||
import reactor.core.publisher.Mono;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
@SpringBootConfiguration
|
||||
@Order(-200)
|
||||
public class VerifyFilter implements GlobalFilter {
|
||||
public class VerifyFilter {
|
||||
|
||||
@Override
|
||||
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
||||
// @Override
|
||||
// public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
||||
|
||||
if (true)
|
||||
return chain.filter(exchange);
|
||||
// TODO: 添加权限
|
||||
ServerHttpRequest request = exchange.getRequest();
|
||||
HttpHeaders header = request.getHeaders();
|
||||
// if (true)
|
||||
// return chain.filter(exchange);
|
||||
// // TODO: 添加权限
|
||||
// ServerHttpRequest request = exchange.getRequest();
|
||||
// HttpHeaders header = request.getHeaders();
|
||||
|
||||
ServerHttpResponse response = exchange.getResponse();
|
||||
HttpHeaders respHeader = response.getHeaders();
|
||||
respHeader.add("token-expire", String.valueOf(System.currentTimeMillis() / 1000));
|
||||
respHeader.add("version", "v0.0.1");
|
||||
// ServerHttpResponse response = exchange.getResponse();
|
||||
// HttpHeaders respHeader = response.getHeaders();
|
||||
// respHeader.add("token-expire", String.valueOf(System.currentTimeMillis() / 1000));
|
||||
// respHeader.add("version", "v0.0.1");
|
||||
|
||||
String token = header.getFirst("token");
|
||||
if (token != null) {
|
||||
//
|
||||
if (VerifyToken(token)) {
|
||||
log.info("request token is Verify Successed: {}", token);
|
||||
return chain.filter(exchange);
|
||||
}
|
||||
log.info("request token is Verify Fail: {} != {}", token, TEST_TOKEN);
|
||||
} else {
|
||||
log.warn("request token is empty: {}", request.getURI());
|
||||
}
|
||||
// String token = header.getFirst("token");
|
||||
// if (token != null) {
|
||||
// //
|
||||
// if (VerifyToken(token)) {
|
||||
// log.info("request token is Verify Successed: {}", token);
|
||||
// return chain.filter(exchange);
|
||||
// }
|
||||
// log.info("request token is Verify Fail: {} != {}", token, TEST_TOKEN);
|
||||
// } else {
|
||||
// log.warn("request token is empty: {}", request.getURI());
|
||||
// }
|
||||
|
||||
if (!response.setStatusCode(HttpStatus.UNAUTHORIZED)) {
|
||||
log.error("if the status code has not been set because the HTTP response is already committed");
|
||||
}
|
||||
// if (!response.setStatusCode(HttpStatus.UNAUTHORIZED)) {
|
||||
// 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) {
|
||||
if (token.equals(TEST_TOKEN)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
// public boolean VerifyToken(String token) {
|
||||
// if (token.equals(TEST_TOKEN)) {
|
||||
// return true;
|
||||
// }
|
||||
// return false;
|
||||
// }
|
||||
|
||||
}
|
|
@ -1,7 +1,9 @@
|
|||
package cn.ecpark.service.usergw.biz.filters.factory;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.reflect.Field;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
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.GatewayFilterChain;
|
||||
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.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.core.Ordered;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||
import org.springframework.http.server.reactive.ServerHttpResponse;
|
||||
import org.springframework.http.server.reactive.ServerHttpResponseDecorator;
|
||||
import org.springframework.stereotype.Component;
|
||||
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 lombok.extern.slf4j.Slf4j;
|
||||
|
@ -34,7 +41,7 @@ import reactor.core.publisher.Mono;;
|
|||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class DubboGatewayFilterFactory extends AbstractGatewayFilterFactory<DubboGatewayFilterFactory.Config> {
|
||||
public class DubboGatewayFilterFactory extends AbstractGatewayFilterFactory<DubboGatewayFilterFactory.Config> {
|
||||
|
||||
@Autowired
|
||||
private ApplicationContext appContext;
|
||||
|
@ -45,8 +52,6 @@ public class DubboGatewayFilterFactory extends AbstractGatewayFilterFactory<Dub
|
|||
public static final String DUBBO_URI = "dubbo_uri";
|
||||
public static HashMap<String, List<String>> mehtods = new HashMap<>();
|
||||
|
||||
|
||||
|
||||
public DubboGatewayFilterFactory() {
|
||||
super(Config.class);
|
||||
}
|
||||
|
@ -58,7 +63,8 @@ public class DubboGatewayFilterFactory extends AbstractGatewayFilterFactory<Dub
|
|||
|
||||
@Override
|
||||
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 {
|
||||
|
@ -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 List<String> emptyList = new LinkedList<String>();
|
||||
|
||||
public DubboGatewayFilter(Config config) {
|
||||
this.config = config;
|
||||
}
|
||||
public DubboGatewayFilter(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();
|
||||
HttpHeaders headers = req.getHeaders();
|
||||
|
||||
|
||||
List<String> methodString = headers.get("method");
|
||||
List<String> params = headers.get("params");
|
||||
|
||||
|
||||
Object result = null;
|
||||
ServerHttpResponse response = exchange.getResponse();
|
||||
|
||||
if (methodString.size() != 0) {
|
||||
HttpStatus statusCode = response.getStatusCode();
|
||||
if (statusCode.value() == 200 && methodString.size() != 0) {
|
||||
List<String> paramTypes;
|
||||
// 判断全部函数允许, 必须带参数类型, 而且要匹配, 否则报错
|
||||
if (config.dubboUri.charAt(5) == '-') {
|
||||
|
@ -108,18 +130,18 @@ public class DubboGatewayFilterFactory extends AbstractGatewayFilterFactory<Dub
|
|||
} else {
|
||||
paramTypes = mehtods.get(methodString.get(0));
|
||||
}
|
||||
|
||||
|
||||
if (paramTypes != null) {
|
||||
int paramsSize = 0;
|
||||
if (params != null) {
|
||||
paramsSize = params.size();
|
||||
}
|
||||
|
||||
|
||||
if (paramTypes.size() == paramsSize) {
|
||||
|
||||
|
||||
GenericServicePool gsPool = appContext.getBean(GenericServicePool.class);
|
||||
GenericService gs = gsPool.get(config.dubboUri);
|
||||
|
||||
|
||||
if (paramsSize == 0) {
|
||||
result = gs.$invoke(methodString.get(0), new String[] {}, new Object[] {});
|
||||
} else {
|
||||
|
@ -127,32 +149,31 @@ public class DubboGatewayFilterFactory extends AbstractGatewayFilterFactory<Dub
|
|||
Arrays.copyOf(paramTypes.toArray(), paramTypes.size(), String[].class),
|
||||
params.toArray());
|
||||
}
|
||||
|
||||
|
||||
if (result == null) {
|
||||
response.setComplete();
|
||||
response.setComplete().block();
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (result.getClass() == String.class) {
|
||||
response.writeWith(Mono.just(
|
||||
response.bufferFactory().wrap(ByteBuffer.wrap(((String) result).getBytes()))));
|
||||
response.bufferFactory().wrap(ByteBuffer.wrap(((String) result).getBytes()))));
|
||||
} else {
|
||||
response.writeWith(Mono.just(
|
||||
response.bufferFactory().wrap(ByteBuffer.wrap(JSON.toJSONString(result).getBytes()))));
|
||||
response.writeWith(Mono.just(response.bufferFactory()
|
||||
.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:
|
||||
# - 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:
|
||||
routes:
|
||||
- id: test
|
||||
|
|
Loading…
Reference in New Issue
Block a user