|
|
|
@ -32,7 +32,10 @@ public class ReactiveSecurityContextHolderTests { |
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void getContextWhenEmpty() { |
|
|
|
public void getContextWhenEmpty() { |
|
|
|
Mono<SecurityContext> context = ReactiveSecurityContextHolder.getContext(); |
|
|
|
Mono<SecurityContext> context = ReactiveSecurityContextHolder.getContext(); |
|
|
|
StepVerifier.create(context).verifyComplete(); |
|
|
|
// @formatter:off
|
|
|
|
|
|
|
|
StepVerifier.create(context) |
|
|
|
|
|
|
|
.verifyComplete(); |
|
|
|
|
|
|
|
// @formatter:on
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
@ -42,17 +45,26 @@ public class ReactiveSecurityContextHolderTests { |
|
|
|
Mono<SecurityContext> context = Mono.subscriberContext() |
|
|
|
Mono<SecurityContext> context = Mono.subscriberContext() |
|
|
|
.flatMap((c) -> ReactiveSecurityContextHolder.getContext()) |
|
|
|
.flatMap((c) -> ReactiveSecurityContextHolder.getContext()) |
|
|
|
.subscriberContext(ReactiveSecurityContextHolder.withSecurityContext(Mono.just(expectedContext))); |
|
|
|
.subscriberContext(ReactiveSecurityContextHolder.withSecurityContext(Mono.just(expectedContext))); |
|
|
|
StepVerifier.create(context).expectNext(expectedContext).verifyComplete(); |
|
|
|
// @formatter:off
|
|
|
|
|
|
|
|
StepVerifier.create(context) |
|
|
|
|
|
|
|
.expectNext(expectedContext) |
|
|
|
|
|
|
|
.verifyComplete(); |
|
|
|
|
|
|
|
// @formatter:on
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void demo() { |
|
|
|
public void demo() { |
|
|
|
Authentication authentication = new TestingAuthenticationToken("user", "password", "ROLE_USER"); |
|
|
|
Authentication authentication = new TestingAuthenticationToken("user", "password", "ROLE_USER"); |
|
|
|
|
|
|
|
// @formatter:off
|
|
|
|
Mono<String> messageByUsername = ReactiveSecurityContextHolder.getContext() |
|
|
|
Mono<String> messageByUsername = ReactiveSecurityContextHolder.getContext() |
|
|
|
.map(SecurityContext::getAuthentication).map(Authentication::getName) |
|
|
|
.map(SecurityContext::getAuthentication) |
|
|
|
|
|
|
|
.map(Authentication::getName) |
|
|
|
.flatMap(this::findMessageByUsername) |
|
|
|
.flatMap(this::findMessageByUsername) |
|
|
|
.subscriberContext(ReactiveSecurityContextHolder.withAuthentication(authentication)); |
|
|
|
.subscriberContext(ReactiveSecurityContextHolder.withAuthentication(authentication)); |
|
|
|
StepVerifier.create(messageByUsername).expectNext("Hi user").verifyComplete(); |
|
|
|
StepVerifier.create(messageByUsername) |
|
|
|
|
|
|
|
.expectNext("Hi user") |
|
|
|
|
|
|
|
.verifyComplete(); |
|
|
|
|
|
|
|
// @formatter:on
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private Mono<String> findMessageByUsername(String username) { |
|
|
|
private Mono<String> findMessageByUsername(String username) { |
|
|
|
@ -63,20 +75,28 @@ public class ReactiveSecurityContextHolderTests { |
|
|
|
public void setContextAndClearAndGetContextThenEmitsEmpty() { |
|
|
|
public void setContextAndClearAndGetContextThenEmitsEmpty() { |
|
|
|
SecurityContext expectedContext = new SecurityContextImpl( |
|
|
|
SecurityContext expectedContext = new SecurityContextImpl( |
|
|
|
new TestingAuthenticationToken("user", "password", "ROLE_USER")); |
|
|
|
new TestingAuthenticationToken("user", "password", "ROLE_USER")); |
|
|
|
|
|
|
|
// @formatter:off
|
|
|
|
Mono<SecurityContext> context = Mono.subscriberContext() |
|
|
|
Mono<SecurityContext> context = Mono.subscriberContext() |
|
|
|
.flatMap((c) -> ReactiveSecurityContextHolder.getContext()) |
|
|
|
.flatMap((c) -> ReactiveSecurityContextHolder.getContext()) |
|
|
|
.subscriberContext(ReactiveSecurityContextHolder.clearContext()) |
|
|
|
.subscriberContext(ReactiveSecurityContextHolder.clearContext()) |
|
|
|
.subscriberContext(ReactiveSecurityContextHolder.withSecurityContext(Mono.just(expectedContext))); |
|
|
|
.subscriberContext(ReactiveSecurityContextHolder.withSecurityContext(Mono.just(expectedContext))); |
|
|
|
StepVerifier.create(context).verifyComplete(); |
|
|
|
StepVerifier.create(context) |
|
|
|
|
|
|
|
.verifyComplete(); |
|
|
|
|
|
|
|
// @formatter:on
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void setAuthenticationAndGetContextThenEmitsContext() { |
|
|
|
public void setAuthenticationAndGetContextThenEmitsContext() { |
|
|
|
Authentication expectedAuthentication = new TestingAuthenticationToken("user", "password", "ROLE_USER"); |
|
|
|
Authentication expectedAuthentication = new TestingAuthenticationToken("user", "password", "ROLE_USER"); |
|
|
|
|
|
|
|
// @formatter:off
|
|
|
|
Mono<Authentication> authentication = Mono.subscriberContext() |
|
|
|
Mono<Authentication> authentication = Mono.subscriberContext() |
|
|
|
.flatMap((c) -> ReactiveSecurityContextHolder.getContext()).map(SecurityContext::getAuthentication) |
|
|
|
.flatMap((c) -> ReactiveSecurityContextHolder.getContext()) |
|
|
|
|
|
|
|
.map(SecurityContext::getAuthentication) |
|
|
|
.subscriberContext(ReactiveSecurityContextHolder.withAuthentication(expectedAuthentication)); |
|
|
|
.subscriberContext(ReactiveSecurityContextHolder.withAuthentication(expectedAuthentication)); |
|
|
|
StepVerifier.create(authentication).expectNext(expectedAuthentication).verifyComplete(); |
|
|
|
StepVerifier.create(authentication) |
|
|
|
|
|
|
|
.expectNext(expectedAuthentication) |
|
|
|
|
|
|
|
.verifyComplete(); |
|
|
|
|
|
|
|
// @formatter:on
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|