gh-6899: @WithMockUser as metaannotation
This commit is contained in:
parent
86fbb8db07
commit
b478e5bc93
|
@ -148,7 +148,7 @@ public class WithSecurityContextTestExecutionListener extends AbstractTestExecut
|
||||||
}
|
}
|
||||||
|
|
||||||
private Annotation findAnnotation(AnnotatedElement annotated, Class<? extends Annotation> type) {
|
private Annotation findAnnotation(AnnotatedElement annotated, Class<? extends Annotation> type) {
|
||||||
Annotation findAnnotation = AnnotationUtils.findAnnotation(annotated, type);
|
Annotation findAnnotation = AnnotatedElementUtils.findMergedAnnotation(annotated, type);
|
||||||
if (findAnnotation != null) {
|
if (findAnnotation != null) {
|
||||||
return findAnnotation;
|
return findAnnotation;
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,11 +16,18 @@
|
||||||
|
|
||||||
package org.springframework.security.test.context.showcase;
|
package org.springframework.security.test.context.showcase;
|
||||||
|
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Inherited;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.api.extension.ExtendWith;
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
|
import org.springframework.core.annotation.AliasFor;
|
||||||
import org.springframework.security.authentication.AuthenticationCredentialsNotFoundException;
|
import org.springframework.security.authentication.AuthenticationCredentialsNotFoundException;
|
||||||
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
||||||
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
|
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
|
||||||
|
@ -77,6 +84,24 @@ public class WithMockUserTests {
|
||||||
assertThat(message).contains("admin").contains("ADMIN").contains("USER").doesNotContain("ROLE_");
|
assertThat(message).contains("admin").contains("ADMIN").contains("USER").doesNotContain("ROLE_");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Target(ElementType.METHOD)
|
||||||
|
@Inherited
|
||||||
|
@WithMockUser(roles = "ADMIN")
|
||||||
|
public @interface WithAdminUser {
|
||||||
|
|
||||||
|
@AliasFor(annotation = WithMockUser.class, attribute = "value")
|
||||||
|
String value();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@WithAdminUser("admin")
|
||||||
|
public void getMessageWithMetaAnnotationAdminUser() {
|
||||||
|
String message = this.messageService.getMessage();
|
||||||
|
assertThat(message).contains("admin").contains("ADMIN").contains("ROLE_ADMIN");
|
||||||
|
}
|
||||||
|
|
||||||
@EnableGlobalMethodSecurity(prePostEnabled = true)
|
@EnableGlobalMethodSecurity(prePostEnabled = true)
|
||||||
@ComponentScan(basePackageClasses = HelloMessageService.class)
|
@ComponentScan(basePackageClasses = HelloMessageService.class)
|
||||||
static class Config {
|
static class Config {
|
||||||
|
|
Loading…
Reference in New Issue