|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2016 the original author or authors. |
| 2 | + * Copyright 2002-2018 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
15 | 15 | */
|
16 | 16 | package org.springframework.security.access.expression.method;
|
17 | 17 |
|
18 |
| -import java.lang.reflect.Method; |
19 |
| - |
20 | 18 | import org.aopalliance.intercept.MethodInvocation;
|
21 | 19 | import org.apache.commons.logging.Log;
|
22 | 20 | import org.apache.commons.logging.LogFactory;
|
23 | 21 | import org.springframework.aop.framework.AopProxyUtils;
|
24 | 22 | import org.springframework.aop.support.AopUtils;
|
| 23 | +import org.springframework.context.expression.MethodBasedEvaluationContext; |
25 | 24 | import org.springframework.core.ParameterNameDiscoverer;
|
26 |
| -import org.springframework.expression.spel.support.StandardEvaluationContext; |
27 | 25 | import org.springframework.security.core.Authentication;
|
28 |
| -import org.springframework.security.core.parameters.DefaultSecurityParameterNameDiscoverer; |
| 26 | + |
| 27 | +import java.lang.reflect.Method; |
29 | 28 |
|
30 | 29 | /**
|
31 | 30 | * Internal security-specific EvaluationContext implementation which lazily adds the
|
32 | 31 | * method parameter values as variables (with the corresponding parameter names) if and
|
33 | 32 | * when they are required.
|
34 | 33 | *
|
35 | 34 | * @author Luke Taylor
|
| 35 | + * @author Daniel Bustamante |
36 | 36 | * @since 3.0
|
37 | 37 | */
|
38 |
| -class MethodSecurityEvaluationContext extends StandardEvaluationContext { |
| 38 | +class MethodSecurityEvaluationContext extends MethodBasedEvaluationContext { |
39 | 39 | private static final Log logger = LogFactory
|
40 | 40 | .getLog(MethodSecurityEvaluationContext.class);
|
41 | 41 |
|
42 |
| - private ParameterNameDiscoverer parameterNameDiscoverer; |
43 |
| - private final MethodInvocation mi; |
44 |
| - private boolean argumentsAdded; |
45 |
| - |
46 |
| - /** |
47 |
| - * Intended for testing. Don't use in practice as it creates a new parameter resolver |
48 |
| - * for each instance. Use the constructor which takes the resolver, as an argument |
49 |
| - * thus allowing for caching. |
50 |
| - */ |
51 |
| - public MethodSecurityEvaluationContext(Authentication user, MethodInvocation mi) { |
52 |
| - this(user, mi, new DefaultSecurityParameterNameDiscoverer()); |
53 |
| - } |
54 | 42 |
|
55 |
| - public MethodSecurityEvaluationContext(Authentication user, MethodInvocation mi, |
| 43 | + MethodSecurityEvaluationContext(Authentication user, MethodInvocation mi, |
56 | 44 | ParameterNameDiscoverer parameterNameDiscoverer) {
|
57 |
| - this.mi = mi; |
58 |
| - this.parameterNameDiscoverer = parameterNameDiscoverer; |
| 45 | + super(mi.getThis(), getSpecificMethod(mi), mi.getArguments(), parameterNameDiscoverer); |
59 | 46 | }
|
60 | 47 |
|
61 |
| - @Override |
62 |
| - public Object lookupVariable(String name) { |
63 |
| - Object variable = super.lookupVariable(name); |
64 |
| - |
65 |
| - if (variable != null) { |
66 |
| - return variable; |
67 |
| - } |
68 |
| - |
69 |
| - if (!argumentsAdded) { |
70 |
| - addArgumentsAsVariables(); |
71 |
| - argumentsAdded = true; |
72 |
| - } |
73 |
| - |
74 |
| - variable = super.lookupVariable(name); |
75 |
| - |
76 |
| - if (variable != null) { |
77 |
| - return variable; |
78 |
| - } |
79 |
| - |
80 |
| - return null; |
81 |
| - } |
82 |
| - |
83 |
| - public void setParameterNameDiscoverer(ParameterNameDiscoverer parameterNameDiscoverer) { |
84 |
| - this.parameterNameDiscoverer = parameterNameDiscoverer; |
85 |
| - } |
86 |
| - |
87 |
| - private void addArgumentsAsVariables() { |
88 |
| - Object[] args = mi.getArguments(); |
89 |
| - |
90 |
| - if (args.length == 0) { |
91 |
| - return; |
92 |
| - } |
93 |
| - |
94 |
| - Object targetObject = mi.getThis(); |
95 |
| - // SEC-1454 |
96 |
| - Class<?> targetClass = AopProxyUtils.ultimateTargetClass(targetObject); |
97 |
| - |
98 |
| - if (targetClass == null) { |
99 |
| - // TODO: Spring should do this, but there's a bug in ultimateTargetClass() |
100 |
| - // which returns null |
101 |
| - targetClass = targetObject.getClass(); |
102 |
| - } |
103 |
| - |
104 |
| - Method method = AopUtils.getMostSpecificMethod(mi.getMethod(), targetClass); |
105 |
| - String[] paramNames = parameterNameDiscoverer.getParameterNames(method); |
106 |
| - |
107 |
| - if (paramNames == null) { |
108 |
| - logger.warn("Unable to resolve method parameter names for method: " |
109 |
| - + method |
110 |
| - + ". Debug symbol information is required if you are using parameter names in expressions."); |
111 |
| - return; |
112 |
| - } |
113 |
| - |
114 |
| - for (int i = 0; i < args.length; i++) { |
115 |
| - super.setVariable(paramNames[i], args[i]); |
116 |
| - } |
| 48 | + private static Method getSpecificMethod(MethodInvocation mi) { |
| 49 | + return AopUtils.getMostSpecificMethod(mi.getMethod(), AopProxyUtils.ultimateTargetClass(mi.getThis())); |
117 | 50 | }
|
118 | 51 |
|
119 | 52 | }
|
0 commit comments