From 9c49ced6ed0ad1b610e92d387d4ef06909e95d28 Mon Sep 17 00:00:00 2001 From: huangsimin Date: Mon, 15 Jul 2019 19:09:32 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B7=AE=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../usergw/biz/filters/VerifyFilter.java | 71 +++++++------- .../factory/DubboGatewayFilterFactory.java | 95 +++++++++++-------- .../src/main/resources/test-gateway2.yaml | 8 ++ 3 files changed, 101 insertions(+), 73 deletions(-) diff --git a/usergw-service/src/main/java/cn/ecpark/service/usergw/biz/filters/VerifyFilter.java b/usergw-service/src/main/java/cn/ecpark/service/usergw/biz/filters/VerifyFilter.java index 99c4335..dee73a7 100644 --- a/usergw-service/src/main/java/cn/ecpark/service/usergw/biz/filters/VerifyFilter.java +++ b/usergw-service/src/main/java/cn/ecpark/service/usergw/biz/filters/VerifyFilter.java @@ -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 filter(ServerWebExchange exchange, GatewayFilterChain chain) { + // @Override + // public Mono 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; + // } } \ No newline at end of file diff --git a/usergw-service/src/main/java/cn/ecpark/service/usergw/biz/filters/factory/DubboGatewayFilterFactory.java b/usergw-service/src/main/java/cn/ecpark/service/usergw/biz/filters/factory/DubboGatewayFilterFactory.java index bc93930..a0ae60a 100644 --- a/usergw-service/src/main/java/cn/ecpark/service/usergw/biz/filters/factory/DubboGatewayFilterFactory.java +++ b/usergw-service/src/main/java/cn/ecpark/service/usergw/biz/filters/factory/DubboGatewayFilterFactory.java @@ -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 { +public class DubboGatewayFilterFactory extends AbstractGatewayFilterFactory { @Autowired private ApplicationContext appContext; @@ -45,8 +52,6 @@ public class DubboGatewayFilterFactory extends AbstractGatewayFilterFactory> mehtods = new HashMap<>(); - - public DubboGatewayFilterFactory() { super(Config.class); } @@ -58,7 +63,8 @@ public class DubboGatewayFilterFactory extends AbstractGatewayFilterFactory emptyList = new LinkedList(); - public DubboGatewayFilter(Config config) { - this.config = config; - } + public DubboGatewayFilter(Config config) { + this.config = config; + } + + @Override + // @SuppressWarnings("unchecked") + public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) { + + // TODO: 侵入私有变量, 但是这样不如直接改源码了, 效率低. + // try { + + // Field filtersField = chain.getClass().getDeclaredField("filters"); + // filtersField.setAccessible(true); + // ArrayList filters = + // (ArrayList)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 filter(ServerWebExchange exchange, GatewayFilterChain chain) { - - return chain.filter(exchange).then(Mono.fromRunnable( ()->{ ServerHttpRequest req = exchange.getRequest(); HttpHeaders headers = req.getHeaders(); - + List methodString = headers.get("method"); List 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 paramTypes; // 判断全部函数允许, 必须带参数类型, 而且要匹配, 否则报错 if (config.dubboUri.charAt(5) == '-') { @@ -108,18 +130,18 @@ public class DubboGatewayFilterFactory extends AbstractGatewayFilterFactory