Merge pull request #3922 from its-fpereira/BAEL-1505
BAEL-1505: Spring WebFlux Filters
This commit is contained in:
commit
ecca8c9a89
|
@ -0,0 +1,15 @@
|
|||
package com.baeldung.reactive.controller;
|
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@RestController
|
||||
public class UserController {
|
||||
|
||||
@GetMapping(path = "/users/{name}")
|
||||
public Mono<String> getName(@PathVariable String name) {
|
||||
return Mono.just(name);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package com.baeldung.reactive.filter;
|
||||
|
||||
import org.springframework.web.reactive.function.server.HandlerFilterFunction;
|
||||
import org.springframework.web.reactive.function.server.HandlerFunction;
|
||||
import org.springframework.web.reactive.function.server.ServerRequest;
|
||||
import org.springframework.web.reactive.function.server.ServerResponse;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import static org.springframework.http.HttpStatus.FORBIDDEN;
|
||||
|
||||
public class ExampleHandlerFilterFunction implements HandlerFilterFunction {
|
||||
|
||||
@Override
|
||||
public Mono filter(ServerRequest serverRequest, HandlerFunction handlerFunction) {
|
||||
if (serverRequest.pathVariable("name").equalsIgnoreCase("test")) {
|
||||
return ServerResponse.status(FORBIDDEN).build();
|
||||
}
|
||||
return handlerFunction.handle(serverRequest);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
package com.baeldung.reactive.filter;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import org.springframework.web.server.WebFilter;
|
||||
import org.springframework.web.server.WebFilterChain;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@Component
|
||||
public class ExampleWebFilter implements WebFilter {
|
||||
|
||||
@Override
|
||||
public Mono<Void> filter(ServerWebExchange serverWebExchange, WebFilterChain webFilterChain) {
|
||||
serverWebExchange.getResponse().getHeaders().add("web-filter", "web-filter-test");
|
||||
return webFilterChain.filter(serverWebExchange);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package com.baeldung.reactive.handler;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.reactive.function.server.ServerRequest;
|
||||
import org.springframework.web.reactive.function.server.ServerResponse;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import static org.springframework.web.reactive.function.server.ServerResponse.ok;
|
||||
|
||||
@Component
|
||||
public class PlayerHandler {
|
||||
|
||||
public Mono<ServerResponse> getName(ServerRequest request) {
|
||||
Mono<String> name = Mono.just(request.pathVariable("name"));
|
||||
return ok().body(name, String.class);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package com.baeldung.reactive.router;
|
||||
|
||||
import com.baeldung.reactive.filter.ExampleHandlerFilterFunction;
|
||||
import com.baeldung.reactive.handler.PlayerHandler;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.reactive.function.server.RouterFunction;
|
||||
import org.springframework.web.reactive.function.server.RouterFunctions;
|
||||
import org.springframework.web.reactive.function.server.ServerResponse;
|
||||
|
||||
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
|
||||
|
||||
@Configuration
|
||||
public class PlayerRouter {
|
||||
|
||||
@Bean
|
||||
public RouterFunction<ServerResponse> route(PlayerHandler playerHandler) {
|
||||
return RouterFunctions
|
||||
.route(GET("/players/{name}"), playerHandler::getName)
|
||||
.filter(new ExampleHandlerFilterFunction()::filter);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
package com.baeldung.reactive.controller;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.web.reactive.server.EntityExchangeResult;
|
||||
import org.springframework.test.web.reactive.server.WebTestClient;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
public class UserControllerTest {
|
||||
|
||||
@Autowired
|
||||
private WebTestClient webTestClient;
|
||||
|
||||
@Test
|
||||
public void whenUserNameIsBaeldung_thenWebFilterIsApplied() {
|
||||
EntityExchangeResult<String> result = webTestClient.get().uri("/users/baeldung")
|
||||
.exchange()
|
||||
.expectStatus().isOk()
|
||||
.expectBody(String.class)
|
||||
.returnResult();
|
||||
|
||||
assertEquals(result.getResponseBody(), "baeldung");
|
||||
assertEquals(result.getResponseHeaders().getFirst("web-filter"), "web-filter-test");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUserNameIsTest_thenHandlerFilterFunctionIsNotApplied() {
|
||||
webTestClient.get().uri("/users/test")
|
||||
.exchange()
|
||||
.expectStatus().isOk();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
package com.baeldung.reactive.handler;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.web.reactive.server.EntityExchangeResult;
|
||||
import org.springframework.test.web.reactive.server.WebTestClient;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
public class PlayerHandlerTest {
|
||||
|
||||
@Autowired
|
||||
private WebTestClient webTestClient;
|
||||
|
||||
@Test
|
||||
public void whenPlayerNameIsBaeldung_thenWebFilterIsApplied() {
|
||||
EntityExchangeResult<String> result = webTestClient.get().uri("/players/baeldung")
|
||||
.exchange()
|
||||
.expectStatus().isOk()
|
||||
.expectBody(String.class)
|
||||
.returnResult();
|
||||
|
||||
assertEquals(result.getResponseBody(), "baeldung");
|
||||
assertEquals(result.getResponseHeaders().getFirst("web-filter"), "web-filter-test");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPlayerNameIsTest_thenHandlerFilterFunctionIsApplied() {
|
||||
webTestClient.get().uri("/players/test")
|
||||
.exchange()
|
||||
.expectStatus().isForbidden();
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue