|
27 | 27 | import org.springframework.security.config.annotation.web.reactive.ServerHttpSecurityConfigurationBuilder;
|
28 | 28 | import org.springframework.security.core.context.SecurityContext;
|
29 | 29 | import org.springframework.security.test.web.reactive.server.WebTestClientBuilder;
|
| 30 | +import org.springframework.security.web.server.SecurityWebFilterChain; |
30 | 31 | import org.springframework.security.web.server.WebFilterChainProxy;
|
31 | 32 | import org.springframework.security.web.server.context.ServerSecurityContextRepository;
|
32 | 33 | import org.springframework.security.web.server.context.WebSessionServerSecurityContextRepository;
|
33 | 34 | import org.springframework.test.web.reactive.server.EntityExchangeResult;
|
34 | 35 | import org.springframework.test.web.reactive.server.FluxExchangeResult;
|
35 | 36 | import org.springframework.test.web.reactive.server.WebTestClient;
|
| 37 | +import org.springframework.web.bind.annotation.GetMapping; |
| 38 | +import org.springframework.web.bind.annotation.RestController; |
| 39 | +import org.springframework.web.server.ServerWebExchange; |
36 | 40 | import reactor.core.publisher.Mono;
|
37 | 41 | import reactor.test.publisher.TestPublisher;
|
38 | 42 |
|
@@ -117,9 +121,33 @@ public void basicWhenNoCredentialsThenUnauthorized() {
|
117 | 121 | .expectBody().isEmpty();
|
118 | 122 | }
|
119 | 123 |
|
| 124 | + @Test |
| 125 | + public void buildWhenServerWebExchangeFromContextThenFound() { |
| 126 | + SecurityWebFilterChain filter = this.http.build(); |
| 127 | + |
| 128 | + WebTestClient client = WebTestClient.bindToController(new SubscriberContextController()) |
| 129 | + .webFilter(new WebFilterChainProxy(filter)) |
| 130 | + .build(); |
| 131 | + |
| 132 | + client.get().uri("/foo/bar") |
| 133 | + .exchange() |
| 134 | + .expectBody(String.class).isEqualTo("/foo/bar"); |
| 135 | + } |
| 136 | + |
120 | 137 | private WebTestClient buildClient() {
|
121 | 138 | WebFilterChainProxy springSecurityFilterChain = new WebFilterChainProxy(
|
122 | 139 | this.http.build());
|
123 | 140 | return WebTestClientBuilder.bindToWebFilters(springSecurityFilterChain).build();
|
124 | 141 | }
|
| 142 | + |
| 143 | + @RestController |
| 144 | + private static class SubscriberContextController { |
| 145 | + @GetMapping("/**") |
| 146 | + Mono<String> pathWithinApplicationFromContext() { |
| 147 | + return Mono.subscriberContext() |
| 148 | + .filter(c -> c.hasKey(ServerWebExchange.class)) |
| 149 | + .map(c -> c.get(ServerWebExchange.class)) |
| 150 | + .map(e -> e.getRequest().getPath().pathWithinApplication().value()); |
| 151 | + } |
| 152 | + } |
125 | 153 | }
|
0 commit comments