|
@@ -1,23 +1,19 @@
|
|
|
package org.springframework.security.expression.support;
|
|
|
|
|
|
-import java.lang.reflect.Method;
|
|
|
import java.util.List;
|
|
|
|
|
|
import org.aopalliance.intercept.MethodInvocation;
|
|
|
import org.apache.commons.logging.Log;
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
-import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
|
|
|
-import org.springframework.core.ParameterNameDiscoverer;
|
|
|
import org.springframework.expression.EvaluationContext;
|
|
|
import org.springframework.expression.Expression;
|
|
|
-import org.springframework.expression.spel.standard.StandardEvaluationContext;
|
|
|
import org.springframework.security.AccessDeniedException;
|
|
|
import org.springframework.security.Authentication;
|
|
|
import org.springframework.security.ConfigAttribute;
|
|
|
import org.springframework.security.afterinvocation.AfterInvocationProvider;
|
|
|
+import org.springframework.security.expression.DefaultSecurityExpressionHandler;
|
|
|
import org.springframework.security.expression.ExpressionUtils;
|
|
|
-import org.springframework.security.expression.SecurityExpressionRoot;
|
|
|
-import org.springframework.util.ClassUtils;
|
|
|
+import org.springframework.security.expression.SecurityExpressionHandler;
|
|
|
|
|
|
/**
|
|
|
* AfterInvocationProvider which handles the @PostAuthorize and @PostFilter annotation expressions.
|
|
@@ -30,7 +26,7 @@ public class MethodExpressionAfterInvocationProvider implements AfterInvocationP
|
|
|
|
|
|
protected final Log logger = LogFactory.getLog(getClass());
|
|
|
|
|
|
- private ParameterNameDiscoverer parameterNameDiscoverer = new LocalVariableTableParameterNameDiscoverer();
|
|
|
+ private SecurityExpressionHandler expressionHandler = new DefaultSecurityExpressionHandler();
|
|
|
|
|
|
public Object decide(Authentication authentication, Object object, List<ConfigAttribute> config, Object returnedObject)
|
|
|
throws AccessDeniedException {
|
|
@@ -41,10 +37,10 @@ public class MethodExpressionAfterInvocationProvider implements AfterInvocationP
|
|
|
return returnedObject;
|
|
|
}
|
|
|
|
|
|
- StandardEvaluationContext ctx = new StandardEvaluationContext();
|
|
|
- populateContextVariables(ctx, (MethodInvocation) object);
|
|
|
- SecurityExpressionRoot expressionRoot = new SecurityExpressionRoot(authentication);
|
|
|
- ctx.setRootObject(expressionRoot);
|
|
|
+ EvaluationContext ctx =
|
|
|
+ expressionHandler.createEvaluationContext(authentication, (MethodInvocation)object);
|
|
|
+ //SecurityExpressionRoot expressionRoot = new SecurityExpressionRoot(authentication);
|
|
|
+ //ctx.setRootObject(expressionRoot);
|
|
|
|
|
|
Expression postFilter = mca.getFilterExpression();
|
|
|
Expression postAuthorize = mca.getAuthorizeExpression();
|
|
@@ -55,7 +51,7 @@ public class MethodExpressionAfterInvocationProvider implements AfterInvocationP
|
|
|
}
|
|
|
|
|
|
if (returnedObject != null) {
|
|
|
- returnedObject = ExpressionUtils.doFilter(returnedObject, postFilter, ctx);
|
|
|
+ returnedObject = expressionHandler.doFilter(returnedObject, postFilter, ctx);
|
|
|
} else {
|
|
|
if (logger.isDebugEnabled()) {
|
|
|
logger.debug("Return object is null, filtering will be skipped");
|
|
@@ -63,7 +59,7 @@ public class MethodExpressionAfterInvocationProvider implements AfterInvocationP
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- expressionRoot.setReturnObject(returnedObject);
|
|
|
+ expressionHandler.setReturnObject(returnedObject, ctx);
|
|
|
|
|
|
if (postAuthorize != null && !ExpressionUtils.evaluateAsBoolean(postAuthorize, ctx)) {
|
|
|
if (logger.isDebugEnabled()) {
|
|
@@ -75,17 +71,6 @@ public class MethodExpressionAfterInvocationProvider implements AfterInvocationP
|
|
|
return returnedObject;
|
|
|
}
|
|
|
|
|
|
- private void populateContextVariables(EvaluationContext ctx, MethodInvocation mi) {
|
|
|
- Object[] args = mi.getArguments();
|
|
|
- Object targetObject = mi.getThis();
|
|
|
- Method method = ClassUtils.getMostSpecificMethod(mi.getMethod(), targetObject.getClass());
|
|
|
- String[] paramNames = parameterNameDiscoverer.getParameterNames(method);
|
|
|
-
|
|
|
- for(int i=0; i < args.length; i++) {
|
|
|
- ctx.setVariable(paramNames[i], args[i]);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
private PostInvocationExpressionAttribute findMethodAccessControlExpression(List<ConfigAttribute> config) {
|
|
|
// Find the MethodAccessControlExpression attribute
|
|
|
for (ConfigAttribute attribute : config) {
|