Added Testing
Issue gh-16177
This commit is contained in:
+23
-2
@@ -19,6 +19,8 @@ package org.springframework.security.web.method.annotation;
|
||||
import java.lang.annotation.Annotation;
|
||||
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.core.annotation.MergedAnnotations;
|
||||
import org.springframework.expression.BeanResolver;
|
||||
import org.springframework.expression.Expression;
|
||||
import org.springframework.expression.ExpressionParser;
|
||||
@@ -93,6 +95,8 @@ import org.springframework.web.method.support.ModelAndViewContainer;
|
||||
*/
|
||||
public final class AuthenticationPrincipalArgumentResolver implements HandlerMethodArgumentResolver {
|
||||
|
||||
private final Class<AuthenticationPrincipal> annotationType = AuthenticationPrincipal.class;
|
||||
|
||||
private SecurityContextHolderStrategy securityContextHolderStrategy = SecurityContextHolder
|
||||
.getContextHolderStrategy();
|
||||
|
||||
@@ -101,6 +105,8 @@ public final class AuthenticationPrincipalArgumentResolver implements HandlerMet
|
||||
private SecurityAnnotationScanner<AuthenticationPrincipal> scanner = SecurityAnnotationScanners
|
||||
.requireUnique(AuthenticationPrincipal.class);
|
||||
|
||||
private boolean useAnnotationTemplate = false;
|
||||
|
||||
private BeanResolver beanResolver;
|
||||
|
||||
@Override
|
||||
@@ -165,6 +171,7 @@ public final class AuthenticationPrincipalArgumentResolver implements HandlerMet
|
||||
*/
|
||||
public void setTemplateDefaults(AnnotationTemplateExpressionDefaults templateDefaults) {
|
||||
this.scanner = SecurityAnnotationScanners.requireUnique(AuthenticationPrincipal.class, templateDefaults);
|
||||
this.useAnnotationTemplate = templateDefaults != null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -174,8 +181,22 @@ public final class AuthenticationPrincipalArgumentResolver implements HandlerMet
|
||||
* @return the {@link Annotation} that was found or null.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
private <T extends Annotation> T findMethodAnnotation(MethodParameter parameter) {
|
||||
return (T) this.scanner.scan(parameter.getParameter());
|
||||
private AuthenticationPrincipal findMethodAnnotation(MethodParameter parameter) {
|
||||
if (this.useAnnotationTemplate) {
|
||||
return this.scanner.scan(parameter.getParameter());
|
||||
}
|
||||
AuthenticationPrincipal annotation = parameter.getParameterAnnotation(this.annotationType);
|
||||
if (annotation != null) {
|
||||
return annotation;
|
||||
}
|
||||
Annotation[] annotationsToSearch = parameter.getParameterAnnotations();
|
||||
for (Annotation toSearch : annotationsToSearch) {
|
||||
annotation = AnnotationUtils.findAnnotation(toSearch.annotationType(), this.annotationType);
|
||||
if (annotation != null) {
|
||||
return MergedAnnotations.from(toSearch).get(this.annotationType).synthesize();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+36
-2
@@ -28,6 +28,7 @@ import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.core.annotation.AliasFor;
|
||||
import org.springframework.core.annotation.AnnotatedMethod;
|
||||
import org.springframework.expression.BeanResolver;
|
||||
import org.springframework.security.authentication.TestingAuthenticationToken;
|
||||
import org.springframework.security.core.annotation.AnnotationTemplateExpressionDefaults;
|
||||
@@ -214,10 +215,22 @@ public class AuthenticationPrincipalArgumentResolverTests {
|
||||
.isEqualTo(this.expectedPrincipal);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveArgumentWhenAliasForOnInterfaceThenInherits() throws Exception {
|
||||
CustomUserPrincipal principal = new CustomUserPrincipal();
|
||||
setAuthenticationPrincipal(principal);
|
||||
assertThat(this.resolver.resolveArgument(showUserNoConcreteAnnotation(), null, null, null))
|
||||
.isEqualTo(principal.property);
|
||||
}
|
||||
|
||||
private MethodParameter showUserNoAnnotation() {
|
||||
return getMethodParameter("showUserNoAnnotation", String.class);
|
||||
}
|
||||
|
||||
private MethodParameter showUserNoConcreteAnnotation() {
|
||||
return getMethodParameter("showUserNoConcreteAnnotation", String.class);
|
||||
}
|
||||
|
||||
private MethodParameter showUserAnnotationString() {
|
||||
return getMethodParameter("showUserAnnotation", String.class);
|
||||
}
|
||||
@@ -272,7 +285,7 @@ public class AuthenticationPrincipalArgumentResolverTests {
|
||||
|
||||
private MethodParameter getMethodParameter(String methodName, Class<?>... paramTypes) {
|
||||
Method method = ReflectionUtils.findMethod(TestController.class, methodName, paramTypes);
|
||||
return new MethodParameter(method, 0);
|
||||
return new AnnotatedMethod(method).getMethodParameters()[0];
|
||||
}
|
||||
|
||||
private void setAuthenticationPrincipal(Object principal) {
|
||||
@@ -295,6 +308,16 @@ public class AuthenticationPrincipalArgumentResolverTests {
|
||||
|
||||
}
|
||||
|
||||
@Target({ ElementType.PARAMETER })
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@AuthenticationPrincipal
|
||||
@interface Property {
|
||||
|
||||
@AliasFor(attribute = "expression", annotation = AuthenticationPrincipal.class)
|
||||
String value() default "id";
|
||||
|
||||
}
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@AuthenticationPrincipal
|
||||
public @interface CurrentUser2 {
|
||||
@@ -312,11 +335,22 @@ public class AuthenticationPrincipalArgumentResolverTests {
|
||||
|
||||
}
|
||||
|
||||
public static class TestController {
|
||||
public interface TestInterface {
|
||||
|
||||
void showUserNoConcreteAnnotation(@Property("property") String property);
|
||||
|
||||
}
|
||||
|
||||
public static class TestController implements TestInterface {
|
||||
|
||||
public void showUserNoAnnotation(String user) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void showUserNoConcreteAnnotation(String user) {
|
||||
|
||||
}
|
||||
|
||||
public void showUserAnnotation(@AuthenticationPrincipal String user) {
|
||||
}
|
||||
|
||||
|
||||
+41
@@ -32,6 +32,7 @@ import reactor.core.publisher.Mono;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.core.ReactiveAdapterRegistry;
|
||||
import org.springframework.core.annotation.AliasFor;
|
||||
import org.springframework.core.annotation.AnnotatedMethod;
|
||||
import org.springframework.core.annotation.SynthesizingMethodParameter;
|
||||
import org.springframework.expression.BeanResolver;
|
||||
import org.springframework.security.core.Authentication;
|
||||
@@ -230,6 +231,19 @@ public class AuthenticationPrincipalArgumentResolverTests {
|
||||
assertThat(result.block()).isEqualTo(principal.id);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveArgumentWhenAliasForOnInterfaceThenInherits() {
|
||||
CustomUserPrincipal principal = new CustomUserPrincipal();
|
||||
given(this.authentication.getPrincipal()).willReturn(principal);
|
||||
ResolvableMethod method = ResolvableMethod.on(TestController.class)
|
||||
.named("showUserNoConcreteAnnotation")
|
||||
.build();
|
||||
MethodParameter parameter = new AnnotatedMethod(method.method()).getMethodParameters()[0];
|
||||
Mono<Object> result = this.resolver.resolveArgument(parameter, this.bindingContext, this.exchange)
|
||||
.contextWrite(ReactiveSecurityContextHolder.withAuthentication(this.authentication));
|
||||
assertThat(result.block()).isEqualTo(principal.property);
|
||||
}
|
||||
|
||||
private MethodParameter arg0(String methodName) {
|
||||
ResolvableMethod method = ResolvableMethod.on(getClass()).named(methodName).build();
|
||||
return new SynthesizingMethodParameter(method.method(), 0);
|
||||
@@ -317,6 +331,8 @@ public class AuthenticationPrincipalArgumentResolverTests {
|
||||
|
||||
public final int id = 1;
|
||||
|
||||
public final String property = "property";
|
||||
|
||||
public Object getPrincipal() {
|
||||
return this;
|
||||
}
|
||||
@@ -340,4 +356,29 @@ public class AuthenticationPrincipalArgumentResolverTests {
|
||||
|
||||
}
|
||||
|
||||
@Target({ ElementType.PARAMETER })
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@AuthenticationPrincipal
|
||||
@interface Property {
|
||||
|
||||
@AliasFor(attribute = "expression", annotation = AuthenticationPrincipal.class)
|
||||
String value() default "id";
|
||||
|
||||
}
|
||||
|
||||
private interface TestInterface {
|
||||
|
||||
void showUserNoConcreteAnnotation(@Property("property") String property);
|
||||
|
||||
}
|
||||
|
||||
private static class TestController implements TestInterface {
|
||||
|
||||
@Override
|
||||
public void showUserNoConcreteAnnotation(String user) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user