Skip to content

Commit 07b6699

Browse files
committed
ServerWebExchangeReactorContextWebFilter
Fixes: gh-5779
1 parent 65c81ce commit 07b6699

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

config/src/main/java/org/springframework/security/config/web/server/ServerHttpSecurity.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@
133133
import org.springframework.web.server.ServerWebExchange;
134134
import org.springframework.web.server.WebFilter;
135135
import org.springframework.web.server.WebFilterChain;
136+
import reactor.util.context.Context;
136137

137138
import static org.springframework.security.web.server.DelegatingServerAuthenticationEntryPoint.DelegateEntry;
138139
import static org.springframework.security.web.server.util.matcher.ServerWebExchangeMatcher.MatchResult.match;
@@ -1098,6 +1099,7 @@ public SecurityWebFilterChain build() {
10981099
}
10991100
sortedWebFilters.add(f);
11001101
});
1102+
sortedWebFilters.add(0, new ServerWebExchangeReactorContextWebFilter());
11011103
return new MatcherSecurityWebFilterChain(getSecurityMatcher(), sortedWebFilters);
11021104
}
11031105

@@ -2191,4 +2193,15 @@ public String toString() {
21912193
+ '}';
21922194
}
21932195
}
2196+
2197+
/**
2198+
* Workaround https://jira.spring.io/projects/SPR/issues/SPR-17213
2199+
*/
2200+
static class ServerWebExchangeReactorContextWebFilter implements WebFilter {
2201+
@Override
2202+
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
2203+
return chain.filter(exchange)
2204+
.subscriberContext(Context.of(ServerWebExchange.class, exchange));
2205+
}
2206+
}
21942207
}

config/src/test/java/org/springframework/security/config/web/server/ServerHttpSecurityTests.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,16 @@
2727
import org.springframework.security.config.annotation.web.reactive.ServerHttpSecurityConfigurationBuilder;
2828
import org.springframework.security.core.context.SecurityContext;
2929
import org.springframework.security.test.web.reactive.server.WebTestClientBuilder;
30+
import org.springframework.security.web.server.SecurityWebFilterChain;
3031
import org.springframework.security.web.server.WebFilterChainProxy;
3132
import org.springframework.security.web.server.context.ServerSecurityContextRepository;
3233
import org.springframework.security.web.server.context.WebSessionServerSecurityContextRepository;
3334
import org.springframework.test.web.reactive.server.EntityExchangeResult;
3435
import org.springframework.test.web.reactive.server.FluxExchangeResult;
3536
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;
3640
import reactor.core.publisher.Mono;
3741
import reactor.test.publisher.TestPublisher;
3842

@@ -117,9 +121,33 @@ public void basicWhenNoCredentialsThenUnauthorized() {
117121
.expectBody().isEmpty();
118122
}
119123

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+
120137
private WebTestClient buildClient() {
121138
WebFilterChainProxy springSecurityFilterChain = new WebFilterChainProxy(
122139
this.http.build());
123140
return WebTestClientBuilder.bindToWebFilters(springSecurityFilterChain).build();
124141
}
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+
}
125153
}

0 commit comments

Comments
 (0)