Merge pull request #28444 from terminux

* pr/28927:
  Polish "Add principal resolution in RSocket handler methods"
  Add principal resolution in RSocket handler methods

Closes gh-28444
This commit is contained in:
Stephane Nicoll 2022-01-04 15:04:03 +01:00
commit 05fa0e246a
3 changed files with 30 additions and 2 deletions

View File

@ -194,6 +194,7 @@ dependencies {
optional("org.springframework.security:spring-security-data") {
exclude group: "javax.xml.bind", module: "jaxb-api"
}
optional("org.springframework.security:spring-security-messaging")
optional("org.springframework.security:spring-security-oauth2-client")
optional("org.springframework.security:spring-security-oauth2-jose")
optional("org.springframework.security:spring-security-oauth2-resource-server")

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -18,10 +18,12 @@ package org.springframework.boot.autoconfigure.security.rsocket;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.rsocket.RSocketMessageHandlerCustomizer;
import org.springframework.boot.rsocket.server.RSocketServerCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.rsocket.EnableRSocketSecurity;
import org.springframework.security.messaging.handler.invocation.reactive.AuthenticationPrincipalArgumentResolver;
import org.springframework.security.rsocket.core.SecuritySocketAcceptorInterceptor;
/**
@ -30,6 +32,7 @@ import org.springframework.security.rsocket.core.SecuritySocketAcceptorIntercept
*
* @author Madhura Bhave
* @author Brian Clozel
* @author Guirong Hu
* @since 2.2.0
*/
@Configuration(proxyBeanMethods = false)
@ -42,4 +45,16 @@ public class RSocketSecurityAutoConfiguration {
return (server) -> server.interceptors((registry) -> registry.forSocketAcceptor(interceptor));
}
@ConditionalOnClass(AuthenticationPrincipalArgumentResolver.class)
@Configuration(proxyBeanMethods = false)
static class RSocketSecurityMessageHandlerConfiguration {
@Bean
RSocketMessageHandlerCustomizer rSocketAuthenticationPrincipalMessageHandlerCustomizer() {
return (messageHandler) -> messageHandler.getArgumentResolverConfigurer()
.addCustomResolver(new AuthenticationPrincipalArgumentResolver());
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -26,7 +26,9 @@ import org.springframework.boot.autoconfigure.security.reactive.ReactiveUserDeta
import org.springframework.boot.rsocket.server.RSocketServerCustomizer;
import org.springframework.boot.test.context.FilteredClassLoader;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.messaging.rsocket.annotation.support.RSocketMessageHandler;
import org.springframework.security.config.annotation.rsocket.RSocketSecurity;
import org.springframework.security.messaging.handler.invocation.reactive.AuthenticationPrincipalArgumentResolver;
import org.springframework.security.rsocket.core.SecuritySocketAcceptorInterceptor;
import static org.assertj.core.api.Assertions.assertThat;
@ -69,4 +71,14 @@ class RSocketSecurityAutoConfigurationTests {
});
}
@Test
void autoConfigurationAddsCustomizerForAuthenticationPrincipalArgumentResolver() {
this.contextRunner.run((context) -> {
assertThat(context).hasSingleBean(RSocketMessageHandler.class);
RSocketMessageHandler handler = context.getBean(RSocketMessageHandler.class);
assertThat(handler.getArgumentResolverConfigurer().getCustomResolvers()).isNotEmpty()
.anyMatch((customResolver) -> customResolver instanceof AuthenticationPrincipalArgumentResolver);
});
}
}