Skip to content

Commit 806e058

Browse files
Replace removed context-related operators
Closes gh-11194
1 parent b803e84 commit 806e058

File tree

45 files changed

+194
-204
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+194
-204
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3236,7 +3236,7 @@ static class ServerWebExchangeReactorContextWebFilter implements WebFilter {
32363236

32373237
@Override
32383238
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
3239-
return chain.filter(exchange).subscriberContext(Context.of(ServerWebExchange.class, exchange));
3239+
return chain.filter(exchange).contextWrite(Context.of(ServerWebExchange.class, exchange));
32403240
}
32413241

32423242
}

config/src/test/java/org/springframework/security/config/annotation/method/configuration/EnableReactiveMethodSecurityTests.java

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,7 @@ public void monoWhenPermitAllThenSuccess() {
9797
@Test
9898
public void monoPreAuthorizeHasRoleWhenGrantedThenSuccess() {
9999
given(this.delegate.monoPreAuthorizeHasRoleFindById(1L)).willReturn(Mono.just("result"));
100-
Mono<String> findById = this.messageService.monoPreAuthorizeHasRoleFindById(1L)
101-
.subscriberContext(this.withAdmin);
100+
Mono<String> findById = this.messageService.monoPreAuthorizeHasRoleFindById(1L).contextWrite(this.withAdmin);
102101
StepVerifier.create(findById).expectNext("result").verifyComplete();
103102
}
104103

@@ -113,16 +112,15 @@ public void monoPreAuthorizeHasRoleWhenNoAuthenticationThenDenied() {
113112
@Test
114113
public void monoPreAuthorizeHasRoleWhenNotAuthorizedThenDenied() {
115114
given(this.delegate.monoPreAuthorizeHasRoleFindById(1L)).willReturn(Mono.from(this.result));
116-
Mono<String> findById = this.messageService.monoPreAuthorizeHasRoleFindById(1L)
117-
.subscriberContext(this.withUser);
115+
Mono<String> findById = this.messageService.monoPreAuthorizeHasRoleFindById(1L).contextWrite(this.withUser);
118116
StepVerifier.create(findById).expectError(AccessDeniedException.class).verify();
119117
this.result.assertNoSubscribers();
120118
}
121119

122120
@Test
123121
public void monoPreAuthorizeBeanWhenGrantedThenSuccess() {
124122
given(this.delegate.monoPreAuthorizeBeanFindById(2L)).willReturn(Mono.just("result"));
125-
Mono<String> findById = this.messageService.monoPreAuthorizeBeanFindById(2L).subscriberContext(this.withAdmin);
123+
Mono<String> findById = this.messageService.monoPreAuthorizeBeanFindById(2L).contextWrite(this.withAdmin);
126124
StepVerifier.create(findById).expectNext("result").verifyComplete();
127125
}
128126

@@ -144,29 +142,29 @@ public void monoPreAuthorizeBeanWhenNoAuthenticationThenDenied() {
144142
@Test
145143
public void monoPreAuthorizeBeanWhenNotAuthorizedThenDenied() {
146144
given(this.delegate.monoPreAuthorizeBeanFindById(1L)).willReturn(Mono.from(this.result));
147-
Mono<String> findById = this.messageService.monoPreAuthorizeBeanFindById(1L).subscriberContext(this.withUser);
145+
Mono<String> findById = this.messageService.monoPreAuthorizeBeanFindById(1L).contextWrite(this.withUser);
148146
StepVerifier.create(findById).expectError(AccessDeniedException.class).verify();
149147
this.result.assertNoSubscribers();
150148
}
151149

152150
@Test
153151
public void monoPostAuthorizeWhenAuthorizedThenSuccess() {
154152
given(this.delegate.monoPostAuthorizeFindById(1L)).willReturn(Mono.just("user"));
155-
Mono<String> findById = this.messageService.monoPostAuthorizeFindById(1L).subscriberContext(this.withUser);
153+
Mono<String> findById = this.messageService.monoPostAuthorizeFindById(1L).contextWrite(this.withUser);
156154
StepVerifier.create(findById).expectNext("user").verifyComplete();
157155
}
158156

159157
@Test
160158
public void monoPostAuthorizeWhenNotAuthorizedThenDenied() {
161159
given(this.delegate.monoPostAuthorizeBeanFindById(1L)).willReturn(Mono.just("not-authorized"));
162-
Mono<String> findById = this.messageService.monoPostAuthorizeBeanFindById(1L).subscriberContext(this.withUser);
160+
Mono<String> findById = this.messageService.monoPostAuthorizeBeanFindById(1L).contextWrite(this.withUser);
163161
StepVerifier.create(findById).expectError(AccessDeniedException.class).verify();
164162
}
165163

166164
@Test
167165
public void monoPostAuthorizeWhenBeanAndAuthorizedThenSuccess() {
168166
given(this.delegate.monoPostAuthorizeBeanFindById(2L)).willReturn(Mono.just("user"));
169-
Mono<String> findById = this.messageService.monoPostAuthorizeBeanFindById(2L).subscriberContext(this.withUser);
167+
Mono<String> findById = this.messageService.monoPostAuthorizeBeanFindById(2L).contextWrite(this.withUser);
170168
StepVerifier.create(findById).expectNext("user").verifyComplete();
171169
}
172170

@@ -180,7 +178,7 @@ public void monoPostAuthorizeWhenBeanAndNotAuthenticatedAndAuthorizedThenSuccess
180178
@Test
181179
public void monoPostAuthorizeWhenBeanAndNotAuthorizedThenDenied() {
182180
given(this.delegate.monoPostAuthorizeBeanFindById(1L)).willReturn(Mono.just("not-authorized"));
183-
Mono<String> findById = this.messageService.monoPostAuthorizeBeanFindById(1L).subscriberContext(this.withUser);
181+
Mono<String> findById = this.messageService.monoPostAuthorizeBeanFindById(1L).contextWrite(this.withUser);
184182
StepVerifier.create(findById).expectError(AccessDeniedException.class).verify();
185183
}
186184

@@ -201,8 +199,7 @@ public void fluxWhenPermitAllThenSuccess() {
201199
@Test
202200
public void fluxPreAuthorizeHasRoleWhenGrantedThenSuccess() {
203201
given(this.delegate.fluxPreAuthorizeHasRoleFindById(1L)).willReturn(Flux.just("result"));
204-
Flux<String> findById = this.messageService.fluxPreAuthorizeHasRoleFindById(1L)
205-
.subscriberContext(this.withAdmin);
202+
Flux<String> findById = this.messageService.fluxPreAuthorizeHasRoleFindById(1L).contextWrite(this.withAdmin);
206203
StepVerifier.create(findById).consumeNextWith((s) -> assertThat(s).isEqualTo("result")).verifyComplete();
207204
}
208205

@@ -217,16 +214,15 @@ public void fluxPreAuthorizeHasRoleWhenNoAuthenticationThenDenied() {
217214
@Test
218215
public void fluxPreAuthorizeHasRoleWhenNotAuthorizedThenDenied() {
219216
given(this.delegate.fluxPreAuthorizeHasRoleFindById(1L)).willReturn(Flux.from(this.result));
220-
Flux<String> findById = this.messageService.fluxPreAuthorizeHasRoleFindById(1L)
221-
.subscriberContext(this.withUser);
217+
Flux<String> findById = this.messageService.fluxPreAuthorizeHasRoleFindById(1L).contextWrite(this.withUser);
222218
StepVerifier.create(findById).expectError(AccessDeniedException.class).verify();
223219
this.result.assertNoSubscribers();
224220
}
225221

226222
@Test
227223
public void fluxPreAuthorizeBeanWhenGrantedThenSuccess() {
228224
given(this.delegate.fluxPreAuthorizeBeanFindById(2L)).willReturn(Flux.just("result"));
229-
Flux<String> findById = this.messageService.fluxPreAuthorizeBeanFindById(2L).subscriberContext(this.withAdmin);
225+
Flux<String> findById = this.messageService.fluxPreAuthorizeBeanFindById(2L).contextWrite(this.withAdmin);
230226
StepVerifier.create(findById).expectNext("result").verifyComplete();
231227
}
232228

@@ -248,29 +244,29 @@ public void fluxPreAuthorizeBeanWhenNoAuthenticationThenDenied() {
248244
@Test
249245
public void fluxPreAuthorizeBeanWhenNotAuthorizedThenDenied() {
250246
given(this.delegate.fluxPreAuthorizeBeanFindById(1L)).willReturn(Flux.from(this.result));
251-
Flux<String> findById = this.messageService.fluxPreAuthorizeBeanFindById(1L).subscriberContext(this.withUser);
247+
Flux<String> findById = this.messageService.fluxPreAuthorizeBeanFindById(1L).contextWrite(this.withUser);
252248
StepVerifier.create(findById).expectError(AccessDeniedException.class).verify();
253249
this.result.assertNoSubscribers();
254250
}
255251

256252
@Test
257253
public void fluxPostAuthorizeWhenAuthorizedThenSuccess() {
258254
given(this.delegate.fluxPostAuthorizeFindById(1L)).willReturn(Flux.just("user"));
259-
Flux<String> findById = this.messageService.fluxPostAuthorizeFindById(1L).subscriberContext(this.withUser);
255+
Flux<String> findById = this.messageService.fluxPostAuthorizeFindById(1L).contextWrite(this.withUser);
260256
StepVerifier.create(findById).expectNext("user").verifyComplete();
261257
}
262258

263259
@Test
264260
public void fluxPostAuthorizeWhenNotAuthorizedThenDenied() {
265261
given(this.delegate.fluxPostAuthorizeBeanFindById(1L)).willReturn(Flux.just("not-authorized"));
266-
Flux<String> findById = this.messageService.fluxPostAuthorizeBeanFindById(1L).subscriberContext(this.withUser);
262+
Flux<String> findById = this.messageService.fluxPostAuthorizeBeanFindById(1L).contextWrite(this.withUser);
267263
StepVerifier.create(findById).expectError(AccessDeniedException.class).verify();
268264
}
269265

270266
@Test
271267
public void fluxPostAuthorizeWhenBeanAndAuthorizedThenSuccess() {
272268
given(this.delegate.fluxPostAuthorizeBeanFindById(2L)).willReturn(Flux.just("user"));
273-
Flux<String> findById = this.messageService.fluxPostAuthorizeBeanFindById(2L).subscriberContext(this.withUser);
269+
Flux<String> findById = this.messageService.fluxPostAuthorizeBeanFindById(2L).contextWrite(this.withUser);
274270
StepVerifier.create(findById).expectNext("user").verifyComplete();
275271
}
276272

@@ -284,7 +280,7 @@ public void fluxPostAuthorizeWhenBeanAndNotAuthenticatedAndAuthorizedThenSuccess
284280
@Test
285281
public void fluxPostAuthorizeWhenBeanAndNotAuthorizedThenDenied() {
286282
given(this.delegate.fluxPostAuthorizeBeanFindById(1L)).willReturn(Flux.just("not-authorized"));
287-
Flux<String> findById = this.messageService.fluxPostAuthorizeBeanFindById(1L).subscriberContext(this.withUser);
283+
Flux<String> findById = this.messageService.fluxPostAuthorizeBeanFindById(1L).contextWrite(this.withUser);
288284
StepVerifier.create(findById).expectError(AccessDeniedException.class).verify();
289285
}
290286

@@ -306,7 +302,7 @@ public void publisherWhenPermitAllThenSuccess() {
306302
public void publisherPreAuthorizeHasRoleWhenGrantedThenSuccess() {
307303
given(this.delegate.publisherPreAuthorizeHasRoleFindById(1L)).willReturn(publisherJust("result"));
308304
Publisher<String> findById = Flux.from(this.messageService.publisherPreAuthorizeHasRoleFindById(1L))
309-
.subscriberContext(this.withAdmin);
305+
.contextWrite(this.withAdmin);
310306
StepVerifier.create(findById).consumeNextWith((s) -> assertThat(s).isEqualTo("result")).verifyComplete();
311307
}
312308

@@ -322,7 +318,7 @@ public void publisherPreAuthorizeHasRoleWhenNoAuthenticationThenDenied() {
322318
public void publisherPreAuthorizeHasRoleWhenNotAuthorizedThenDenied() {
323319
given(this.delegate.publisherPreAuthorizeHasRoleFindById(1L)).willReturn(this.result);
324320
Publisher<String> findById = Flux.from(this.messageService.publisherPreAuthorizeHasRoleFindById(1L))
325-
.subscriberContext(this.withUser);
321+
.contextWrite(this.withUser);
326322
StepVerifier.create(findById).expectError(AccessDeniedException.class).verify();
327323
this.result.assertNoSubscribers();
328324
}
@@ -331,7 +327,7 @@ public void publisherPreAuthorizeHasRoleWhenNotAuthorizedThenDenied() {
331327
public void publisherPreAuthorizeBeanWhenGrantedThenSuccess() {
332328
given(this.delegate.publisherPreAuthorizeBeanFindById(2L)).willReturn(publisherJust("result"));
333329
Publisher<String> findById = Flux.from(this.messageService.publisherPreAuthorizeBeanFindById(2L))
334-
.subscriberContext(this.withAdmin);
330+
.contextWrite(this.withAdmin);
335331
StepVerifier.create(findById).expectNext("result").verifyComplete();
336332
}
337333

@@ -354,7 +350,7 @@ public void publisherPreAuthorizeBeanWhenNoAuthenticationThenDenied() {
354350
public void publisherPreAuthorizeBeanWhenNotAuthorizedThenDenied() {
355351
given(this.delegate.publisherPreAuthorizeBeanFindById(1L)).willReturn(this.result);
356352
Publisher<String> findById = Flux.from(this.messageService.publisherPreAuthorizeBeanFindById(1L))
357-
.subscriberContext(this.withUser);
353+
.contextWrite(this.withUser);
358354
StepVerifier.create(findById).expectError(AccessDeniedException.class).verify();
359355
this.result.assertNoSubscribers();
360356
}
@@ -363,23 +359,23 @@ public void publisherPreAuthorizeBeanWhenNotAuthorizedThenDenied() {
363359
public void publisherPostAuthorizeWhenAuthorizedThenSuccess() {
364360
given(this.delegate.publisherPostAuthorizeFindById(1L)).willReturn(publisherJust("user"));
365361
Publisher<String> findById = Flux.from(this.messageService.publisherPostAuthorizeFindById(1L))
366-
.subscriberContext(this.withUser);
362+
.contextWrite(this.withUser);
367363
StepVerifier.create(findById).expectNext("user").verifyComplete();
368364
}
369365

370366
@Test
371367
public void publisherPostAuthorizeWhenNotAuthorizedThenDenied() {
372368
given(this.delegate.publisherPostAuthorizeBeanFindById(1L)).willReturn(publisherJust("not-authorized"));
373369
Publisher<String> findById = Flux.from(this.messageService.publisherPostAuthorizeBeanFindById(1L))
374-
.subscriberContext(this.withUser);
370+
.contextWrite(this.withUser);
375371
StepVerifier.create(findById).expectError(AccessDeniedException.class).verify();
376372
}
377373

378374
@Test
379375
public void publisherPostAuthorizeWhenBeanAndAuthorizedThenSuccess() {
380376
given(this.delegate.publisherPostAuthorizeBeanFindById(2L)).willReturn(publisherJust("user"));
381377
Publisher<String> findById = Flux.from(this.messageService.publisherPostAuthorizeBeanFindById(2L))
382-
.subscriberContext(this.withUser);
378+
.contextWrite(this.withUser);
383379
StepVerifier.create(findById).expectNext("user").verifyComplete();
384380
}
385381

@@ -394,7 +390,7 @@ public void publisherPostAuthorizeWhenBeanAndNotAuthenticatedAndAuthorizedThenSu
394390
public void publisherPostAuthorizeWhenBeanAndNotAuthorizedThenDenied() {
395391
given(this.delegate.publisherPostAuthorizeBeanFindById(1L)).willReturn(publisherJust("not-authorized"));
396392
Publisher<String> findById = Flux.from(this.messageService.publisherPostAuthorizeBeanFindById(1L))
397-
.subscriberContext(this.withUser);
393+
.contextWrite(this.withUser);
398394
StepVerifier.create(findById).expectError(AccessDeniedException.class).verify();
399395
}
400396

config/src/test/java/org/springframework/security/config/annotation/web/configuration/SecurityReactorContextConfigurationTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ public void createPublisherWhenLastOperatorAddedThenSecurityContextAttributesAva
199199
SecurityContextHolder.getContext().setAuthentication(this.authentication);
200200
ClientResponse clientResponseOk = ClientResponse.create(HttpStatus.OK).build();
201201
// @formatter:off
202-
ExchangeFilterFunction filter = (req, next) -> Mono.subscriberContext()
202+
ExchangeFilterFunction filter = (req, next) -> Mono.deferContextual(Mono::just)
203203
.filter((ctx) -> ctx.hasKey(SecurityReactorContextSubscriber.SECURITY_CONTEXT_ATTRIBUTES))
204204
.map((ctx) -> ctx.get(SecurityReactorContextSubscriber.SECURITY_CONTEXT_ATTRIBUTES))
205205
.cast(Map.class)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ private static class SubscriberContextController {
557557

558558
@GetMapping("/**")
559559
Mono<String> pathWithinApplicationFromContext() {
560-
return Mono.subscriberContext().filter((c) -> c.hasKey(ServerWebExchange.class))
560+
return Mono.deferContextual(Mono::just).filter((c) -> c.hasKey(ServerWebExchange.class))
561561
.map((c) -> c.get(ServerWebExchange.class))
562562
.map((e) -> e.getRequest().getPath().pathWithinApplication().value());
563563
}

core/src/main/java/org/springframework/security/core/context/ReactiveSecurityContextHolder.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ private ReactiveSecurityContextHolder() {
4242
*/
4343
public static Mono<SecurityContext> getContext() {
4444
// @formatter:off
45-
return Mono.subscriberContext()
45+
return Mono.deferContextual(Mono::just)
46+
.cast(Context.class)
4647
.filter(ReactiveSecurityContextHolder::hasSecurityContext)
4748
.flatMap(ReactiveSecurityContextHolder::getSecurityContext);
4849
// @formatter:on

core/src/test/java/org/springframework/security/core/context/ReactiveSecurityContextHolderTests.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ public void getContextWhenEmpty() {
4242
public void setContextAndGetContextThenEmitsContext() {
4343
SecurityContext expectedContext = new SecurityContextImpl(
4444
new TestingAuthenticationToken("user", "password", "ROLE_USER"));
45-
Mono<SecurityContext> context = Mono.subscriberContext()
45+
Mono<SecurityContext> context = Mono.deferContextual(Mono::just)
4646
.flatMap((c) -> ReactiveSecurityContextHolder.getContext())
47-
.subscriberContext(ReactiveSecurityContextHolder.withSecurityContext(Mono.just(expectedContext)));
47+
.contextWrite(ReactiveSecurityContextHolder.withSecurityContext(Mono.just(expectedContext)));
4848
// @formatter:off
4949
StepVerifier.create(context)
5050
.expectNext(expectedContext)
@@ -60,7 +60,7 @@ public void demo() {
6060
.map(SecurityContext::getAuthentication)
6161
.map(Authentication::getName)
6262
.flatMap(this::findMessageByUsername)
63-
.subscriberContext(ReactiveSecurityContextHolder.withAuthentication(authentication));
63+
.contextWrite(ReactiveSecurityContextHolder.withAuthentication(authentication));
6464
StepVerifier.create(messageByUsername)
6565
.expectNext("Hi user")
6666
.verifyComplete();
@@ -76,10 +76,10 @@ public void setContextAndClearAndGetContextThenEmitsEmpty() {
7676
SecurityContext expectedContext = new SecurityContextImpl(
7777
new TestingAuthenticationToken("user", "password", "ROLE_USER"));
7878
// @formatter:off
79-
Mono<SecurityContext> context = Mono.subscriberContext()
79+
Mono<SecurityContext> context = Mono.deferContextual(Mono::just)
8080
.flatMap((c) -> ReactiveSecurityContextHolder.getContext())
81-
.subscriberContext(ReactiveSecurityContextHolder.clearContext())
82-
.subscriberContext(ReactiveSecurityContextHolder.withSecurityContext(Mono.just(expectedContext)));
81+
.contextWrite(ReactiveSecurityContextHolder.clearContext())
82+
.contextWrite(ReactiveSecurityContextHolder.withSecurityContext(Mono.just(expectedContext)));
8383
StepVerifier.create(context)
8484
.verifyComplete();
8585
// @formatter:on
@@ -89,10 +89,10 @@ public void setContextAndClearAndGetContextThenEmitsEmpty() {
8989
public void setAuthenticationAndGetContextThenEmitsContext() {
9090
Authentication expectedAuthentication = new TestingAuthenticationToken("user", "password", "ROLE_USER");
9191
// @formatter:off
92-
Mono<Authentication> authentication = Mono.subscriberContext()
92+
Mono<Authentication> authentication = Mono.deferContextual(Mono::just)
9393
.flatMap((c) -> ReactiveSecurityContextHolder.getContext())
9494
.map(SecurityContext::getAuthentication)
95-
.subscriberContext(ReactiveSecurityContextHolder.withAuthentication(expectedAuthentication));
95+
.contextWrite(ReactiveSecurityContextHolder.withAuthentication(expectedAuthentication));
9696
StepVerifier.create(authentication)
9797
.expectNext(expectedAuthentication)
9898
.verifyComplete();

dependencies/spring-security-dependencies.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ javaPlatform {
99
dependencies {
1010
api platform("org.springframework:spring-framework-bom:$springFrameworkVersion")
1111
api platform("io.projectreactor:reactor-bom:2020.0.17")
12-
api platform("io.rsocket:rsocket-bom:1.1.1")
12+
api platform("io.rsocket:rsocket-bom:1.1.2")
1313
api platform("org.junit:junit-bom:5.8.2")
1414
api platform("org.springframework.data:spring-data-bom:2022.0.0-M3")
1515
api platform("org.jetbrains.kotlin:kotlin-bom:$kotlinVersion")

docs/modules/ROOT/pages/reactive/authorization/method.adoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Mono<String> messageByUsername = ReactiveSecurityContextHolder.getContext()
2121
.map(Authentication::getName)
2222
.flatMap(this::findMessageByUsername)
2323
// In a WebFlux application the `subscriberContext` is automatically setup using `ReactorContextWebFilter`
24-
.subscriberContext(ReactiveSecurityContextHolder.withAuthentication(authentication));
24+
.contextWrite(ReactiveSecurityContextHolder.withAuthentication(authentication));
2525
2626
StepVerifier.create(messageByUsername)
2727
.expectNext("Hi user")
@@ -37,7 +37,7 @@ val messageByUsername: Mono<String> = ReactiveSecurityContextHolder.getContext()
3737
.map(SecurityContext::getAuthentication)
3838
.map(Authentication::getName)
3939
.flatMap(this::findMessageByUsername) // In a WebFlux application the `subscriberContext` is automatically setup using `ReactorContextWebFilter`
40-
.subscriberContext(ReactiveSecurityContextHolder.withAuthentication(authentication))
40+
.contextWrite(ReactiveSecurityContextHolder.withAuthentication(authentication))
4141
4242
StepVerifier.create(messageByUsername)
4343
.expectNext("Hi user")

0 commit comments

Comments
 (0)