Skip to content

Commit 05fa0e2

Browse files
committed
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
2 parents 988decd + c943ef1 commit 05fa0e2

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

spring-boot-project/spring-boot-autoconfigure/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ dependencies {
194194
optional("org.springframework.security:spring-security-data") {
195195
exclude group: "javax.xml.bind", module: "jaxb-api"
196196
}
197+
optional("org.springframework.security:spring-security-messaging")
197198
optional("org.springframework.security:spring-security-oauth2-client")
198199
optional("org.springframework.security:spring-security-oauth2-jose")
199200
optional("org.springframework.security:spring-security-oauth2-resource-server")

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/rsocket/RSocketSecurityAutoConfiguration.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,10 +18,12 @@
1818

1919
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
2020
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
21+
import org.springframework.boot.autoconfigure.rsocket.RSocketMessageHandlerCustomizer;
2122
import org.springframework.boot.rsocket.server.RSocketServerCustomizer;
2223
import org.springframework.context.annotation.Bean;
2324
import org.springframework.context.annotation.Configuration;
2425
import org.springframework.security.config.annotation.rsocket.EnableRSocketSecurity;
26+
import org.springframework.security.messaging.handler.invocation.reactive.AuthenticationPrincipalArgumentResolver;
2527
import org.springframework.security.rsocket.core.SecuritySocketAcceptorInterceptor;
2628

2729
/**
@@ -30,6 +32,7 @@
3032
*
3133
* @author Madhura Bhave
3234
* @author Brian Clozel
35+
* @author Guirong Hu
3336
* @since 2.2.0
3437
*/
3538
@Configuration(proxyBeanMethods = false)
@@ -42,4 +45,16 @@ RSocketServerCustomizer springSecurityRSocketSecurity(SecuritySocketAcceptorInte
4245
return (server) -> server.interceptors((registry) -> registry.forSocketAcceptor(interceptor));
4346
}
4447

48+
@ConditionalOnClass(AuthenticationPrincipalArgumentResolver.class)
49+
@Configuration(proxyBeanMethods = false)
50+
static class RSocketSecurityMessageHandlerConfiguration {
51+
52+
@Bean
53+
RSocketMessageHandlerCustomizer rSocketAuthenticationPrincipalMessageHandlerCustomizer() {
54+
return (messageHandler) -> messageHandler.getArgumentResolverConfigurer()
55+
.addCustomResolver(new AuthenticationPrincipalArgumentResolver());
56+
}
57+
58+
}
59+
4560
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/rsocket/RSocketSecurityAutoConfigurationTests.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,7 +26,9 @@
2626
import org.springframework.boot.rsocket.server.RSocketServerCustomizer;
2727
import org.springframework.boot.test.context.FilteredClassLoader;
2828
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
29+
import org.springframework.messaging.rsocket.annotation.support.RSocketMessageHandler;
2930
import org.springframework.security.config.annotation.rsocket.RSocketSecurity;
31+
import org.springframework.security.messaging.handler.invocation.reactive.AuthenticationPrincipalArgumentResolver;
3032
import org.springframework.security.rsocket.core.SecuritySocketAcceptorInterceptor;
3133

3234
import static org.assertj.core.api.Assertions.assertThat;
@@ -69,4 +71,14 @@ void autoConfigurationAddsCustomizerForServerRSocketFactory() {
6971
});
7072
}
7173

74+
@Test
75+
void autoConfigurationAddsCustomizerForAuthenticationPrincipalArgumentResolver() {
76+
this.contextRunner.run((context) -> {
77+
assertThat(context).hasSingleBean(RSocketMessageHandler.class);
78+
RSocketMessageHandler handler = context.getBean(RSocketMessageHandler.class);
79+
assertThat(handler.getArgumentResolverConfigurer().getCustomResolvers()).isNotEmpty()
80+
.anyMatch((customResolver) -> customResolver instanceof AuthenticationPrincipalArgumentResolver);
81+
});
82+
}
83+
7284
}

0 commit comments

Comments
 (0)