|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| package org.acegisecurity.intercept.method; |
|
17 |
| |
|
18 |
| import org.acegisecurity.ConfigAttribute; |
|
19 |
| import org.acegisecurity.ConfigAttributeDefinition; |
|
20 |
| |
|
21 |
| import org.apache.commons.logging.Log; |
|
22 |
| import org.apache.commons.logging.LogFactory; |
|
23 |
| |
|
24 |
| import java.lang.reflect.Method; |
|
25 |
| |
|
26 |
| import java.util.ArrayList; |
|
27 |
| import java.util.HashMap; |
|
28 |
| import java.util.Iterator; |
|
29 |
| import java.util.List; |
|
30 |
| import java.util.Map; |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
| public class MethodDefinitionMap extends AbstractMethodDefinitionSource { |
|
67 |
| |
|
68 |
| |
|
69 |
| private static final Log logger = LogFactory.getLog(MethodDefinitionMap.class); |
|
70 |
| |
|
71 |
| |
|
72 |
| |
|
73 |
| |
|
74 |
| protected Map methodMap = new HashMap(); |
|
75 |
| |
|
76 |
| |
|
77 |
| private Map nameMap = new HashMap(); |
|
78 |
| |
|
79 |
| |
|
80 |
| |
|
81 |
| |
|
82 |
| |
|
83 |
| |
|
84 |
| |
|
85 |
| |
|
86 |
| |
|
87 |
| |
|
88 |
| |
|
89 |
12
| public Iterator getConfigAttributeDefinitions() {
|
|
90 |
12
| return methodMap.values().iterator();
|
|
91 |
| } |
|
92 |
| |
|
93 |
| |
|
94 |
| |
|
95 |
| |
|
96 |
| |
|
97 |
| |
|
98 |
| |
|
99 |
| |
|
100 |
| |
|
101 |
| |
|
102 |
5
| public int getMethodMapSize() {
|
|
103 |
5
| return this.methodMap.size();
|
|
104 |
| } |
|
105 |
| |
|
106 |
| |
|
107 |
| |
|
108 |
| |
|
109 |
| |
|
110 |
| |
|
111 |
| |
|
112 |
| |
|
113 |
51
| public void addSecureMethod(Method method, ConfigAttributeDefinition attr) {
|
|
114 |
51
| logger.info("Adding secure method [" + method + "] with attributes ["
|
|
115 |
| + attr + "]"); |
|
116 |
51
| this.methodMap.put(method, attr);
|
|
117 |
| } |
|
118 |
| |
|
119 |
| |
|
120 |
| |
|
121 |
| |
|
122 |
| |
|
123 |
| |
|
124 |
| |
|
125 |
| |
|
126 |
| |
|
127 |
| |
|
128 |
50
| public void addSecureMethod(String name, ConfigAttributeDefinition attr) {
|
|
129 |
50
| int lastDotIndex = name.lastIndexOf(".");
|
|
130 |
| |
|
131 |
50
| if (lastDotIndex == -1) {
|
|
132 |
1
| throw new IllegalArgumentException("'" + name
|
|
133 |
| + "' is not a valid method name: format is FQN.methodName"); |
|
134 |
| } |
|
135 |
| |
|
136 |
49
| String className = name.substring(0, lastDotIndex);
|
|
137 |
49
| String methodName = name.substring(lastDotIndex + 1);
|
|
138 |
| |
|
139 |
49
| try {
|
|
140 |
49
| Class clazz = Class.forName(className, true,
|
|
141 |
| Thread.currentThread().getContextClassLoader()); |
|
142 |
48
| addSecureMethod(clazz, methodName, attr);
|
|
143 |
| } catch (ClassNotFoundException ex) { |
|
144 |
1
| throw new IllegalArgumentException("Class '" + className
|
|
145 |
| + "' not found"); |
|
146 |
| } |
|
147 |
| } |
|
148 |
| |
|
149 |
| |
|
150 |
| |
|
151 |
| |
|
152 |
| |
|
153 |
| |
|
154 |
| |
|
155 |
| |
|
156 |
| |
|
157 |
| |
|
158 |
| |
|
159 |
48
| public void addSecureMethod(Class clazz, String mappedName,
|
|
160 |
| ConfigAttributeDefinition attr) { |
|
161 |
48
| String name = clazz.getName() + '.' + mappedName;
|
|
162 |
| |
|
163 |
48
| if (logger.isDebugEnabled()) {
|
|
164 |
0
| logger.debug("Adding secure method [" + name
|
|
165 |
| + "] with attributes [" + attr + "]"); |
|
166 |
| } |
|
167 |
| |
|
168 |
48
| Method[] methods = clazz.getDeclaredMethods();
|
|
169 |
48
| List matchingMethods = new ArrayList();
|
|
170 |
| |
|
171 |
48
| for (int i = 0; i < methods.length; i++) {
|
|
172 |
240
| if (methods[i].getName().equals(mappedName)
|
|
173 |
| || isMatch(methods[i].getName(), mappedName)) { |
|
174 |
54
| matchingMethods.add(methods[i]);
|
|
175 |
| } |
|
176 |
| } |
|
177 |
| |
|
178 |
48
| if (matchingMethods.isEmpty()) {
|
|
179 |
1
| throw new IllegalArgumentException("Couldn't find method '"
|
|
180 |
| + mappedName + "' on " + clazz); |
|
181 |
| } |
|
182 |
| |
|
183 |
| |
|
184 |
47
| for (Iterator it = matchingMethods.iterator(); it.hasNext();) {
|
|
185 |
54
| Method method = (Method) it.next();
|
|
186 |
54
| String regMethodName = (String) this.nameMap.get(method);
|
|
187 |
| |
|
188 |
54
| if ((regMethodName == null)
|
|
189 |
| || (!regMethodName.equals(name) |
|
190 |
| && (regMethodName.length() <= name.length()))) { |
|
191 |
| |
|
192 |
| |
|
193 |
51
| if (regMethodName != null) {
|
|
194 |
1
| logger.debug("Replacing attributes for secure method ["
|
|
195 |
| + method + "]: current name [" + name |
|
196 |
| + "] is more specific than [" + regMethodName + "]"); |
|
197 |
| } |
|
198 |
| |
|
199 |
51
| this.nameMap.put(method, name);
|
|
200 |
51
| addSecureMethod(method, attr);
|
|
201 |
| } else { |
|
202 |
3
| logger.debug("Keeping attributes for secure method [" + method
|
|
203 |
| + "]: current name [" + name |
|
204 |
| + "] is not more specific than [" + regMethodName + "]"); |
|
205 |
| } |
|
206 |
| } |
|
207 |
| } |
|
208 |
| |
|
209 |
21
| protected ConfigAttributeDefinition lookupAttributes(Method method) {
|
|
210 |
21
| ConfigAttributeDefinition definition = new ConfigAttributeDefinition();
|
|
211 |
| |
|
212 |
| |
|
213 |
21
| ConfigAttributeDefinition directlyAssigned = (ConfigAttributeDefinition) this.methodMap
|
|
214 |
| .get(method); |
|
215 |
21
| merge(definition, directlyAssigned);
|
|
216 |
| |
|
217 |
| |
|
218 |
21
| Class[] interfaces = method.getDeclaringClass().getInterfaces();
|
|
219 |
| |
|
220 |
21
| for (int i = 0; i < interfaces.length; i++) {
|
|
221 |
12
| Class clazz = interfaces[i];
|
|
222 |
| |
|
223 |
12
| try {
|
|
224 |
| |
|
225 |
12
| Method interfaceMethod = clazz.getDeclaredMethod(method.getName(),
|
|
226 |
| (Class[]) method.getParameterTypes()); |
|
227 |
12
| ConfigAttributeDefinition interfaceAssigned = (ConfigAttributeDefinition) this.methodMap
|
|
228 |
| .get(interfaceMethod); |
|
229 |
12
| merge(definition, interfaceAssigned);
|
|
230 |
| } catch (Exception e) { |
|
231 |
| |
|
232 |
| } |
|
233 |
| } |
|
234 |
| |
|
235 |
| |
|
236 |
21
| if (definition.size() == 0) {
|
|
237 |
4
| return null;
|
|
238 |
| } else { |
|
239 |
17
| return definition;
|
|
240 |
| } |
|
241 |
| } |
|
242 |
| |
|
243 |
| |
|
244 |
| |
|
245 |
| |
|
246 |
| |
|
247 |
| |
|
248 |
| |
|
249 |
| |
|
250 |
| |
|
251 |
| |
|
252 |
229
| private boolean isMatch(String methodName, String mappedName) {
|
|
253 |
229
| return (mappedName.endsWith("*")
|
|
254 |
| && methodName.startsWith(mappedName.substring(0, mappedName.length() |
|
255 |
| - 1))) |
|
256 |
| || (mappedName.startsWith("*") |
|
257 |
| && methodName.endsWith(mappedName.substring(1, mappedName.length()))); |
|
258 |
| } |
|
259 |
| |
|
260 |
33
| private void merge(ConfigAttributeDefinition definition,
|
|
261 |
| ConfigAttributeDefinition toMerge) { |
|
262 |
33
| if (toMerge == null) {
|
|
263 |
15
| return;
|
|
264 |
| } |
|
265 |
| |
|
266 |
18
| Iterator attribs = toMerge.getConfigAttributes();
|
|
267 |
| |
|
268 |
18
| while (attribs.hasNext()) {
|
|
269 |
27
| definition.addConfigAttribute((ConfigAttribute) attribs.next());
|
|
270 |
| } |
|
271 |
| } |
|
272 |
| } |