Skip to content

Fix #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open

Fix #14

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 8 additions & 42 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
<version>2.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.RC2</spring-cloud.version>
<spring-cloud.version>Hoxton.SR5</spring-cloud.version>
</properties>

<dependencies>
Expand All @@ -33,6 +33,12 @@
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down Expand Up @@ -87,44 +93,4 @@
</plugin>
</plugins>
</build>

<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>

<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>


</project>
5 changes: 5 additions & 0 deletions src/main/java/com/example/demogateway/CustomFilter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//package com.example.demogateway;
//
//public class CustomFilter implements Filter {
//}
//a
57 changes: 0 additions & 57 deletions src/main/java/com/example/demogateway/DemogatewayApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -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>.*)",
"/${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);
}
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/com/example/demogateway/RestAPI.java
Original file line number Diff line number Diff line change
@@ -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";
}

}
32 changes: 32 additions & 0 deletions src/main/java/com/example/demogateway/Routing.java
Original file line number Diff line number Diff line change
@@ -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>.*)","/${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);
}
}
30 changes: 30 additions & 0 deletions src/main/java/com/example/demogateway/SecurityConfiguration.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() {
Expand Down