diff --git a/spring-boot-project/spring-boot-autoconfigure/build.gradle b/spring-boot-project/spring-boot-autoconfigure/build.gradle index 456481a5ac8..feb44e09ffd 100644 --- a/spring-boot-project/spring-boot-autoconfigure/build.gradle +++ b/spring-boot-project/spring-boot-autoconfigure/build.gradle @@ -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") diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/rsocket/RSocketSecurityAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/rsocket/RSocketSecurityAutoConfiguration.java index 69235ed362c..108223ba497 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/rsocket/RSocketSecurityAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/rsocket/RSocketSecurityAutoConfiguration.java @@ -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()); + } + + } + } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/rsocket/RSocketSecurityAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/rsocket/RSocketSecurityAutoConfigurationTests.java index 8b6f2411dc2..c86748bc1fd 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/rsocket/RSocketSecurityAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/rsocket/RSocketSecurityAutoConfigurationTests.java @@ -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); + }); + } + }