Description
Summary
I would like to create a meta annotation for @WithMockUser
from Spring Security Test that uses @AliasFor
to set the username, but the aliased attribute does not work.
Actual Behavior
I created the following annotation. The goal is to have an annotation that has a role preset but still allows switching the username.
package com.example.security;
import org.springframework.core.annotation.AliasFor;
import org.springframework.security.test.context.support.WithMockUser;
import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Inherited
@WithMockUser(roles = "APP_USER")
public @interface WithAppUser {
@AliasFor(annotation = WithMockUser.class, attribute = "value")
String value();
}
But when using this in a test like
@SpringBootTest
public class Test {
@Test
@WithAppUser("foo")
public void test() {
// some code that uses the principal
}
}
the username is the default value of @WithMockUser
, "user".
Expected Behavior
The username should be "foo".
Configuration
A Spring Boot Webflux application with Spring Security.
Version
Spring Boot 2.1.5 with Spring Security starter, the Spring Boot test starter and Spring Security Test (5.1.5)
Sample
spring-security-withmockuser-alias.zip
Additional info
I'm guessing this is because here AnnotationUtils.findAnnotation
is used but could be AnnotatedElementUtils .findMergedAnnotation
, though I am not aware of other implications that would have.