1
1
/*
2
- * Copyright 2002-2016 the original author or authors.
2
+ * Copyright 2002-2020 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.
22
22
import org .apache .commons .logging .LogFactory ;
23
23
import org .springframework .aop .framework .AopProxyUtils ;
24
24
import org .springframework .aop .support .AopUtils ;
25
+ import org .springframework .context .expression .MethodBasedEvaluationContext ;
25
26
import org .springframework .core .ParameterNameDiscoverer ;
26
- import org .springframework .expression .spel .support .StandardEvaluationContext ;
27
27
import org .springframework .security .core .Authentication ;
28
28
import org .springframework .security .core .parameters .DefaultSecurityParameterNameDiscoverer ;
29
29
33
33
* when they are required.
34
34
*
35
35
* @author Luke Taylor
36
+ * @author Daniel Bustamante
36
37
* @since 3.0
37
38
*/
38
- class MethodSecurityEvaluationContext extends StandardEvaluationContext {
39
+ class MethodSecurityEvaluationContext extends MethodBasedEvaluationContext {
39
40
private static final Log logger = LogFactory
40
41
.getLog (MethodSecurityEvaluationContext .class );
41
42
42
- private ParameterNameDiscoverer parameterNameDiscoverer ;
43
- private final MethodInvocation mi ;
44
- private boolean argumentsAdded ;
45
-
46
43
/**
47
44
* Intended for testing. Don't use in practice as it creates a new parameter resolver
48
45
* for each instance. Use the constructor which takes the resolver, as an argument
@@ -54,68 +51,10 @@ class MethodSecurityEvaluationContext extends StandardEvaluationContext {
54
51
55
52
MethodSecurityEvaluationContext (Authentication user , MethodInvocation mi ,
56
53
ParameterNameDiscoverer parameterNameDiscoverer ) {
57
- this .mi = mi ;
58
- this .parameterNameDiscoverer = parameterNameDiscoverer ;
54
+ super (mi .getThis (), getSpecificMethod (mi ), mi .getArguments (), parameterNameDiscoverer );
59
55
}
60
56
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 ;
57
+ private static Method getSpecificMethod (MethodInvocation mi ) {
58
+ return AopUtils .getMostSpecificMethod (mi .getMethod (), AopProxyUtils .ultimateTargetClass (mi .getThis ()));
85
59
}
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
- if (paramNames [i ] != null ) {
116
- setVariable (paramNames [i ], args [i ]);
117
- }
118
- }
119
- }
120
-
121
60
}
0 commit comments