Skip to content

Commit 1aec571

Browse files
committed
Test Reactive Method Security Exactly Once Semantics
Issue gh-15592
1 parent 3e1f8bb commit 1aec571

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

config/src/test/java/org/springframework/security/config/annotation/method/configuration/PrePostReactiveMethodSecurityConfigurationTests.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@
3232
import reactor.core.publisher.Mono;
3333
import reactor.test.StepVerifier;
3434

35+
import org.springframework.aop.config.AopConfigUtils;
36+
import org.springframework.beans.factory.FactoryBean;
3537
import org.springframework.beans.factory.config.BeanDefinition;
38+
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
3639
import org.springframework.context.annotation.Bean;
3740
import org.springframework.context.annotation.Configuration;
3841
import org.springframework.context.annotation.Role;
@@ -48,6 +51,7 @@
4851
import org.springframework.security.access.prepost.PreAuthorize;
4952
import org.springframework.security.access.prepost.PreFilter;
5053
import org.springframework.security.authorization.AuthorizationDeniedException;
54+
import org.springframework.security.authorization.method.AuthorizationAdvisor;
5155
import org.springframework.security.authorization.method.AuthorizationAdvisorProxyFactory;
5256
import org.springframework.security.authorization.method.AuthorizeReturnObject;
5357
import org.springframework.security.authorization.method.PrePostTemplateDefaults;
@@ -57,6 +61,7 @@
5761
import org.springframework.security.core.annotation.AnnotationTemplateExpressionDefaults;
5862
import org.springframework.security.test.context.annotation.SecurityTestExecutionListeners;
5963
import org.springframework.security.test.context.support.WithMockUser;
64+
import org.springframework.stereotype.Component;
6065
import org.springframework.test.context.junit.jupiter.SpringExtension;
6166

6267
import static org.assertj.core.api.Assertions.assertThat;
@@ -418,6 +423,32 @@ void annotationsInChildClassesDoNotAffectSuperclasses() {
418423
this.spring.getContext().getBean(ClassInheritingAbstractClassWithNoAnnotations.class).method();
419424
}
420425

426+
// gh-15592
427+
@Test
428+
void autowireWhenDefaultsThenCreatesExactlyOneAdvisorPerAnnotation() {
429+
this.spring.register(MethodSecurityServiceEnabledConfig.class).autowire();
430+
AuthorizationAdvisorProxyFactory proxyFactory = this.spring.getContext()
431+
.getBean(AuthorizationAdvisorProxyFactory.class);
432+
assertThat(proxyFactory).hasSize(5);
433+
assertThat(this.spring.getContext().getBeanNamesForType(AuthorizationAdvisor.class)).hasSize(5)
434+
.containsExactlyInAnyOrder("preFilterAuthorizationMethodInterceptor",
435+
"preAuthorizeAuthorizationMethodInterceptor", "postAuthorizeAuthorizationMethodInterceptor",
436+
"postFilterAuthorizationMethodInterceptor", "authorizeReturnObjectMethodInterceptor");
437+
}
438+
439+
// gh-15592
440+
@Test
441+
void autowireWhenAspectJAutoProxyAndFactoryBeanThenExactlyOneAdvisorPerAnnotation() {
442+
this.spring.register(AspectJAwareAutoProxyAndFactoryBeansConfig.class).autowire();
443+
AuthorizationAdvisorProxyFactory proxyFactory = this.spring.getContext()
444+
.getBean(AuthorizationAdvisorProxyFactory.class);
445+
assertThat(proxyFactory).hasSize(5);
446+
assertThat(this.spring.getContext().getBeanNamesForType(AuthorizationAdvisor.class)).hasSize(5)
447+
.containsExactlyInAnyOrder("preFilterAuthorizationMethodInterceptor",
448+
"preAuthorizeAuthorizationMethodInterceptor", "postAuthorizeAuthorizationMethodInterceptor",
449+
"postFilterAuthorizationMethodInterceptor", "authorizeReturnObjectMethodInterceptor");
450+
}
451+
421452
@Configuration
422453
@EnableReactiveMethodSecurity
423454
static class MethodSecurityServiceEnabledConfig {
@@ -740,4 +771,30 @@ ClassInheritingAbstractClassWithNoAnnotations inheriting() {
740771

741772
}
742773

774+
@Configuration
775+
@EnableReactiveMethodSecurity
776+
static class AspectJAwareAutoProxyAndFactoryBeansConfig {
777+
778+
@Bean
779+
static BeanDefinitionRegistryPostProcessor beanDefinitionRegistryPostProcessor() {
780+
return AopConfigUtils::registerAspectJAnnotationAutoProxyCreatorIfNecessary;
781+
}
782+
783+
@Component
784+
static class MyFactoryBean implements FactoryBean<Object> {
785+
786+
@Override
787+
public Object getObject() throws Exception {
788+
return new Object();
789+
}
790+
791+
@Override
792+
public Class<?> getObjectType() {
793+
return Object.class;
794+
}
795+
796+
}
797+
798+
}
799+
743800
}

0 commit comments

Comments
 (0)