|
16 | 16 |
|
17 | 17 | package org.springframework.security.test.context.showcase;
|
18 | 18 |
|
| 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 | + |
19 | 25 | import org.junit.jupiter.api.Test;
|
20 | 26 | import org.junit.jupiter.api.extension.ExtendWith;
|
21 | 27 |
|
22 | 28 | import org.springframework.beans.factory.annotation.Autowired;
|
23 | 29 | import org.springframework.context.annotation.ComponentScan;
|
| 30 | +import org.springframework.core.annotation.AliasFor; |
24 | 31 | import org.springframework.security.authentication.AuthenticationCredentialsNotFoundException;
|
25 | 32 | import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
26 | 33 | import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
|
@@ -77,6 +84,24 @@ public void getMessageWithMockUserCustomAuthorities() {
|
77 | 84 | assertThat(message).contains("admin").contains("ADMIN").contains("USER").doesNotContain("ROLE_");
|
78 | 85 | }
|
79 | 86 |
|
| 87 | + @Retention(RetentionPolicy.RUNTIME) |
| 88 | + @Target(ElementType.METHOD) |
| 89 | + @Inherited |
| 90 | + @WithMockUser(roles = "ADMIN") |
| 91 | + public @interface WithAdminUser { |
| 92 | + |
| 93 | + @AliasFor(annotation = WithMockUser.class, attribute = "value") |
| 94 | + String value(); |
| 95 | + |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + @WithAdminUser("admin") |
| 100 | + public void getMessageWithMetaAnnotationAdminUser() { |
| 101 | + String message = this.messageService.getMessage(); |
| 102 | + assertThat(message).contains("admin").contains("ADMIN").contains("ROLE_ADMIN"); |
| 103 | + } |
| 104 | + |
80 | 105 | @EnableGlobalMethodSecurity(prePostEnabled = true)
|
81 | 106 | @ComponentScan(basePackageClasses = HelloMessageService.class)
|
82 | 107 | static class Config {
|
|
0 commit comments