1
0
mirror of synced 2026-08-03 00:37:03 +00:00

Add BeanResolver to AuthenticationPrincipalArgumentResolver

Previously @AuthenticationPrincipal's expression attribute didn't support
bean references because the BeanResolver was not set on the SpEL context.

This commit adds a BeanResolver and ensures that the configuration
sets a BeanResolver.

Fixes gh-3949
This commit is contained in:
Rob Winch
2016-10-18 19:31:30 -05:00
parent df9e6c973c
commit aaa9708b95
4 changed files with 154 additions and 4 deletions
@@ -19,6 +19,7 @@ import java.lang.annotation.Annotation;
import org.springframework.core.MethodParameter;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.expression.BeanResolver;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
@@ -88,6 +89,8 @@ public final class AuthenticationPrincipalArgumentResolver
private ExpressionParser parser = new SpelExpressionParser();
private BeanResolver beanResolver;
/*
* (non-Javadoc)
*
@@ -125,6 +128,7 @@ public final class AuthenticationPrincipalArgumentResolver
StandardEvaluationContext context = new StandardEvaluationContext();
context.setRootObject(principal);
context.setVariable("this", principal);
context.setBeanResolver(beanResolver);
Expression expression = this.parser.parseExpression(expressionToParse);
principal = expression.getValue(context);
@@ -144,6 +148,14 @@ public final class AuthenticationPrincipalArgumentResolver
return principal;
}
/**
* Sets the {@link BeanResolver} to be used on the expressions
* @param beanResolver the {@link BeanResolver} to use
*/
public void setBeanResolver(BeanResolver beanResolver) {
this.beanResolver = beanResolver;
}
/**
* Obtains the specified {@link Annotation} on the specified {@link MethodParameter}.
*