diff --git a/pom.xml b/pom.xml index f9ec608..0f1be9f 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.SR5 @@ -33,6 +33,12 @@ org.springframework.cloud spring-cloud-starter + + + org.springframework.boot + spring-boot-starter-validation + + org.springframework.boot @@ -87,44 +93,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 - - - - - 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 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..b321a4e --- /dev/null +++ b/src/main/java/com/example/demogateway/RestAPI.java @@ -0,0 +1,22 @@ +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; + +@RestController +public class RestAPI { + + @RequestMapping("/hystrixfallback") + 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 new file mode 100644 index 0000000..0e3450d --- /dev/null +++ b/src/main/java/com/example/demogateway/Routing.java @@ -0,0 +1,32 @@ +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(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")) + .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); + } +} diff --git a/src/test/java/com/example/demogateway/DemogatewayApplicationTests.java b/src/test/java/com/example/demogateway/DemogatewayApplicationTests.java index 2bae209..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 @@ -40,6 +44,18 @@ public void pathRouteWorks() { }); } + @Test + @SuppressWarnings("unchecked") + public void helloPathRouteWorks() { + staticClient.get().uri("/hello") + .exchange() + .expectStatus().isOk() + .expectBody(Map.class) + .consumeWith(result -> { + assertThat(result.getResponseBody()).isNotEmpty(); + }); + } + @Test @SuppressWarnings("unchecked") public void hostRouteWorks() {