parent
95bc670dd5
commit
38aae7f015
|
@ -125,11 +125,15 @@ public class AuthenticationPrincipalArgumentResolver implements HandlerMethodArg
|
|||
@Override
|
||||
public Mono<Object> resolveArgument(MethodParameter parameter, Message<?> message) {
|
||||
ReactiveAdapter adapter = this.adapterRegistry.getAdapter(parameter.getParameterType());
|
||||
return ReactiveSecurityContextHolder.getContext().map(SecurityContext::getAuthentication).flatMap((a) -> {
|
||||
Object p = resolvePrincipal(parameter, a.getPrincipal());
|
||||
Mono<Object> principal = Mono.justOrEmpty(p);
|
||||
return (adapter != null) ? Mono.just(adapter.fromPublisher(principal)) : principal;
|
||||
});
|
||||
// @formatter:off
|
||||
return ReactiveSecurityContextHolder.getContext()
|
||||
.map(SecurityContext::getAuthentication)
|
||||
.flatMap((a) -> {
|
||||
Object p = resolvePrincipal(parameter, a.getPrincipal());
|
||||
Mono<Object> principal = Mono.justOrEmpty(p);
|
||||
return (adapter != null) ? Mono.just(adapter.fromPublisher(principal)) : principal;
|
||||
});
|
||||
// @formatter:on
|
||||
}
|
||||
|
||||
private Object resolvePrincipal(MethodParameter parameter, Object principal) {
|
||||
|
|
|
@ -124,11 +124,14 @@ public class CurrentSecurityContextArgumentResolver implements HandlerMethodArgu
|
|||
@Override
|
||||
public Mono<Object> resolveArgument(MethodParameter parameter, Message<?> message) {
|
||||
ReactiveAdapter adapter = this.adapterRegistry.getAdapter(parameter.getParameterType());
|
||||
return ReactiveSecurityContextHolder.getContext().flatMap((securityContext) -> {
|
||||
Object sc = resolveSecurityContext(parameter, securityContext);
|
||||
Mono<Object> result = Mono.justOrEmpty(sc);
|
||||
return (adapter != null) ? Mono.just(adapter.fromPublisher(result)) : result;
|
||||
});
|
||||
// @formatter:off
|
||||
return ReactiveSecurityContextHolder.getContext()
|
||||
.flatMap((securityContext) -> {
|
||||
Object sc = resolveSecurityContext(parameter, securityContext);
|
||||
Mono<Object> result = Mono.justOrEmpty(sc);
|
||||
return (adapter != null) ? Mono.just(adapter.fromPublisher(result)) : result;
|
||||
});
|
||||
// @formatter:on
|
||||
}
|
||||
|
||||
private Object resolveSecurityContext(MethodParameter parameter, Object securityContext) {
|
||||
|
|
|
@ -80,8 +80,11 @@ public class MessageExpressionConfigAttributeTests {
|
|||
@Test
|
||||
public void postProcessContext() {
|
||||
SimpDestinationMessageMatcher matcher = new SimpDestinationMessageMatcher("/topics/{topic}/**");
|
||||
// @formatter:off
|
||||
Message<?> message = MessageBuilder.withPayload("M")
|
||||
.setHeader(SimpMessageHeaderAccessor.DESTINATION_HEADER, "/topics/someTopic/sub1").build();
|
||||
.setHeader(SimpMessageHeaderAccessor.DESTINATION_HEADER, "/topics/someTopic/sub1")
|
||||
.build();
|
||||
// @formatter:on
|
||||
EvaluationContext context = mock(EvaluationContext.class);
|
||||
this.attribute = new MessageExpressionConfigAttribute(this.expression, matcher);
|
||||
this.attribute.postProcess(context, message);
|
||||
|
|
|
@ -54,9 +54,12 @@ public class AuthenticationPrincipalArgumentResolverTests {
|
|||
@Test
|
||||
public void resolveArgumentWhenAuthenticationPrincipalThenFound() {
|
||||
Authentication authentication = TestAuthentication.authenticatedUser();
|
||||
// @formatter:off
|
||||
Mono<UserDetails> result = (Mono<UserDetails>) this.resolver
|
||||
.resolveArgument(arg0("authenticationPrincipalOnMonoUserDetails"), null)
|
||||
.subscriberContext(ReactiveSecurityContextHolder.withAuthentication(authentication)).block();
|
||||
.subscriberContext(ReactiveSecurityContextHolder.withAuthentication(authentication))
|
||||
.block();
|
||||
// @formatter:on
|
||||
assertThat(result.block()).isEqualTo(authentication.getPrincipal());
|
||||
}
|
||||
|
||||
|
@ -72,9 +75,12 @@ public class AuthenticationPrincipalArgumentResolverTests {
|
|||
@Test
|
||||
public void resolveArgumentWhenMonoAndAuthenticationPrincipalThenFound() {
|
||||
Authentication authentication = TestAuthentication.authenticatedUser();
|
||||
// @formatter:off
|
||||
Mono<UserDetails> result = (Mono<UserDetails>) this.resolver
|
||||
.resolveArgument(arg0("currentUserOnMonoUserDetails"), null)
|
||||
.subscriberContext(ReactiveSecurityContextHolder.withAuthentication(authentication)).block();
|
||||
.subscriberContext(ReactiveSecurityContextHolder.withAuthentication(authentication))
|
||||
.block();
|
||||
// @formatter:on
|
||||
assertThat(result.block()).isEqualTo(authentication.getPrincipal());
|
||||
}
|
||||
|
||||
|
@ -85,9 +91,12 @@ public class AuthenticationPrincipalArgumentResolverTests {
|
|||
@Test
|
||||
public void resolveArgumentWhenExpressionThenFound() {
|
||||
Authentication authentication = TestAuthentication.authenticatedUser();
|
||||
// @formatter:off
|
||||
Mono<String> result = (Mono<String>) this.resolver
|
||||
.resolveArgument(arg0("authenticationPrincipalExpression"), null)
|
||||
.subscriberContext(ReactiveSecurityContextHolder.withAuthentication(authentication)).block();
|
||||
.subscriberContext(ReactiveSecurityContextHolder.withAuthentication(authentication))
|
||||
.block();
|
||||
// @formatter:on
|
||||
assertThat(result.block()).isEqualTo(authentication.getName());
|
||||
}
|
||||
|
||||
|
|
|
@ -42,15 +42,21 @@ public class SimpMessageTypeMatcherTests {
|
|||
|
||||
@Test
|
||||
public void matchesMessageMessageTrue() {
|
||||
// @formatter:off
|
||||
Message<String> message = MessageBuilder.withPayload("Hi")
|
||||
.setHeader(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER, SimpMessageType.MESSAGE).build();
|
||||
.setHeader(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER, SimpMessageType.MESSAGE)
|
||||
.build();
|
||||
// @formatter:on
|
||||
assertThat(this.matcher.matches(message)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void matchesMessageConnectFalse() {
|
||||
// @formatter:off
|
||||
Message<String> message = MessageBuilder.withPayload("Hi")
|
||||
.setHeader(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER, SimpMessageType.CONNECT).build();
|
||||
.setHeader(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER, SimpMessageType.CONNECT)
|
||||
.build();
|
||||
// @formatter:on
|
||||
assertThat(this.matcher.matches(message)).isFalse();
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue