|
32 | 32 | import reactor.core.publisher.Mono; |
33 | 33 | import reactor.test.StepVerifier; |
34 | 34 |
|
| 35 | +import org.springframework.aop.config.AopConfigUtils; |
| 36 | +import org.springframework.beans.factory.FactoryBean; |
35 | 37 | import org.springframework.beans.factory.config.BeanDefinition; |
| 38 | +import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor; |
36 | 39 | import org.springframework.context.annotation.Bean; |
37 | 40 | import org.springframework.context.annotation.Configuration; |
38 | 41 | import org.springframework.context.annotation.Role; |
|
48 | 51 | import org.springframework.security.access.prepost.PreAuthorize; |
49 | 52 | import org.springframework.security.access.prepost.PreFilter; |
50 | 53 | import org.springframework.security.authorization.AuthorizationDeniedException; |
| 54 | +import org.springframework.security.authorization.method.AuthorizationAdvisor; |
51 | 55 | import org.springframework.security.authorization.method.AuthorizationAdvisorProxyFactory; |
52 | 56 | import org.springframework.security.authorization.method.AuthorizeReturnObject; |
53 | 57 | import org.springframework.security.authorization.method.PrePostTemplateDefaults; |
|
57 | 61 | import org.springframework.security.core.annotation.AnnotationTemplateExpressionDefaults; |
58 | 62 | import org.springframework.security.test.context.annotation.SecurityTestExecutionListeners; |
59 | 63 | import org.springframework.security.test.context.support.WithMockUser; |
| 64 | +import org.springframework.stereotype.Component; |
60 | 65 | import org.springframework.test.context.junit.jupiter.SpringExtension; |
61 | 66 |
|
62 | 67 | import static org.assertj.core.api.Assertions.assertThat; |
@@ -418,6 +423,32 @@ void annotationsInChildClassesDoNotAffectSuperclasses() { |
418 | 423 | this.spring.getContext().getBean(ClassInheritingAbstractClassWithNoAnnotations.class).method(); |
419 | 424 | } |
420 | 425 |
|
| 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 | + |
421 | 452 | @Configuration |
422 | 453 | @EnableReactiveMethodSecurity |
423 | 454 | static class MethodSecurityServiceEnabledConfig { |
@@ -740,4 +771,30 @@ ClassInheritingAbstractClassWithNoAnnotations inheriting() { |
740 | 771 |
|
741 | 772 | } |
742 | 773 |
|
| 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 | + |
743 | 800 | } |
0 commit comments