Skip to content

Commit 0248421

Browse files
committed
Merge branch '5.8.x'
2 parents d996c2a + b478e5b commit 0248421

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

test/src/main/java/org/springframework/security/test/context/support/WithSecurityContextTestExecutionListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ private TestSecurityContext createTestSecurityContext(AnnotatedElement annotated
148148
}
149149

150150
private Annotation findAnnotation(AnnotatedElement annotated, Class<? extends Annotation> type) {
151-
Annotation findAnnotation = AnnotationUtils.findAnnotation(annotated, type);
151+
Annotation findAnnotation = AnnotatedElementUtils.findMergedAnnotation(annotated, type);
152152
if (findAnnotation != null) {
153153
return findAnnotation;
154154
}

test/src/test/java/org/springframework/security/test/context/showcase/WithMockUserTests.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,19 @@
1616

1717
package org.springframework.security.test.context.showcase;
1818

19+
import java.lang.annotation.ElementType;
20+
import java.lang.annotation.Inherited;
21+
import java.lang.annotation.Retention;
22+
import java.lang.annotation.RetentionPolicy;
23+
import java.lang.annotation.Target;
24+
1925
import org.junit.jupiter.api.Test;
2026
import org.junit.jupiter.api.extension.ExtendWith;
2127

2228
import org.springframework.beans.factory.annotation.Autowired;
2329
import org.springframework.context.annotation.ComponentScan;
2430
import org.springframework.context.annotation.Configuration;
31+
import org.springframework.core.annotation.AliasFor;
2532
import org.springframework.security.authentication.AuthenticationCredentialsNotFoundException;
2633
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
2734
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
@@ -79,6 +86,24 @@ public void getMessageWithMockUserCustomAuthorities() {
7986
}
8087

8188
@Configuration
89+
@Retention(RetentionPolicy.RUNTIME)
90+
@Target(ElementType.METHOD)
91+
@Inherited
92+
@WithMockUser(roles = "ADMIN")
93+
public @interface WithAdminUser {
94+
95+
@AliasFor(annotation = WithMockUser.class, attribute = "value")
96+
String value();
97+
98+
}
99+
100+
@Test
101+
@WithAdminUser("admin")
102+
public void getMessageWithMetaAnnotationAdminUser() {
103+
String message = this.messageService.getMessage();
104+
assertThat(message).contains("admin").contains("ADMIN").contains("ROLE_ADMIN");
105+
}
106+
82107
@EnableGlobalMethodSecurity(prePostEnabled = true)
83108
@ComponentScan(basePackageClasses = HelloMessageService.class)
84109
static class Config {

0 commit comments

Comments
 (0)