Skip to content

Commit c5461b1

Browse files
evgeniychebanmarcusdacoregio
authored andcommitted
EnableMethodSecurity annotation does not get imported when defined as a meta-annotation
Closes gh-12870
1 parent dd469ac commit c5461b1

File tree

3 files changed

+71
-3
lines changed

3 files changed

+71
-3
lines changed

config/src/main/java/org/springframework/security/config/annotation/method/configuration/MethodSecuritySelector.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -41,7 +41,8 @@ final class MethodSecuritySelector implements ImportSelector {
4141

4242
@Override
4343
public String[] selectImports(@NonNull AnnotationMetadata importMetadata) {
44-
if (!importMetadata.hasAnnotation(EnableMethodSecurity.class.getName())) {
44+
if (!importMetadata.hasAnnotation(EnableMethodSecurity.class.getName())
45+
&& !importMetadata.hasMetaAnnotation(EnableMethodSecurity.class.getName())) {
4546
return new String[0];
4647
}
4748
EnableMethodSecurity annotation = importMetadata.getAnnotations().get(EnableMethodSecurity.class).synthesize();
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2002-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.config.annotation.method.configuration;
18+
19+
import java.lang.annotation.ElementType;
20+
import java.lang.annotation.Retention;
21+
import java.lang.annotation.RetentionPolicy;
22+
import java.lang.annotation.Target;
23+
24+
import org.springframework.context.annotation.AdviceMode;
25+
import org.springframework.core.annotation.AliasFor;
26+
27+
/**
28+
* @author Evgeniy Cheban
29+
*/
30+
@Retention(RetentionPolicy.RUNTIME)
31+
@Target(ElementType.TYPE)
32+
@EnableMethodSecurity
33+
public @interface EnableCustomMethodSecurity {
34+
35+
@AliasFor(annotation = EnableMethodSecurity.class, attribute = "proxyTargetClass")
36+
boolean proxyTargetClass() default false;
37+
38+
@AliasFor(annotation = EnableMethodSecurity.class, attribute = "mode")
39+
AdviceMode mode() default AdviceMode.PROXY;
40+
41+
}

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -92,6 +92,21 @@ public class PrePostMethodSecurityConfigurationTests {
9292
@Autowired(required = false)
9393
BusinessService businessService;
9494

95+
@WithMockUser
96+
@Test
97+
public void customMethodSecurityPreAuthorizeAdminWhenRoleUserThenAccessDeniedException() {
98+
this.spring.register(CustomMethodSecurityServiceConfig.class).autowire();
99+
assertThatExceptionOfType(AccessDeniedException.class).isThrownBy(this.methodSecurityService::preAuthorizeAdmin)
100+
.withMessage("Access Denied");
101+
}
102+
103+
@WithMockUser(roles = "ADMIN")
104+
@Test
105+
public void customMethodSecurityPreAuthorizeAdminWhenRoleAdminThenPasses() {
106+
this.spring.register(CustomMethodSecurityServiceConfig.class).autowire();
107+
this.methodSecurityService.preAuthorizeAdmin();
108+
}
109+
95110
@WithMockUser(roles = "ADMIN")
96111
@Test
97112
public void preAuthorizeWhenRoleAdminThenAccessDeniedException() {
@@ -418,6 +433,17 @@ public void configureWhenAspectJThenRegistersAspects() {
418433
assertThat(this.spring.getContext().containsBean("annotationSecurityAspect$0")).isFalse();
419434
}
420435

436+
@Configuration
437+
@EnableCustomMethodSecurity
438+
static class CustomMethodSecurityServiceConfig {
439+
440+
@Bean
441+
MethodSecurityService methodSecurityService() {
442+
return new MethodSecurityServiceImpl();
443+
}
444+
445+
}
446+
421447
@Configuration
422448
@EnableMethodSecurity
423449
static class MethodSecurityServiceConfig {

0 commit comments

Comments
 (0)