From 14ff85e322497706aff90bc33342b0fbd0526319 Mon Sep 17 00:00:00 2001 From: Hazar Gul Nazari Date: Mon, 22 Jun 2020 22:23:33 +0200 Subject: [PATCH 1/8] upgrade spring boot version to 2.3.1 and upgrade cloud version to Hoxton.SR3 --- pom.xml | 44 ++------------------------------------------ 1 file changed, 2 insertions(+), 42 deletions(-) diff --git a/pom.xml b/pom.xml index f9ec608..8694766 100644 --- a/pom.xml +++ b/pom.xml @@ -14,7 +14,7 @@ org.springframework.boot spring-boot-starter-parent - 2.0.2.RELEASE + 2.3.1.RELEASE @@ -22,7 +22,7 @@ UTF-8 UTF-8 1.8 - Finchley.RC2 + Hoxton.SR3 @@ -87,44 +87,4 @@ - - - - spring-snapshots - Spring Snapshots - https://repo.spring.io/snapshot - - true - - - - spring-milestones - Spring Milestones - https://repo.spring.io/milestone - - false - - - - - - - spring-snapshots - Spring Snapshots - https://repo.spring.io/snapshot - - true - - - - spring-milestones - Spring Milestones - https://repo.spring.io/milestone - - false - - - - - From b6f373052e0cecc2a3bf468d67eaa90188016a2f Mon Sep 17 00:00:00 2001 From: Hazar Gul Nazari Date: Mon, 22 Jun 2020 22:23:41 +0200 Subject: [PATCH 2/8] Refactor code --- .../demogateway/DemogatewayApplication.java | 57 ------------------- .../java/com/example/demogateway/RestAPI.java | 14 +++++ .../java/com/example/demogateway/Routing.java | 44 ++++++++++++++ .../demogateway/SecurityConfiguration.java | 30 ++++++++++ 4 files changed, 88 insertions(+), 57 deletions(-) create mode 100644 src/main/java/com/example/demogateway/RestAPI.java create mode 100644 src/main/java/com/example/demogateway/Routing.java create mode 100644 src/main/java/com/example/demogateway/SecurityConfiguration.java diff --git a/src/main/java/com/example/demogateway/DemogatewayApplication.java b/src/main/java/com/example/demogateway/DemogatewayApplication.java index 652d87a..7480aa9 100644 --- a/src/main/java/com/example/demogateway/DemogatewayApplication.java +++ b/src/main/java/com/example/demogateway/DemogatewayApplication.java @@ -14,65 +14,8 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; -@RestController @SpringBootApplication public class DemogatewayApplication { - - @RequestMapping("/hystrixfallback") - public String hystrixfallback() { - return "This is a fallback"; - } - - @Bean - public RouteLocator customRouteLocator(RouteLocatorBuilder builder) { - //@formatter:off - return builder.routes() - .route("path_route", r -> r.path("/get") - .uri("http://httpbin.org")) - .route("host_route", r -> r.host("*.myhost.org") - .uri("http://httpbin.org")) - .route("rewrite_route", r -> r.host("*.rewrite.org") - .filters(f -> f.rewritePath("/foo/(?.*)", - "/${segment}")) - .uri("http://httpbin.org")) - .route("hystrix_route", r -> r.host("*.hystrix.org") - .filters(f -> f.hystrix(c -> c.setName("slowcmd"))) - .uri("http://httpbin.org")) - .route("hystrix_fallback_route", r -> r.host("*.hystrixfallback.org") - .filters(f -> f.hystrix(c -> c.setName("slowcmd").setFallbackUri("forward:/hystrixfallback"))) - .uri("http://httpbin.org")) - .route("limit_route", r -> r - .host("*.limited.org").and().path("/anything/**") - .filters(f -> f.requestRateLimiter(c -> c.setRateLimiter(redisRateLimiter()))) - .uri("http://httpbin.org")) - .route("websocket_route", r -> r.path("/echo") - .uri("ws://localhost:9000")) - .build(); - //@formatter:on - } - - @Bean - RedisRateLimiter redisRateLimiter() { - return new RedisRateLimiter(1, 2); - } - - @Bean - SecurityWebFilterChain springWebFilterChain(ServerHttpSecurity http) throws Exception { - return http.httpBasic().and() - .csrf().disable() - .authorizeExchange() - .pathMatchers("/anything/**").authenticated() - .anyExchange().permitAll() - .and() - .build(); - } - - @Bean - public MapReactiveUserDetailsService reactiveUserDetailsService() { - UserDetails user = User.withDefaultPasswordEncoder().username("user").password("password").roles("USER").build(); - return new MapReactiveUserDetailsService(user); - } - public static void main(String[] args) { SpringApplication.run(DemogatewayApplication.class, args); } diff --git a/src/main/java/com/example/demogateway/RestAPI.java b/src/main/java/com/example/demogateway/RestAPI.java new file mode 100644 index 0000000..9b5086a --- /dev/null +++ b/src/main/java/com/example/demogateway/RestAPI.java @@ -0,0 +1,14 @@ +package com.example.demogateway; + +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class RestAPI { + + @RequestMapping("/hystrixfallback") + public String hystrixfallback() { + return "This is a fallback"; + } + +} diff --git a/src/main/java/com/example/demogateway/Routing.java b/src/main/java/com/example/demogateway/Routing.java new file mode 100644 index 0000000..6d1fb26 --- /dev/null +++ b/src/main/java/com/example/demogateway/Routing.java @@ -0,0 +1,44 @@ +package com.example.demogateway; + +import org.springframework.cloud.gateway.filter.ratelimit.RedisRateLimiter; +import org.springframework.cloud.gateway.route.RouteLocator; +import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class Routing { + + @Bean + public RouteLocator customRouteLocator(RouteLocatorBuilder builder) { + //@formatter:off + return builder.routes() + .route("path_route", r -> r.path("/get") + .uri("http://httpbin.org")) + .route("host_route", r -> r.host("*.myhost.org") + .uri("http://httpbin.org")) + .route("rewrite_route", r -> r.host("*.rewrite.org") + .filters(f -> f.rewritePath("/foo/(?.*)", + "/${segment}")) + .uri("http://httpbin.org")) + .route("hystrix_route", r -> r.host("*.hystrix.org") + .filters(f -> f.hystrix(c -> c.setName("slowcmd"))) + .uri("http://httpbin.org")) + .route("hystrix_fallback_route", r -> r.host("*.hystrixfallback.org") + .filters(f -> f.hystrix(c -> c.setName("slowcmd").setFallbackUri("forward:/hystrixfallback"))) + .uri("http://httpbin.org")) + .route("limit_route", r -> r + .host("*.limited.org").and().path("/anything/**") + .filters(f -> f.requestRateLimiter(c -> c.setRateLimiter(redisRateLimiter()))) + .uri("http://httpbin.org")) + .route("websocket_route", r -> r.path("/echo") + .uri("ws://localhost:9000")) + .build(); + //@formatter:on + } + + @Bean + RedisRateLimiter redisRateLimiter() { + return new RedisRateLimiter(1, 2); + } +} diff --git a/src/main/java/com/example/demogateway/SecurityConfiguration.java b/src/main/java/com/example/demogateway/SecurityConfiguration.java new file mode 100644 index 0000000..5a543b2 --- /dev/null +++ b/src/main/java/com/example/demogateway/SecurityConfiguration.java @@ -0,0 +1,30 @@ +package com.example.demogateway; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.config.web.server.ServerHttpSecurity; +import org.springframework.security.core.userdetails.MapReactiveUserDetailsService; +import org.springframework.security.core.userdetails.User; +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.security.web.server.SecurityWebFilterChain; + +@Configuration +public class SecurityConfiguration { + + @Bean + SecurityWebFilterChain springWebFilterChain(ServerHttpSecurity http) throws Exception { + return http.httpBasic().and() + .csrf().disable() + .authorizeExchange() + .pathMatchers("/anything/**").authenticated() + .anyExchange().permitAll() + .and() + .build(); + } + + @Bean + public MapReactiveUserDetailsService reactiveUserDetailsService() { + UserDetails user = User.withDefaultPasswordEncoder().username("user").password("password").roles("USER").build(); + return new MapReactiveUserDetailsService(user); + } +} From a375f7041cedd94e6bbe8320bbcb84858854b235 Mon Sep 17 00:00:00 2001 From: Hazar Gul Nazari Date: Tue, 23 Jun 2020 22:39:37 +0200 Subject: [PATCH 3/8] upgrade cloud version to Hoxton.sr5 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 8694766..0bc732b 100644 --- a/pom.xml +++ b/pom.xml @@ -22,7 +22,7 @@ UTF-8 UTF-8 1.8 - Hoxton.SR3 + Hoxton.SR5 From 78c72cb8a3d54fce204094eccf043a0b87797563 Mon Sep 17 00:00:00 2001 From: Hazar Gul Nazari Date: Tue, 23 Jun 2020 22:39:46 +0200 Subject: [PATCH 4/8] add filter for testing purpose --- src/main/java/com/example/demogateway/CustomFilter.java | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 src/main/java/com/example/demogateway/CustomFilter.java diff --git a/src/main/java/com/example/demogateway/CustomFilter.java b/src/main/java/com/example/demogateway/CustomFilter.java new file mode 100644 index 0000000..028ead3 --- /dev/null +++ b/src/main/java/com/example/demogateway/CustomFilter.java @@ -0,0 +1,5 @@ +//package com.example.demogateway; +// +//public class CustomFilter implements Filter { +//} +//a \ No newline at end of file From 1e8cead9fd8686af496c8350ddfcb9368f798c0d Mon Sep 17 00:00:00 2001 From: Hazar Gul Nazari Date: Tue, 23 Jun 2020 22:39:56 +0200 Subject: [PATCH 5/8] add test for /hello --- .../demogateway/DemogatewayApplicationTests.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/test/java/com/example/demogateway/DemogatewayApplicationTests.java b/src/test/java/com/example/demogateway/DemogatewayApplicationTests.java index 2bae209..096033f 100644 --- a/src/test/java/com/example/demogateway/DemogatewayApplicationTests.java +++ b/src/test/java/com/example/demogateway/DemogatewayApplicationTests.java @@ -40,6 +40,18 @@ public void pathRouteWorks() { }); } + @Test + @SuppressWarnings("unchecked") + public void helloPathRouteWorks() { + client.get().uri("/hello") + .exchange() + .expectStatus().isOk() + .expectBody(Map.class) + .consumeWith(result -> { + assertThat(result.getResponseBody()).isNotEmpty(); + }); + } + @Test @SuppressWarnings("unchecked") public void hostRouteWorks() { From 09b13c424fde3a3e3df4d6aac481eb7f1aa75751 Mon Sep 17 00:00:00 2001 From: Hazar Gul Nazari Date: Tue, 23 Jun 2020 22:40:03 +0200 Subject: [PATCH 6/8] refactor code --- .../java/com/example/demogateway/RestAPI.java | 8 ++++++ .../java/com/example/demogateway/Routing.java | 28 ++++++------------- 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/example/demogateway/RestAPI.java b/src/main/java/com/example/demogateway/RestAPI.java index 9b5086a..b321a4e 100644 --- a/src/main/java/com/example/demogateway/RestAPI.java +++ b/src/main/java/com/example/demogateway/RestAPI.java @@ -1,5 +1,7 @@ package com.example.demogateway; +import org.springframework.http.server.reactive.ServerHttpRequest; +import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -11,4 +13,10 @@ public String hystrixfallback() { return "This is a fallback"; } + @GetMapping("/hello") + public String hello(ServerHttpRequest request){ + request.getHeaders().forEach((key, value) -> System.out.println(key + " " + value)); + return "Hello, from spring boot application"; + } + } diff --git a/src/main/java/com/example/demogateway/Routing.java b/src/main/java/com/example/demogateway/Routing.java index 6d1fb26..73d914d 100644 --- a/src/main/java/com/example/demogateway/Routing.java +++ b/src/main/java/com/example/demogateway/Routing.java @@ -13,26 +13,14 @@ public class Routing { public RouteLocator customRouteLocator(RouteLocatorBuilder builder) { //@formatter:off return builder.routes() - .route("path_route", r -> r.path("/get") - .uri("http://httpbin.org")) - .route("host_route", r -> r.host("*.myhost.org") - .uri("http://httpbin.org")) - .route("rewrite_route", r -> r.host("*.rewrite.org") - .filters(f -> f.rewritePath("/foo/(?.*)", - "/${segment}")) - .uri("http://httpbin.org")) - .route("hystrix_route", r -> r.host("*.hystrix.org") - .filters(f -> f.hystrix(c -> c.setName("slowcmd"))) - .uri("http://httpbin.org")) - .route("hystrix_fallback_route", r -> r.host("*.hystrixfallback.org") - .filters(f -> f.hystrix(c -> c.setName("slowcmd").setFallbackUri("forward:/hystrixfallback"))) - .uri("http://httpbin.org")) - .route("limit_route", r -> r - .host("*.limited.org").and().path("/anything/**") - .filters(f -> f.requestRateLimiter(c -> c.setRateLimiter(redisRateLimiter()))) - .uri("http://httpbin.org")) - .route("websocket_route", r -> r.path("/echo") - .uri("ws://localhost:9000")) +// .route(p -> p.path("/hello").filters(f -> f.addRequestHeader("hello", "world")).uri("http://localhost:8080")) + .route("path_route", r -> r.path("/get").uri("http://httpbin.org")) + .route("host_route", r -> r.host("*.myhost.org").uri("http://httpbin.org")) + .route("rewrite_route", r -> r.host("*.rewrite.org").filters(f -> f.rewritePath("/foo/(?.*)","/${segment}")).uri("http://httpbin.org")) + .route("hystrix_route", r -> r.host("*.hystrix.org").filters(f -> f.hystrix(c -> c.setName("slowcmd"))).uri("http://httpbin.org")) + .route("hystrix_fallback_route", r -> r.host("*.hystrixfallback.org").filters(f -> f.hystrix(c -> c.setName("slowcmd").setFallbackUri("forward:/hystrixfallback"))).uri("http://httpbin.org")) + .route("limit_route", r -> r.host("*.limited.org").and().path("/anything/**").filters(f -> f.requestRateLimiter(c -> c.setRateLimiter(redisRateLimiter()))).uri("http://httpbin.org")) + .route("websocket_route", r -> r.path("/echo").uri("ws://localhost:9000")) .build(); //@formatter:on } From f4d394602e3a47e7368f1b4d55c20c09143f51a3 Mon Sep 17 00:00:00 2001 From: Hazar Gul Nazari Date: Tue, 23 Jun 2020 22:54:44 +0200 Subject: [PATCH 7/8] remove validator --- pom.xml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pom.xml b/pom.xml index 0bc732b..0f1be9f 100644 --- a/pom.xml +++ b/pom.xml @@ -33,6 +33,12 @@ org.springframework.cloud spring-cloud-starter + + + org.springframework.boot + spring-boot-starter-validation + + org.springframework.boot From ff1481dbc2d128304018290611199f87a506cdd3 Mon Sep 17 00:00:00 2001 From: Hazar Gul Nazari Date: Tue, 23 Jun 2020 22:54:55 +0200 Subject: [PATCH 8/8] add static client --- src/main/java/com/example/demogateway/Routing.java | 2 +- .../example/demogateway/DemogatewayApplicationTests.java | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/example/demogateway/Routing.java b/src/main/java/com/example/demogateway/Routing.java index 73d914d..0e3450d 100644 --- a/src/main/java/com/example/demogateway/Routing.java +++ b/src/main/java/com/example/demogateway/Routing.java @@ -13,7 +13,7 @@ public class Routing { public RouteLocator customRouteLocator(RouteLocatorBuilder builder) { //@formatter:off return builder.routes() -// .route(p -> p.path("/hello").filters(f -> f.addRequestHeader("hello", "world")).uri("http://localhost:8080")) + .route(p -> p.path("/hello").filters(f -> f.addRequestHeader("Hello", "World")).uri("http://httpbin.org:80")) .route("path_route", r -> r.path("/get").uri("http://httpbin.org")) .route("host_route", r -> r.host("*.myhost.org").uri("http://httpbin.org")) .route("rewrite_route", r -> r.host("*.rewrite.org").filters(f -> f.rewritePath("/foo/(?.*)","/${segment}")).uri("http://httpbin.org")) diff --git a/src/test/java/com/example/demogateway/DemogatewayApplicationTests.java b/src/test/java/com/example/demogateway/DemogatewayApplicationTests.java index 096033f..cc54f20 100644 --- a/src/test/java/com/example/demogateway/DemogatewayApplicationTests.java +++ b/src/test/java/com/example/demogateway/DemogatewayApplicationTests.java @@ -23,9 +23,13 @@ public class DemogatewayApplicationTests { int port; private WebTestClient client; + int staticPort = 8080; + private WebTestClient staticClient; + @Before public void setup() { client = WebTestClient.bindToServer().baseUrl("http://localhost:" + port).build(); + staticClient = WebTestClient.bindToServer().baseUrl("http://localhost:" + staticPort).build(); } @Test @@ -43,7 +47,7 @@ public void pathRouteWorks() { @Test @SuppressWarnings("unchecked") public void helloPathRouteWorks() { - client.get().uri("/hello") + staticClient.get().uri("/hello") .exchange() .expectStatus().isOk() .expectBody(Map.class)