1
0
mirror of synced 2026-08-05 17:57:15 +00:00

Support errorOnInvalidType for Reactive AuthenticationPrincipal

Fixes: gh-5096
This commit is contained in:
Rob Winch
2018-03-09 11:14:58 -06:00
parent a2073b2b91
commit d21338d212
3 changed files with 88 additions and 4 deletions
@@ -15,9 +15,13 @@
*/
package org.springframework.security.web.reactive.result.method.annotation;
import java.lang.annotation.Annotation;
import org.reactivestreams.Publisher;
import org.springframework.core.MethodParameter;
import org.springframework.core.ReactiveAdapter;
import org.springframework.core.ReactiveAdapterRegistry;
import org.springframework.core.ResolvableType;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.expression.BeanResolver;
import org.springframework.expression.Expression;
@@ -30,9 +34,8 @@ import org.springframework.util.StringUtils;
import org.springframework.web.reactive.BindingContext;
import org.springframework.web.reactive.result.method.HandlerMethodArgumentResolverSupport;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
import java.lang.annotation.Annotation;
import reactor.core.publisher.Mono;
/**
* Resolves the Authentication
@@ -90,9 +93,37 @@ public class AuthenticationPrincipalArgumentResolver extends HandlerMethodArgume
principal = expression.getValue(context);
}
if (isInvalidType(parameter, principal)) {
if (authPrincipal.errorOnInvalidType()) {
throw new ClassCastException(principal + " is not assignable to "
+ parameter.getParameterType());
}
else {
return null;
}
}
return principal;
}
private boolean isInvalidType(MethodParameter parameter, Object principal) {
if (principal == null) {
return false;
}
Class<?> typeToCheck = parameter.getParameterType();
boolean isParameterPublisher = Publisher.class.isAssignableFrom(parameter.getParameterType());
if (isParameterPublisher) {
ResolvableType resolvableType = ResolvableType.forMethodParameter(parameter);
Class<?> genericType = resolvableType.resolveGeneric(0);
if (genericType == null) {
return false;
}
typeToCheck = genericType;
}
return !typeToCheck.isAssignableFrom(principal.getClass());
}
/**
* Obtains the specified {@link Annotation} on the specified {@link MethodParameter}.
*