|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| package org.acegisecurity.providers.jaas; |
|
17 |
| |
|
18 |
| import org.acegisecurity.AcegiSecurityException; |
|
19 |
| import org.acegisecurity.Authentication; |
|
20 |
| import org.acegisecurity.AuthenticationException; |
|
21 |
| import org.acegisecurity.GrantedAuthority; |
|
22 |
| import org.acegisecurity.context.HttpSessionContextIntegrationFilter; |
|
23 |
| import org.acegisecurity.context.SecurityContext; |
|
24 |
| import org.acegisecurity.providers.AuthenticationProvider; |
|
25 |
| import org.acegisecurity.providers.UsernamePasswordAuthenticationToken; |
|
26 |
| import org.acegisecurity.providers.jaas.event.JaasAuthenticationFailedEvent; |
|
27 |
| import org.acegisecurity.providers.jaas.event.JaasAuthenticationSuccessEvent; |
|
28 |
| import org.acegisecurity.ui.session.HttpSessionDestroyedEvent; |
|
29 |
| import org.apache.commons.logging.Log; |
|
30 |
| import org.apache.commons.logging.LogFactory; |
|
31 |
| import org.springframework.beans.BeansException; |
|
32 |
| import org.springframework.beans.factory.InitializingBean; |
|
33 |
| import org.springframework.context.ApplicationContext; |
|
34 |
| import org.springframework.context.ApplicationContextAware; |
|
35 |
| import org.springframework.context.ApplicationEvent; |
|
36 |
| import org.springframework.context.ApplicationListener; |
|
37 |
| import org.springframework.core.io.Resource; |
|
38 |
| import org.springframework.util.Assert; |
|
39 |
| |
|
40 |
| import javax.security.auth.callback.Callback; |
|
41 |
| import javax.security.auth.callback.CallbackHandler; |
|
42 |
| import javax.security.auth.callback.UnsupportedCallbackException; |
|
43 |
| import javax.security.auth.login.Configuration; |
|
44 |
| import javax.security.auth.login.LoginContext; |
|
45 |
| import javax.security.auth.login.LoginException; |
|
46 |
| import java.io.IOException; |
|
47 |
| import java.security.Principal; |
|
48 |
| import java.security.Security; |
|
49 |
| import java.util.Arrays; |
|
50 |
| import java.util.HashSet; |
|
51 |
| import java.util.Iterator; |
|
52 |
| import java.util.Set; |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
| |
|
67 |
| |
|
68 |
| |
|
69 |
| |
|
70 |
| |
|
71 |
| |
|
72 |
| |
|
73 |
| |
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
| |
|
78 |
| |
|
79 |
| |
|
80 |
| |
|
81 |
| |
|
82 |
| |
|
83 |
| |
|
84 |
| |
|
85 |
| |
|
86 |
| |
|
87 |
| |
|
88 |
| |
|
89 |
| |
|
90 |
| |
|
91 |
| |
|
92 |
| |
|
93 |
| |
|
94 |
| |
|
95 |
| |
|
96 |
| |
|
97 |
| |
|
98 |
| |
|
99 |
| |
|
100 |
| |
|
101 |
| |
|
102 |
| |
|
103 |
| |
|
104 |
| |
|
105 |
| |
|
106 |
| |
|
107 |
| |
|
108 |
| |
|
109 |
| |
|
110 |
| |
|
111 |
| |
|
112 |
| |
|
113 |
| |
|
114 |
| |
|
115 |
| |
|
116 |
| |
|
117 |
| |
|
118 |
| |
|
119 |
| |
|
120 |
| |
|
121 |
| |
|
122 |
| |
|
123 |
| |
|
124 |
| |
|
125 |
| |
|
126 |
| |
|
127 |
| |
|
128 |
| |
|
129 |
| |
|
130 |
| |
|
131 |
| |
|
132 |
| |
|
133 |
| |
|
134 |
| |
|
135 |
| |
|
136 |
| |
|
137 |
| |
|
138 |
| |
|
139 |
| |
|
140 |
| |
|
141 |
| |
|
142 |
| |
|
143 |
| |
|
144 |
| |
|
145 |
| |
|
146 |
| |
|
147 |
| |
|
148 |
| |
|
149 |
| |
|
150 |
| |
|
151 |
| |
|
152 |
| |
|
153 |
| |
|
154 |
| |
|
155 |
| |
|
156 |
| |
|
157 |
| |
|
158 |
| |
|
159 |
| |
|
160 |
| |
|
161 |
| |
|
162 |
| |
|
163 |
| public class JaasAuthenticationProvider implements AuthenticationProvider, |
|
164 |
| InitializingBean, ApplicationContextAware, ApplicationListener { |
|
165 |
| |
|
166 |
| |
|
167 |
| protected static final Log log = LogFactory.getLog(JaasAuthenticationProvider.class); |
|
168 |
| |
|
169 |
| |
|
170 |
| |
|
171 |
| private ApplicationContext context; |
|
172 |
| private LoginExceptionResolver loginExceptionResolver = new DefaultLoginExceptionResolver(); |
|
173 |
| private Resource loginConfig; |
|
174 |
| |
|
175 |
| private String loginContextName = "ACEGI"; |
|
176 |
| private AuthorityGranter[] authorityGranters; |
|
177 |
| private JaasAuthenticationCallbackHandler[] callbackHandlers; |
|
178 |
| |
|
179 |
| |
|
180 |
| |
|
181 |
14
| public void setApplicationContext(ApplicationContext applicationContext)
|
|
182 |
| throws BeansException { |
|
183 |
14
| this.context = applicationContext;
|
|
184 |
| } |
|
185 |
| |
|
186 |
7
| public ApplicationContext getApplicationContext() {
|
|
187 |
7
| return context;
|
|
188 |
| } |
|
189 |
| |
|
190 |
| |
|
191 |
| |
|
192 |
| |
|
193 |
| |
|
194 |
| |
|
195 |
| |
|
196 |
| |
|
197 |
14
| public void setAuthorityGranters(AuthorityGranter[] authorityGranters) {
|
|
198 |
14
| this.authorityGranters = authorityGranters;
|
|
199 |
| } |
|
200 |
| |
|
201 |
| |
|
202 |
| |
|
203 |
| |
|
204 |
| |
|
205 |
| |
|
206 |
| |
|
207 |
| |
|
208 |
| |
|
209 |
4
| public AuthorityGranter[] getAuthorityGranters() {
|
|
210 |
4
| return authorityGranters;
|
|
211 |
| } |
|
212 |
| |
|
213 |
| |
|
214 |
| |
|
215 |
| |
|
216 |
| |
|
217 |
| |
|
218 |
| |
|
219 |
14
| public void setCallbackHandlers(
|
|
220 |
| JaasAuthenticationCallbackHandler[] callbackHandlers) { |
|
221 |
14
| this.callbackHandlers = callbackHandlers;
|
|
222 |
| } |
|
223 |
| |
|
224 |
| |
|
225 |
| |
|
226 |
| |
|
227 |
| |
|
228 |
| |
|
229 |
| |
|
230 |
| |
|
231 |
4
| public JaasAuthenticationCallbackHandler[] getCallbackHandlers() {
|
|
232 |
4
| return callbackHandlers;
|
|
233 |
| } |
|
234 |
| |
|
235 |
| |
|
236 |
| |
|
237 |
| |
|
238 |
| |
|
239 |
| |
|
240 |
| |
|
241 |
| |
|
242 |
| |
|
243 |
| |
|
244 |
| |
|
245 |
13
| public void setLoginConfig(Resource loginConfig) {
|
|
246 |
13
| this.loginConfig = loginConfig;
|
|
247 |
| } |
|
248 |
| |
|
249 |
3
| public Resource getLoginConfig() {
|
|
250 |
3
| return loginConfig;
|
|
251 |
| } |
|
252 |
| |
|
253 |
| |
|
254 |
| |
|
255 |
| |
|
256 |
| |
|
257 |
| |
|
258 |
| |
|
259 |
15
| public void setLoginContextName(String loginContextName) {
|
|
260 |
15
| this.loginContextName = loginContextName;
|
|
261 |
| } |
|
262 |
| |
|
263 |
4
| public String getLoginContextName() {
|
|
264 |
4
| return loginContextName;
|
|
265 |
| } |
|
266 |
| |
|
267 |
1
| public void setLoginExceptionResolver(
|
|
268 |
| LoginExceptionResolver loginExceptionResolver) { |
|
269 |
1
| this.loginExceptionResolver = loginExceptionResolver;
|
|
270 |
| } |
|
271 |
| |
|
272 |
1
| public LoginExceptionResolver getLoginExceptionResolver() {
|
|
273 |
1
| return loginExceptionResolver;
|
|
274 |
| } |
|
275 |
| |
|
276 |
15
| public void afterPropertiesSet() throws Exception {
|
|
277 |
15
| Assert.notNull(loginConfig, "loginConfig must be set on " + getClass());
|
|
278 |
14
| Assert.hasLength(loginContextName,
|
|
279 |
| "loginContextName must be set on " + getClass()); |
|
280 |
| |
|
281 |
12
| configureJaas(loginConfig);
|
|
282 |
| |
|
283 |
12
| Assert.notNull(Configuration.getConfiguration(),
|
|
284 |
| "As per http://java.sun.com/j2se/1.5.0/docs/api/javax/security/auth/login/Configuration.html \"If a Configuration object was set via the Configuration.setConfiguration method, then that object is returned. Otherwise, a default Configuration object is returned\". Your JRE returned null to Configuration.getConfiguration()."); |
|
285 |
| } |
|
286 |
| |
|
287 |
| |
|
288 |
| |
|
289 |
| |
|
290 |
| |
|
291 |
| |
|
292 |
12
| protected void configureJaas(Resource loginConfig) throws IOException {
|
|
293 |
12
| configureJaasUsingLoop();
|
|
294 |
| } |
|
295 |
| |
|
296 |
| |
|
297 |
| |
|
298 |
| |
|
299 |
| |
|
300 |
| |
|
301 |
12
| private void configureJaasUsingLoop() throws IOException {
|
|
302 |
12
| String loginConfigUrl = loginConfig.getURL().toString();
|
|
303 |
12
| boolean alreadySet = false;
|
|
304 |
| |
|
305 |
12
| int n = 1;
|
|
306 |
12
| String prefix = "login.config.url.";
|
|
307 |
12
| String existing = null;
|
|
308 |
| |
|
309 |
?
| while ((existing = Security.getProperty(prefix + n)) != null) {
|
|
310 |
19
| alreadySet = existing.equals(loginConfigUrl);
|
|
311 |
| |
|
312 |
19
| if (alreadySet) {
|
|
313 |
10
| break;
|
|
314 |
| } |
|
315 |
| |
|
316 |
9
| n++;
|
|
317 |
| } |
|
318 |
| |
|
319 |
12
| if (!alreadySet) {
|
|
320 |
2
| String key = prefix + n;
|
|
321 |
2
| log.debug("Setting security property [" + key + "] to: "
|
|
322 |
| + loginConfigUrl); |
|
323 |
2
| Security.setProperty(key, loginConfigUrl);
|
|
324 |
| } |
|
325 |
| } |
|
326 |
| |
|
327 |
| |
|
328 |
| |
|
329 |
| |
|
330 |
| |
|
331 |
| |
|
332 |
| |
|
333 |
| |
|
334 |
| |
|
335 |
| |
|
336 |
| |
|
337 |
| |
|
338 |
| |
|
339 |
| |
|
340 |
7
| public Authentication authenticate(Authentication auth)
|
|
341 |
| throws AuthenticationException { |
|
342 |
7
| if (auth instanceof UsernamePasswordAuthenticationToken) {
|
|
343 |
6
| UsernamePasswordAuthenticationToken request = (UsernamePasswordAuthenticationToken) auth;
|
|
344 |
| |
|
345 |
6
| try {
|
|
346 |
| |
|
347 |
6
| LoginContext loginContext = new LoginContext(loginContextName,
|
|
348 |
| new InternalCallbackHandler(auth)); |
|
349 |
| |
|
350 |
| |
|
351 |
6
| loginContext.login();
|
|
352 |
| |
|
353 |
| |
|
354 |
4
| Set authorities = new HashSet();
|
|
355 |
| |
|
356 |
4
| if (request.getAuthorities() != null) {
|
|
357 |
2
| authorities.addAll(Arrays.asList(request.getAuthorities()));
|
|
358 |
| } |
|
359 |
| |
|
360 |
| |
|
361 |
4
| Set principals = loginContext.getSubject().getPrincipals();
|
|
362 |
| |
|
363 |
4
| for (Iterator iterator = principals.iterator();
|
|
364 |
12
| iterator.hasNext();) {
|
|
365 |
8
| Principal principal = (Principal) iterator.next();
|
|
366 |
| |
|
367 |
8
| for (int i = 0; i < authorityGranters.length; i++) {
|
|
368 |
8
| AuthorityGranter granter = authorityGranters[i];
|
|
369 |
8
| Set roles = granter.grant(principal);
|
|
370 |
| |
|
371 |
| |
|
372 |
8
| if ((roles != null) && !roles.isEmpty()) {
|
|
373 |
4
| for (Iterator roleIterator = roles.iterator();
|
|
374 |
12
| roleIterator.hasNext();) {
|
|
375 |
8
| String role = roleIterator.next().toString();
|
|
376 |
8
| authorities.add(new JaasGrantedAuthority(role,
|
|
377 |
| principal)); |
|
378 |
| } |
|
379 |
| } |
|
380 |
| } |
|
381 |
| } |
|
382 |
| |
|
383 |
| |
|
384 |
4
| JaasAuthenticationToken result = new JaasAuthenticationToken(request
|
|
385 |
| .getPrincipal(), request.getCredentials(), |
|
386 |
| (GrantedAuthority[]) authorities.toArray( |
|
387 |
| new GrantedAuthority[authorities.size()]), loginContext); |
|
388 |
| |
|
389 |
| |
|
390 |
4
| publishSuccessEvent(result);
|
|
391 |
| |
|
392 |
| |
|
393 |
4
| return result;
|
|
394 |
| } catch (LoginException loginException) { |
|
395 |
2
| AcegiSecurityException ase = loginExceptionResolver
|
|
396 |
| .resolveException(loginException); |
|
397 |
| |
|
398 |
2
| publishFailureEvent(request, ase);
|
|
399 |
2
| throw ase;
|
|
400 |
| } |
|
401 |
| } |
|
402 |
| |
|
403 |
1
| return null;
|
|
404 |
| } |
|
405 |
| |
|
406 |
3
| public boolean supports(Class aClass) {
|
|
407 |
3
| return UsernamePasswordAuthenticationToken.class.isAssignableFrom(aClass);
|
|
408 |
| } |
|
409 |
| |
|
410 |
19
| public void onApplicationEvent(ApplicationEvent applicationEvent) {
|
|
411 |
19
| if (applicationEvent instanceof HttpSessionDestroyedEvent) {
|
|
412 |
1
| HttpSessionDestroyedEvent event = (HttpSessionDestroyedEvent) applicationEvent;
|
|
413 |
1
| handleLogout(event);
|
|
414 |
| } |
|
415 |
| } |
|
416 |
| |
|
417 |
| |
|
418 |
| |
|
419 |
| |
|
420 |
| |
|
421 |
| |
|
422 |
1
| protected void handleLogout(HttpSessionDestroyedEvent event) {
|
|
423 |
1
| SecurityContext context = (SecurityContext) event.getSession().getAttribute(HttpSessionContextIntegrationFilter.ACEGI_SECURITY_CONTEXT_KEY);
|
|
424 |
1
| Authentication auth = context.getAuthentication();
|
|
425 |
1
| if (auth instanceof JaasAuthenticationToken) {
|
|
426 |
1
| JaasAuthenticationToken token = (JaasAuthenticationToken) auth;
|
|
427 |
1
| try {
|
|
428 |
1
| LoginContext loginContext = token.getLoginContext();
|
|
429 |
1
| if (loginContext != null) {
|
|
430 |
1
| log.debug("Logging principal: [" + token.getPrincipal() + "] out of LoginContext");
|
|
431 |
1
| loginContext.logout();
|
|
432 |
| } else { |
|
433 |
0
| log.debug("Cannot logout principal: [" + token.getPrincipal() + "] from LoginContext. " +
|
|
434 |
| "The LoginContext is unavailable"); |
|
435 |
| } |
|
436 |
| } catch (LoginException e) { |
|
437 |
0
| log.warn("Error error logging out of LoginContext", e);
|
|
438 |
| } |
|
439 |
| } |
|
440 |
| } |
|
441 |
| |
|
442 |
| |
|
443 |
| |
|
444 |
| |
|
445 |
| |
|
446 |
| |
|
447 |
| |
|
448 |
| |
|
449 |
| |
|
450 |
2
| protected void publishFailureEvent(
|
|
451 |
| UsernamePasswordAuthenticationToken token, AcegiSecurityException ase) { |
|
452 |
2
| getApplicationContext().publishEvent(new JaasAuthenticationFailedEvent(
|
|
453 |
| token, ase)); |
|
454 |
| } |
|
455 |
| |
|
456 |
| |
|
457 |
| |
|
458 |
| |
|
459 |
| |
|
460 |
| |
|
461 |
| |
|
462 |
| |
|
463 |
4
| protected void publishSuccessEvent(
|
|
464 |
| UsernamePasswordAuthenticationToken token) { |
|
465 |
4
| getApplicationContext().publishEvent(new JaasAuthenticationSuccessEvent(
|
|
466 |
| token)); |
|
467 |
| } |
|
468 |
| |
|
469 |
| |
|
470 |
| |
|
471 |
| |
|
472 |
| |
|
473 |
| |
|
474 |
| private class InternalCallbackHandler implements CallbackHandler { |
|
475 |
| private Authentication authentication; |
|
476 |
| |
|
477 |
6
| public InternalCallbackHandler(Authentication authentication) {
|
|
478 |
6
| this.authentication = authentication;
|
|
479 |
| } |
|
480 |
| |
|
481 |
6
| public void handle(Callback[] callbacks)
|
|
482 |
| throws IOException, UnsupportedCallbackException { |
|
483 |
6
| for (int i = 0; i < callbackHandlers.length; i++) {
|
|
484 |
18
| JaasAuthenticationCallbackHandler handler = callbackHandlers[i];
|
|
485 |
| |
|
486 |
18
| for (int j = 0; j < callbacks.length; j++) {
|
|
487 |
54
| Callback callback = callbacks[j];
|
|
488 |
| |
|
489 |
54
| handler.handle(callback, authentication);
|
|
490 |
| } |
|
491 |
| } |
|
492 |
| } |
|
493 |
| } |
|
494 |
| } |