|
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;
|
24 | 30 | import org.springframework.context.annotation.Configuration;
|
| 31 | +import org.springframework.core.annotation.AliasFor; |
25 | 32 | import org.springframework.security.authentication.AuthenticationCredentialsNotFoundException;
|
26 | 33 | import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
27 | 34 | import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
|
@@ -79,6 +86,24 @@ public void getMessageWithMockUserCustomAuthorities() {
|
79 | 86 | }
|
80 | 87 |
|
81 | 88 | @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 | + |
82 | 107 | @EnableGlobalMethodSecurity(prePostEnabled = true)
|
83 | 108 | @ComponentScan(basePackageClasses = HelloMessageService.class)
|
84 | 109 | static class Config {
|
|
0 commit comments