|
@@ -17,6 +17,8 @@
|
|
|
package org.springframework.security.messaging.handler.invocation.reactive;
|
|
|
|
|
|
import java.lang.annotation.Annotation;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
|
|
import org.reactivestreams.Publisher;
|
|
|
import reactor.core.publisher.Mono;
|
|
@@ -25,7 +27,6 @@ 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;
|
|
|
import org.springframework.expression.ExpressionParser;
|
|
@@ -34,6 +35,9 @@ import org.springframework.expression.spel.support.StandardEvaluationContext;
|
|
|
import org.springframework.messaging.Message;
|
|
|
import org.springframework.messaging.handler.invocation.reactive.HandlerMethodArgumentResolver;
|
|
|
import org.springframework.security.core.Authentication;
|
|
|
+import org.springframework.security.core.annotation.AnnotationSynthesizer;
|
|
|
+import org.springframework.security.core.annotation.AnnotationSynthesizers;
|
|
|
+import org.springframework.security.core.annotation.AnnotationTemplateExpressionDefaults;
|
|
|
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
|
|
import org.springframework.security.core.context.ReactiveSecurityContextHolder;
|
|
|
import org.springframework.security.core.context.SecurityContext;
|
|
@@ -90,12 +94,18 @@ import org.springframework.util.StringUtils;
|
|
|
* </pre>
|
|
|
*
|
|
|
* @author Rob Winch
|
|
|
+ * @author DingHao
|
|
|
* @since 5.2
|
|
|
*/
|
|
|
public class AuthenticationPrincipalArgumentResolver implements HandlerMethodArgumentResolver {
|
|
|
|
|
|
+ private final Map<MethodParameter, Annotation> cachedAttributes = new ConcurrentHashMap<>();
|
|
|
+
|
|
|
private ExpressionParser parser = new SpelExpressionParser();
|
|
|
|
|
|
+ private AnnotationSynthesizer<AuthenticationPrincipal> synthesizer = AnnotationSynthesizers
|
|
|
+ .requireUnique(AuthenticationPrincipal.class);
|
|
|
+
|
|
|
private BeanResolver beanResolver;
|
|
|
|
|
|
private ReactiveAdapterRegistry adapterRegistry = ReactiveAdapterRegistry.getSharedInstance();
|
|
@@ -120,7 +130,7 @@ public class AuthenticationPrincipalArgumentResolver implements HandlerMethodArg
|
|
|
|
|
|
@Override
|
|
|
public boolean supportsParameter(MethodParameter parameter) {
|
|
|
- return findMethodAnnotation(AuthenticationPrincipal.class, parameter) != null;
|
|
|
+ return findMethodAnnotation(parameter) != null;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -138,7 +148,7 @@ public class AuthenticationPrincipalArgumentResolver implements HandlerMethodArg
|
|
|
}
|
|
|
|
|
|
private Object resolvePrincipal(MethodParameter parameter, Object principal) {
|
|
|
- AuthenticationPrincipal authPrincipal = findMethodAnnotation(AuthenticationPrincipal.class, parameter);
|
|
|
+ AuthenticationPrincipal authPrincipal = findMethodAnnotation(parameter);
|
|
|
String expressionToParse = authPrincipal.expression();
|
|
|
if (StringUtils.hasLength(expressionToParse)) {
|
|
|
StandardEvaluationContext context = new StandardEvaluationContext();
|
|
@@ -174,26 +184,29 @@ public class AuthenticationPrincipalArgumentResolver implements HandlerMethodArg
|
|
|
return !ClassUtils.isAssignable(typeToCheck, principal.getClass());
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Configure AuthenticationPrincipal template resolution
|
|
|
+ * <p>
|
|
|
+ * By default, this value is <code>null</code>, which indicates that templates should
|
|
|
+ * not be resolved.
|
|
|
+ * @param templateDefaults - whether to resolve AuthenticationPrincipal templates
|
|
|
+ * parameters
|
|
|
+ * @since 6.4
|
|
|
+ */
|
|
|
+ public void setTemplateDefaults(AnnotationTemplateExpressionDefaults templateDefaults) {
|
|
|
+ this.synthesizer = AnnotationSynthesizers.requireUnique(AuthenticationPrincipal.class, templateDefaults);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* Obtains the specified {@link Annotation} on the specified {@link MethodParameter}.
|
|
|
- * @param annotationClass the class of the {@link Annotation} to find on the
|
|
|
* {@link MethodParameter}
|
|
|
* @param parameter the {@link MethodParameter} to search for an {@link Annotation}
|
|
|
* @return the {@link Annotation} that was found or null.
|
|
|
*/
|
|
|
- private <T extends Annotation> T findMethodAnnotation(Class<T> annotationClass, MethodParameter parameter) {
|
|
|
- T annotation = parameter.getParameterAnnotation(annotationClass);
|
|
|
- if (annotation != null) {
|
|
|
- return annotation;
|
|
|
- }
|
|
|
- Annotation[] annotationsToSearch = parameter.getParameterAnnotations();
|
|
|
- for (Annotation toSearch : annotationsToSearch) {
|
|
|
- annotation = AnnotationUtils.findAnnotation(toSearch.annotationType(), annotationClass);
|
|
|
- if (annotation != null) {
|
|
|
- return annotation;
|
|
|
- }
|
|
|
- }
|
|
|
- return null;
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ private <T extends Annotation> T findMethodAnnotation(MethodParameter parameter) {
|
|
|
+ return (T) this.cachedAttributes.computeIfAbsent(parameter,
|
|
|
+ (methodParameter) -> this.synthesizer.synthesize(methodParameter.getParameter()));
|
|
|
}
|
|
|
|
|
|
}
|