From b3d4e28575f96b17759bc9afad8d585e07864e20 Mon Sep 17 00:00:00 2001
From: fangzhipeng <124746406@qq.com>
Date: Fri, 16 Nov 2018 19:03:05 +0800
Subject: [PATCH] gateway filter
---
sc-f-gateway-filter/.idea/compiler.xml | 2 +-
sc-f-gateway-filter/.idea/uiDesigner.xml | 124 +++++
sc-f-gateway-filter/.idea/workspace.xml | 513 ++++++++++++------
sc-f-gateway-filter/sc-f-gateway-filter.iml | 2 +-
.../src/main/java/gateway/Application.java | 47 +-
.../src/main/java/gateway/ElapsedFilter.java | 42 ++
.../gateway/ElapsedGatewayFilterFactory.java | 74 +++
.../src/main/java/gateway/TokenFilter.java | 39 ++
.../src/main/resources/application.yml | 46 +-
.../target/classes/application.yml | 84 ---
.../target/classes/gateway/Application.class | Bin 736 -> 0 bytes
sc-f-gateway-first-sight/.idea/workspace.xml | 70 ++-
12 files changed, 726 insertions(+), 317 deletions(-)
create mode 100644 sc-f-gateway-filter/.idea/uiDesigner.xml
create mode 100644 sc-f-gateway-filter/src/main/java/gateway/ElapsedFilter.java
create mode 100644 sc-f-gateway-filter/src/main/java/gateway/ElapsedGatewayFilterFactory.java
create mode 100644 sc-f-gateway-filter/src/main/java/gateway/TokenFilter.java
delete mode 100644 sc-f-gateway-filter/target/classes/application.yml
delete mode 100644 sc-f-gateway-filter/target/classes/gateway/Application.class
diff --git a/sc-f-gateway-filter/.idea/compiler.xml b/sc-f-gateway-filter/.idea/compiler.xml
index b5406d6..c21be92 100644
--- a/sc-f-gateway-filter/.idea/compiler.xml
+++ b/sc-f-gateway-filter/.idea/compiler.xml
@@ -10,7 +10,7 @@
-
+
\ No newline at end of file
diff --git a/sc-f-gateway-filter/.idea/uiDesigner.xml b/sc-f-gateway-filter/.idea/uiDesigner.xml
new file mode 100644
index 0000000..e96534f
--- /dev/null
+++ b/sc-f-gateway-filter/.idea/uiDesigner.xml
@@ -0,0 +1,124 @@
+
+
+
+
+ -
+
+
+ -
+
+
+ -
+
+
+ -
+
+
+ -
+
+
+
+
+
+ -
+
+
+
+
+
+ -
+
+
+
+
+
+ -
+
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+ -
+
+
+ -
+
+
+ -
+
+
+ -
+
+
+
+
+ -
+
+
+ -
+
+
+
+
+
\ No newline at end of file
diff --git a/sc-f-gateway-filter/.idea/workspace.xml b/sc-f-gateway-filter/.idea/workspace.xml
index 4d7fc9a..d8c3603 100644
--- a/sc-f-gateway-filter/.idea/workspace.xml
+++ b/sc-f-gateway-filter/.idea/workspace.xml
@@ -12,11 +12,11 @@
-
-
+
+
-
-
+
+
@@ -24,8 +24,8 @@
-
-
+
+
@@ -33,11 +33,92 @@
-
-
+
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -45,6 +126,13 @@
+
+
+
after_route
@@ -66,9 +154,13 @@
+
+
+
-
+
+
@@ -92,8 +184,7 @@
-
-
+
@@ -102,8 +193,7 @@
-
-
+
@@ -144,11 +234,85 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
+
@@ -243,50 +407,51 @@
+
-
+
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
@@ -296,104 +461,6 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
@@ -464,20 +531,6 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
@@ -562,13 +615,6 @@
-
-
-
-
-
-
-
@@ -594,14 +640,6 @@
-
-
-
-
-
-
-
-
@@ -647,21 +685,168 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
-
-
+
-
-
+
+
@@ -717,7 +902,7 @@
- sc-f-gateway-predicate
+ sc-f-gateway-filter
diff --git a/sc-f-gateway-filter/sc-f-gateway-filter.iml b/sc-f-gateway-filter/sc-f-gateway-filter.iml
index 24f0bca..3b4b571 100644
--- a/sc-f-gateway-filter/sc-f-gateway-filter.iml
+++ b/sc-f-gateway-filter/sc-f-gateway-filter.iml
@@ -5,7 +5,7 @@
-
+
diff --git a/sc-f-gateway-filter/src/main/java/gateway/Application.java b/sc-f-gateway-filter/src/main/java/gateway/Application.java
index f6c92fa..70a5931 100644
--- a/sc-f-gateway-filter/src/main/java/gateway/Application.java
+++ b/sc-f-gateway-filter/src/main/java/gateway/Application.java
@@ -1,16 +1,11 @@
package gateway;
-import reactor.core.publisher.Mono;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
-import org.springframework.cloud.gateway.route.RouteLocator;
-import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
-import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
-// tag::code[]
@SpringBootApplication
@RestController
public class Application {
@@ -21,29 +16,29 @@ public class Application {
// @Bean
-// public RouteLocator myRoutes(RouteLocatorBuilder builder) {
-// String httpUri = "http://httpbin.org:80";
+// public RouteLocator customerRouteLocator(RouteLocatorBuilder builder) {
+//
// return builder.routes()
-// .route(p -> p
-// .path("/get")
-// .filters(f -> f.addRequestHeader("Hello", "World"))
-// .uri(httpUri))
-// .route(p -> p
-// .host("*.hystrix.com")
-// .filters(f -> f
-// .hystrix(config -> config
-// .setName("mycmd")
-// .setFallbackUri("forward:/fallback")))
-// .uri(httpUri))
-// .build();
-// }
- // end::route-locator[]
-
- // tag::fallback[]
-// @RequestMapping("/fallback")
-// public Mono fallback() {
-// return Mono.just("fallback");
+// .route(r -> r.path("/customer/**")
+// .filters(f -> f.filter(new ElapsedFilter())
+// .addResponseHeader("X-Response-Default-Foo", "Default-Bar"))
+// .uri("http://httpbin.org:80/get")
+// .order(0)
+// .id("customer_filter_router")
+// )
+// .build();
+//
// }
+// @Bean
+// public TokenFilter tokenFilter(){
+// return new TokenFilter();
+// }
+
+
+ @Bean
+ public ElapsedGatewayFilterFactory elapsedGatewayFilterFactory() {
+ return new ElapsedGatewayFilterFactory();
+ }
}
diff --git a/sc-f-gateway-filter/src/main/java/gateway/ElapsedFilter.java b/sc-f-gateway-filter/src/main/java/gateway/ElapsedFilter.java
new file mode 100644
index 0000000..2139976
--- /dev/null
+++ b/sc-f-gateway-filter/src/main/java/gateway/ElapsedFilter.java
@@ -0,0 +1,42 @@
+package gateway;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.springframework.cloud.gateway.filter.GatewayFilter;
+import org.springframework.cloud.gateway.filter.GatewayFilterChain;
+import org.springframework.core.Ordered;
+import org.springframework.web.server.ServerWebExchange;
+import reactor.core.publisher.Mono;
+
+/**
+ * Email miles02@163.com
+ *
+ * @author fangzhipeng
+ * create 2018-11-16
+ **/
+public class ElapsedFilter implements GatewayFilter, Ordered {
+
+ private static final Log log = LogFactory.getLog(GatewayFilter.class);
+ private static final String ELAPSED_TIME_BEGIN = "elapsedTimeBegin";
+
+
+ @Override
+ public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) {
+
+ exchange.getAttributes().put(ELAPSED_TIME_BEGIN, System.currentTimeMillis());
+ return chain.filter(exchange).then(
+ Mono.fromRunnable(() -> {
+ Long startTime = exchange.getAttribute(ELAPSED_TIME_BEGIN);
+ if (startTime != null) {
+ log.info(exchange.getRequest().getURI().getRawPath() + ": " + (System.currentTimeMillis() - startTime) + "ms");
+ }
+ })
+ );
+
+ }
+
+ @Override
+ public int getOrder() {
+ return 0;
+ }
+}
diff --git a/sc-f-gateway-filter/src/main/java/gateway/ElapsedGatewayFilterFactory.java b/sc-f-gateway-filter/src/main/java/gateway/ElapsedGatewayFilterFactory.java
new file mode 100644
index 0000000..d8a1aa5
--- /dev/null
+++ b/sc-f-gateway-filter/src/main/java/gateway/ElapsedGatewayFilterFactory.java
@@ -0,0 +1,74 @@
+package gateway;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.springframework.cloud.gateway.filter.GatewayFilter;
+import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
+import reactor.core.publisher.Mono;
+
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * Email miles02@163.com
+ *
+ * @author fangzhipeng
+ * create 2018-11-16
+ **/
+public class ElapsedGatewayFilterFactory extends AbstractGatewayFilterFactory {
+
+
+ private static final Log log = LogFactory.getLog(GatewayFilter.class);
+ private static final String ELAPSED_TIME_BEGIN = "elapsedTimeBegin";
+ private static final String KEY = "withParams";
+
+ @Override
+ public List shortcutFieldOrder() {
+ return Arrays.asList(KEY);
+ }
+
+ public ElapsedGatewayFilterFactory() {
+ super(Config.class);
+ }
+
+ @Override
+ public GatewayFilter apply(Config config) {
+ return (exchange, chain) -> {
+ exchange.getAttributes().put(ELAPSED_TIME_BEGIN, System.currentTimeMillis());
+ return chain.filter(exchange).then(
+ Mono.fromRunnable(() -> {
+ Long startTime = exchange.getAttribute(ELAPSED_TIME_BEGIN);
+ if (startTime != null) {
+ StringBuilder sb = new StringBuilder(exchange.getRequest().getURI().getRawPath())
+ .append(": ")
+ .append(System.currentTimeMillis() - startTime)
+ .append("ms");
+ if (config.isWithParams()) {
+ sb.append(" params:").append(exchange.getRequest().getQueryParams());
+ }
+ log.info(sb.toString());
+ }
+ })
+ );
+ };
+ }
+
+
+
+
+
+
+ public static class Config {
+
+ private boolean withParams;
+
+ public boolean isWithParams() {
+ return withParams;
+ }
+
+ public void setWithParams(boolean withParams) {
+ this.withParams = withParams;
+ }
+
+ }
+}
diff --git a/sc-f-gateway-filter/src/main/java/gateway/TokenFilter.java b/sc-f-gateway-filter/src/main/java/gateway/TokenFilter.java
new file mode 100644
index 0000000..b250f91
--- /dev/null
+++ b/sc-f-gateway-filter/src/main/java/gateway/TokenFilter.java
@@ -0,0 +1,39 @@
+package gateway;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.cloud.gateway.filter.GatewayFilterChain;
+import org.springframework.cloud.gateway.filter.GlobalFilter;
+import org.springframework.core.Ordered;
+import org.springframework.http.HttpStatus;
+import org.springframework.web.server.ServerWebExchange;
+import reactor.core.publisher.Mono;
+
+
+
+/**
+ * Email miles02@163.com
+ *
+ * @author fangzhipeng
+ * create 2018-11-16
+ **/
+public class TokenFilter implements GlobalFilter, Ordered {
+
+ Logger logger=LoggerFactory.getLogger( TokenFilter.class );
+ @Override
+ public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) {
+ String token = exchange.getRequest().getQueryParams().getFirst("token");
+ if (token == null || token.isEmpty()) {
+ logger.info( "token is empty..." );
+ exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
+ return exchange.getResponse().setComplete();
+ }
+ return chain.filter(exchange);
+ }
+
+ @Override
+ public int getOrder() {
+ return -100;
+ }
+}
+
diff --git a/sc-f-gateway-filter/src/main/resources/application.yml b/sc-f-gateway-filter/src/main/resources/application.yml
index cfe16c4..be3616a 100644
--- a/sc-f-gateway-filter/src/main/resources/application.yml
+++ b/sc-f-gateway-filter/src/main/resources/application.yml
@@ -2,17 +2,57 @@ server:
port: 8081
spring:
profiles:
- active: after_route
+ active: add_request_header_route
---
spring:
cloud:
gateway:
routes:
- - id: after_route
+ - id: add_request_header_route
uri: http://httpbin.org:80/get
+ filters:
+ - AddRequestHeader=X-Request-Foo, Bar
predicates:
- After=2017-01-20T17:42:47.789-07:00[America/Denver]
- profiles: after_route
+ profiles: add_request_header_route
+
+---
+spring:
+ cloud:
+ gateway:
+ routes:
+ - id: add_response_header_route
+ uri: http://httpbin.org:80/get
+ filters:
+ - AddResponseHeader=X-Response-Foo, Bar
+ predicates:
+ - After=2017-01-20T17:42:47.789-07:00[America/Denver]
+ profiles: add_response_header_route
+---
+spring:
+ cloud:
+ gateway:
+ routes:
+ - id: rewritepath_route
+ uri: https://blog.csdn.net
+ predicates:
+ - Path=/foo/**
+ filters:
+ - RewritePath=/foo/(?.*), /$\{segment}
+ profiles: rewritepath_route
+
+---
+spring:
+ cloud:
+ gateway:
+ routes:
+ - id: elapse_route
+ uri: http://httpbin.org:80/get
+ filters:
+ - Elapsed=false
+ predicates:
+ - After=2017-01-20T17:42:47.789-07:00[America/Denver]
+ profiles: elapse_route
diff --git a/sc-f-gateway-filter/target/classes/application.yml b/sc-f-gateway-filter/target/classes/application.yml
deleted file mode 100644
index 99fd3fb..0000000
--- a/sc-f-gateway-filter/target/classes/application.yml
+++ /dev/null
@@ -1,84 +0,0 @@
-server:
- port: 8081
-spring:
- profiles:
- active: query_route
-
----
-spring:
- cloud:
- gateway:
- routes:
- - id: after_route
- uri: http://httpbin.org:80/get
- predicates:
- - After=2017-01-20T17:42:47.789-07:00[America/Denver]
- profiles: after_route
-
----
-spring:
- cloud:
- gateway:
- routes:
- - id: header_route
- uri: http://httpbin.org:80/get
- predicates:
- - Header=X-Request-Id, \d+
- profiles: header_route
-
----
-spring:
- cloud:
- gateway:
- routes:
- - id: cookie_route
- uri: http://httpbin.org:80/get
- predicates:
- - Cookie=name, forezp
- profiles: cookie_route
-
----
-spring:
- cloud:
- gateway:
- routes:
- - id: host_route
- uri: http://httpbin.org:80/get
- predicates:
- - Host=**.fangzhipeng.com
- profiles: host_route
-
-
----
-spring:
- cloud:
- gateway:
- routes:
- - id: method_route
- uri: http://httpbin.org:80/get
- predicates:
- - Method=GET
- profiles: method_route
-
----
-spring:
- cloud:
- gateway:
- routes:
- - id: path_route
- uri: http://httpbin.org:80/get
- predicates:
- - Path=/foo/{segment}
- profiles: path_route
-
----
-
-spring:
- cloud:
- gateway:
- routes:
- - id: query_route
- uri: http://httpbin.org:80/get
- predicates:
- - Query=foo, ba.
- profiles: query_route
\ No newline at end of file
diff --git a/sc-f-gateway-filter/target/classes/gateway/Application.class b/sc-f-gateway-filter/target/classes/gateway/Application.class
deleted file mode 100644
index 99f5bc566a6d8d2f4e13809a3727a48d8e3f2f2c..0000000000000000000000000000000000000000
GIT binary patch
literal 0
HcmV?d00001
literal 736
zcmZ`%%TC)s6g?9H2`1%{@GNxEUC5%dsSuJYB#6|NRU}fSi>}6Ia0i^3XgntHS*)nK
z=m+poh&u*>7siWwbzkS)d&Uos|NaB`icbNW*l1xhz!tV!*kM@srnGWL40HYcF++1`
z&LzW2q_q4|Ok!z&ia4W8H!_LH#=po#zGwSfXf=;t!PZ!v`f8(C#({e=+y#Z9bq1(4(&P3Pu~tSfPA_kiA_)
Mm%Q)okzWU%0J1Q_C;$Ke
diff --git a/sc-f-gateway-first-sight/.idea/workspace.xml b/sc-f-gateway-first-sight/.idea/workspace.xml
index 93fb726..39ef404 100644
--- a/sc-f-gateway-first-sight/.idea/workspace.xml
+++ b/sc-f-gateway-first-sight/.idea/workspace.xml
@@ -25,7 +25,7 @@
-
+
@@ -33,7 +33,7 @@
-
+
@@ -69,7 +69,7 @@
-
+
@@ -78,7 +78,7 @@
-
+
@@ -162,8 +162,8 @@
-
+
@@ -179,7 +179,7 @@
-
+
@@ -270,43 +270,45 @@
+
-
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
+
+
@@ -320,6 +322,13 @@
+
+
+
+
+
+
+
@@ -492,10 +501,6 @@
-
-
-
-
@@ -562,21 +567,10 @@
-
-
-
-
-
-
-
-
-
-
-
@@ -631,14 +625,14 @@
-
+
-
+
@@ -659,7 +653,7 @@
-
+
@@ -681,7 +675,7 @@
-
+