|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| package org.acegisecurity.ui.switchuser; |
|
17 |
| |
|
18 |
| import java.io.IOException; |
|
19 |
| import java.util.ArrayList; |
|
20 |
| import java.util.Arrays; |
|
21 |
| import java.util.List; |
|
22 |
| |
|
23 |
| import javax.servlet.Filter; |
|
24 |
| import javax.servlet.FilterChain; |
|
25 |
| import javax.servlet.FilterConfig; |
|
26 |
| import javax.servlet.ServletException; |
|
27 |
| import javax.servlet.ServletRequest; |
|
28 |
| import javax.servlet.ServletResponse; |
|
29 |
| import javax.servlet.http.HttpServletRequest; |
|
30 |
| import javax.servlet.http.HttpServletResponse; |
|
31 |
| |
|
32 |
| import org.acegisecurity.AccountExpiredException; |
|
33 |
| import org.acegisecurity.AcegiMessageSource; |
|
34 |
| import org.acegisecurity.Authentication; |
|
35 |
| import org.acegisecurity.AuthenticationCredentialsNotFoundException; |
|
36 |
| import org.acegisecurity.AuthenticationException; |
|
37 |
| import org.acegisecurity.CredentialsExpiredException; |
|
38 |
| import org.acegisecurity.DisabledException; |
|
39 |
| import org.acegisecurity.GrantedAuthority; |
|
40 |
| import org.acegisecurity.LockedException; |
|
41 |
| import org.acegisecurity.context.SecurityContextHolder; |
|
42 |
| import org.acegisecurity.event.authentication.AuthenticationSwitchUserEvent; |
|
43 |
| import org.acegisecurity.providers.UsernamePasswordAuthenticationToken; |
|
44 |
| import org.acegisecurity.ui.WebAuthenticationDetails; |
|
45 |
| import org.acegisecurity.userdetails.UserDetails; |
|
46 |
| import org.acegisecurity.userdetails.UserDetailsService; |
|
47 |
| import org.acegisecurity.userdetails.UsernameNotFoundException; |
|
48 |
| import org.apache.commons.logging.Log; |
|
49 |
| import org.apache.commons.logging.LogFactory; |
|
50 |
| import org.springframework.beans.BeansException; |
|
51 |
| import org.springframework.beans.factory.InitializingBean; |
|
52 |
| import org.springframework.context.ApplicationEventPublisher; |
|
53 |
| import org.springframework.context.ApplicationEventPublisherAware; |
|
54 |
| import org.springframework.context.MessageSource; |
|
55 |
| import org.springframework.context.MessageSourceAware; |
|
56 |
| import org.springframework.context.support.MessageSourceAccessor; |
|
57 |
| import org.springframework.util.Assert; |
|
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 |
| public class SwitchUserProcessingFilter implements Filter, InitializingBean, |
|
110 |
| ApplicationEventPublisherAware, MessageSourceAware { |
|
111 |
| |
|
112 |
| |
|
113 |
| private static final Log logger = LogFactory.getLog(SwitchUserProcessingFilter.class); |
|
114 |
| |
|
115 |
| |
|
116 |
| |
|
117 |
| public static final String ACEGI_SECURITY_SWITCH_USERNAME_KEY = "j_username"; |
|
118 |
| public static final String ROLE_PREVIOUS_ADMINISTRATOR = "ROLE_PREVIOUS_ADMINISTRATOR"; |
|
119 |
| |
|
120 |
| |
|
121 |
| |
|
122 |
| private ApplicationEventPublisher eventPublisher; |
|
123 |
| |
|
124 |
| |
|
125 |
| |
|
126 |
| private UserDetailsService userDetailsService; |
|
127 |
| protected MessageSourceAccessor messages = AcegiMessageSource.getAccessor(); |
|
128 |
| private String exitUserUrl = "/j_acegi_exit_user"; |
|
129 |
| private String switchUserUrl = "/j_acegi_switch_user"; |
|
130 |
| private String targetUrl; |
|
131 |
| |
|
132 |
| |
|
133 |
| |
|
134 |
2
| public void afterPropertiesSet() throws Exception {
|
|
135 |
2
| Assert.hasLength(switchUserUrl, "switchUserUrl must be specified");
|
|
136 |
2
| Assert.hasLength(exitUserUrl, "exitUserUrl must be specified");
|
|
137 |
2
| Assert.hasLength(targetUrl, "targetUrl must be specified");
|
|
138 |
1
| Assert.notNull(userDetailsService, "authenticationDao must be specified");
|
|
139 |
0
| Assert.notNull(messages, "A message source must be set");
|
|
140 |
| } |
|
141 |
| |
|
142 |
| |
|
143 |
| |
|
144 |
| |
|
145 |
| |
|
146 |
| |
|
147 |
| |
|
148 |
| |
|
149 |
| |
|
150 |
| |
|
151 |
| |
|
152 |
| |
|
153 |
2
| protected Authentication attemptExitUser(HttpServletRequest request)
|
|
154 |
| throws AuthenticationCredentialsNotFoundException { |
|
155 |
| |
|
156 |
2
| Authentication current = SecurityContextHolder.getContext()
|
|
157 |
| .getAuthentication(); |
|
158 |
| |
|
159 |
2
| if (null == current) {
|
|
160 |
1
| throw new AuthenticationCredentialsNotFoundException(messages
|
|
161 |
| .getMessage("SwitchUserProcessingFilter.noCurrentUser", |
|
162 |
| "No current user associated with this request")); |
|
163 |
| } |
|
164 |
| |
|
165 |
| |
|
166 |
| |
|
167 |
1
| Authentication original = getSourceAuthentication(current);
|
|
168 |
| |
|
169 |
1
| if (original == null) {
|
|
170 |
0
| logger.error(
|
|
171 |
| "Could not find original user Authentication object!"); |
|
172 |
0
| throw new AuthenticationCredentialsNotFoundException(messages
|
|
173 |
| .getMessage( |
|
174 |
| "SwitchUserProcessingFilter.noOriginalAuthentication", |
|
175 |
| "Could not find original Authentication object")); |
|
176 |
| } |
|
177 |
| |
|
178 |
| |
|
179 |
1
| UserDetails originalUser = null;
|
|
180 |
1
| Object obj = original.getPrincipal();
|
|
181 |
| |
|
182 |
1
| if ((obj != null) && obj instanceof UserDetails) {
|
|
183 |
0
| originalUser = (UserDetails) obj;
|
|
184 |
| } |
|
185 |
| |
|
186 |
| |
|
187 |
1
| if (this.eventPublisher != null) {
|
|
188 |
0
| eventPublisher.publishEvent(new AuthenticationSwitchUserEvent(
|
|
189 |
| current, originalUser)); |
|
190 |
| } |
|
191 |
| |
|
192 |
1
| return original;
|
|
193 |
| } |
|
194 |
| |
|
195 |
| |
|
196 |
| |
|
197 |
| |
|
198 |
| |
|
199 |
| |
|
200 |
| |
|
201 |
| |
|
202 |
| |
|
203 |
| |
|
204 |
| |
|
205 |
| |
|
206 |
| |
|
207 |
| |
|
208 |
| |
|
209 |
| |
|
210 |
| |
|
211 |
| |
|
212 |
| |
|
213 |
| |
|
214 |
| |
|
215 |
7
| protected Authentication attemptSwitchUser(
|
|
216 |
| HttpServletRequest request) throws AuthenticationException { |
|
217 |
7
| UsernamePasswordAuthenticationToken targetUserRequest = null;
|
|
218 |
| |
|
219 |
7
| String username = request.getParameter(ACEGI_SECURITY_SWITCH_USERNAME_KEY);
|
|
220 |
| |
|
221 |
7
| if (username == null) {
|
|
222 |
0
| username = "";
|
|
223 |
| } |
|
224 |
| |
|
225 |
7
| if (logger.isDebugEnabled()) {
|
|
226 |
0
| logger.debug("Attempt to switch to user [" + username + "]");
|
|
227 |
| } |
|
228 |
| |
|
229 |
| |
|
230 |
7
| UserDetails targetUser = this.userDetailsService
|
|
231 |
| .loadUserByUsername(username); |
|
232 |
| |
|
233 |
| |
|
234 |
6
| if (targetUser == null) {
|
|
235 |
0
| throw new UsernameNotFoundException(messages.getMessage(
|
|
236 |
| "SwitchUserProcessingFilter.usernameNotFound", |
|
237 |
| new Object[] {username}, |
|
238 |
| "Username {0} not found")); |
|
239 |
| } |
|
240 |
| |
|
241 |
| |
|
242 |
6
| if (!targetUser.isAccountNonLocked()) {
|
|
243 |
0
| throw new LockedException(messages.getMessage(
|
|
244 |
| "SwitchUserProcessingFilter.locked", |
|
245 |
| "User account is locked")); |
|
246 |
| } |
|
247 |
| |
|
248 |
| |
|
249 |
6
| if (!targetUser.isEnabled()) {
|
|
250 |
1
| throw new DisabledException(messages.getMessage(
|
|
251 |
| "SwitchUserProcessingFilter.disabled", |
|
252 |
| "User is disabled")); |
|
253 |
| } |
|
254 |
| |
|
255 |
| |
|
256 |
5
| if (!targetUser.isAccountNonExpired()) {
|
|
257 |
1
| throw new AccountExpiredException(messages.getMessage(
|
|
258 |
| "SwitchUserProcessingFilter.expired", |
|
259 |
| "User account has expired")); |
|
260 |
| } |
|
261 |
| |
|
262 |
| |
|
263 |
4
| if (!targetUser.isCredentialsNonExpired()) {
|
|
264 |
1
| throw new CredentialsExpiredException(messages
|
|
265 |
| .getMessage( |
|
266 |
| "SwitchUserProcessingFilter.credentialsExpired", |
|
267 |
| "User credentials have expired")); |
|
268 |
| } |
|
269 |
| |
|
270 |
| |
|
271 |
3
| targetUserRequest = createSwitchUserToken(request,
|
|
272 |
| username, targetUser); |
|
273 |
| |
|
274 |
3
| if (logger.isDebugEnabled()) {
|
|
275 |
0
| logger.debug("Switch User Token ["
|
|
276 |
| + targetUserRequest + "]"); |
|
277 |
| } |
|
278 |
| |
|
279 |
| |
|
280 |
3
| if (this.eventPublisher != null) {
|
|
281 |
0
| eventPublisher.publishEvent(new AuthenticationSwitchUserEvent(
|
|
282 |
| SecurityContextHolder.getContext() |
|
283 |
| .getAuthentication(), |
|
284 |
| targetUser)); |
|
285 |
| } |
|
286 |
| |
|
287 |
3
| return targetUserRequest;
|
|
288 |
| } |
|
289 |
| |
|
290 |
| |
|
291 |
| |
|
292 |
| |
|
293 |
| |
|
294 |
| |
|
295 |
| |
|
296 |
| |
|
297 |
| |
|
298 |
| |
|
299 |
| |
|
300 |
| |
|
301 |
| |
|
302 |
| |
|
303 |
3
| private UsernamePasswordAuthenticationToken createSwitchUserToken(
|
|
304 |
| HttpServletRequest request, String username, |
|
305 |
| UserDetails targetUser) { |
|
306 |
3
| UsernamePasswordAuthenticationToken targetUserRequest;
|
|
307 |
| |
|
308 |
| |
|
309 |
| |
|
310 |
3
| Authentication currentAuth = SecurityContextHolder.getContext()
|
|
311 |
| .getAuthentication(); |
|
312 |
3
| GrantedAuthority switchAuthority = new SwitchUserGrantedAuthority(ROLE_PREVIOUS_ADMINISTRATOR,
|
|
313 |
| currentAuth); |
|
314 |
| |
|
315 |
| |
|
316 |
3
| List orig = Arrays.asList(targetUser.getAuthorities());
|
|
317 |
| |
|
318 |
| |
|
319 |
3
| List newAuths = new ArrayList(orig);
|
|
320 |
3
| newAuths.add(switchAuthority);
|
|
321 |
| |
|
322 |
3
| GrantedAuthority[] authorities = {};
|
|
323 |
3
| authorities = (GrantedAuthority[]) newAuths.toArray(authorities);
|
|
324 |
| |
|
325 |
| |
|
326 |
3
| targetUserRequest = new UsernamePasswordAuthenticationToken(targetUser,
|
|
327 |
| targetUser.getPassword(), authorities); |
|
328 |
| |
|
329 |
| |
|
330 |
3
| targetUserRequest.setDetails(new WebAuthenticationDetails(
|
|
331 |
| request)); |
|
332 |
| |
|
333 |
3
| return targetUserRequest;
|
|
334 |
| } |
|
335 |
| |
|
336 |
0
| public void destroy() {}
|
|
337 |
| |
|
338 |
| |
|
339 |
| |
|
340 |
| |
|
341 |
4
| public void doFilter(ServletRequest request,
|
|
342 |
| ServletResponse response, FilterChain chain) |
|
343 |
| throws IOException, ServletException { |
|
344 |
4
| Assert.isInstanceOf(HttpServletRequest.class, request);
|
|
345 |
4
| Assert.isInstanceOf(HttpServletResponse.class, response);
|
|
346 |
| |
|
347 |
4
| HttpServletRequest httpRequest = (HttpServletRequest) request;
|
|
348 |
4
| HttpServletResponse httpResponse = (HttpServletResponse) response;
|
|
349 |
| |
|
350 |
| |
|
351 |
4
| if (requiresSwitchUser(httpRequest)) {
|
|
352 |
| |
|
353 |
2
| Authentication targetUser = attemptSwitchUser(httpRequest);
|
|
354 |
| |
|
355 |
| |
|
356 |
2
| SecurityContextHolder.getContext()
|
|
357 |
| .setAuthentication(targetUser); |
|
358 |
| |
|
359 |
| |
|
360 |
2
| httpResponse.sendRedirect(httpResponse
|
|
361 |
| .encodeRedirectURL(httpRequest |
|
362 |
| .getContextPath() + targetUrl)); |
|
363 |
| |
|
364 |
2
| return;
|
|
365 |
2
| } else if (requiresExitUser(httpRequest)) {
|
|
366 |
| |
|
367 |
2
| Authentication originalUser = attemptExitUser(httpRequest);
|
|
368 |
| |
|
369 |
| |
|
370 |
1
| SecurityContextHolder.getContext()
|
|
371 |
| .setAuthentication(originalUser); |
|
372 |
| |
|
373 |
| |
|
374 |
1
| httpResponse.sendRedirect(httpResponse
|
|
375 |
| .encodeRedirectURL(httpRequest |
|
376 |
| .getContextPath() |
|
377 |
| + targetUrl)); |
|
378 |
| |
|
379 |
1
| return;
|
|
380 |
| } |
|
381 |
| |
|
382 |
0
| chain.doFilter(request, response);
|
|
383 |
| } |
|
384 |
| |
|
385 |
| |
|
386 |
| |
|
387 |
| |
|
388 |
| |
|
389 |
| |
|
390 |
| |
|
391 |
| |
|
392 |
| |
|
393 |
| |
|
394 |
| |
|
395 |
| |
|
396 |
| |
|
397 |
| |
|
398 |
| |
|
399 |
| |
|
400 |
| |
|
401 |
| |
|
402 |
| |
|
403 |
| |
|
404 |
1
| private Authentication getSourceAuthentication(
|
|
405 |
| Authentication current) { |
|
406 |
1
| Authentication original = null;
|
|
407 |
| |
|
408 |
| |
|
409 |
1
| GrantedAuthority[] authorities = current
|
|
410 |
| .getAuthorities(); |
|
411 |
| |
|
412 |
1
| for (int i = 0; i < authorities.length;
|
|
413 |
| i++) { |
|
414 |
| |
|
415 |
3
| if (authorities[i] instanceof SwitchUserGrantedAuthority) {
|
|
416 |
1
| original = ((SwitchUserGrantedAuthority) authorities[i])
|
|
417 |
| .getSource(); |
|
418 |
1
| logger.debug(
|
|
419 |
| "Found original switch user granted authority [" |
|
420 |
| + original + "]"); |
|
421 |
| } |
|
422 |
| } |
|
423 |
| |
|
424 |
1
| return original;
|
|
425 |
| } |
|
426 |
| |
|
427 |
0
| public void init(FilterConfig ignored)
|
|
428 |
| throws ServletException {} |
|
429 |
| |
|
430 |
| |
|
431 |
| |
|
432 |
| |
|
433 |
| |
|
434 |
| |
|
435 |
| |
|
436 |
| |
|
437 |
| |
|
438 |
| |
|
439 |
| |
|
440 |
| |
|
441 |
| |
|
442 |
3
| protected boolean requiresExitUser(
|
|
443 |
| HttpServletRequest request) { |
|
444 |
3
| String uri = stripUri(request);
|
|
445 |
| |
|
446 |
3
| return uri.endsWith(request
|
|
447 |
| .getContextPath() + exitUserUrl); |
|
448 |
| } |
|
449 |
| |
|
450 |
| |
|
451 |
| |
|
452 |
| |
|
453 |
| |
|
454 |
| |
|
455 |
| |
|
456 |
| |
|
457 |
| |
|
458 |
| |
|
459 |
| |
|
460 |
| |
|
461 |
| |
|
462 |
| |
|
463 |
| |
|
464 |
6
| protected boolean requiresSwitchUser(
|
|
465 |
| HttpServletRequest request) { |
|
466 |
6
| String uri = stripUri(request);
|
|
467 |
| |
|
468 |
6
| return uri.endsWith(request
|
|
469 |
| .getContextPath() |
|
470 |
| + switchUserUrl); |
|
471 |
| } |
|
472 |
| |
|
473 |
0
| public void setApplicationEventPublisher(
|
|
474 |
| ApplicationEventPublisher eventPublisher) |
|
475 |
| throws BeansException { |
|
476 |
0
| this.eventPublisher = eventPublisher;
|
|
477 |
| } |
|
478 |
| |
|
479 |
| |
|
480 |
| |
|
481 |
| |
|
482 |
| |
|
483 |
| |
|
484 |
| |
|
485 |
| |
|
486 |
10
| public void setUserDetailsService(
|
|
487 |
| UserDetailsService authenticationDao) { |
|
488 |
10
| this.userDetailsService = authenticationDao;
|
|
489 |
| } |
|
490 |
| |
|
491 |
| |
|
492 |
| |
|
493 |
| |
|
494 |
| |
|
495 |
| |
|
496 |
| |
|
497 |
| |
|
498 |
5
| public void setExitUserUrl(
|
|
499 |
| String exitUserUrl) { |
|
500 |
5
| this.exitUserUrl = exitUserUrl;
|
|
501 |
| } |
|
502 |
| |
|
503 |
0
| public void setMessageSource(
|
|
504 |
| MessageSource messageSource) { |
|
505 |
0
| this.messages = new MessageSourceAccessor(messageSource);
|
|
506 |
| } |
|
507 |
| |
|
508 |
| |
|
509 |
| |
|
510 |
| |
|
511 |
| |
|
512 |
| |
|
513 |
| |
|
514 |
| |
|
515 |
6
| public void setSwitchUserUrl(
|
|
516 |
| String switchUserUrl) { |
|
517 |
6
| this.switchUserUrl = switchUserUrl;
|
|
518 |
| } |
|
519 |
| |
|
520 |
| |
|
521 |
| |
|
522 |
| |
|
523 |
| |
|
524 |
| |
|
525 |
| |
|
526 |
| |
|
527 |
2
| public void setTargetUrl(
|
|
528 |
| String targetUrl) { |
|
529 |
2
| this.targetUrl = targetUrl;
|
|
530 |
| } |
|
531 |
| |
|
532 |
| |
|
533 |
| |
|
534 |
| |
|
535 |
| |
|
536 |
| |
|
537 |
| |
|
538 |
| |
|
539 |
| |
|
540 |
9
| private static String stripUri(
|
|
541 |
| HttpServletRequest request) { |
|
542 |
9
| String uri = request
|
|
543 |
| .getRequestURI(); |
|
544 |
9
| int idx = uri.indexOf(';');
|
|
545 |
| |
|
546 |
9
| if (idx > 0) {
|
|
547 |
1
| uri = uri.substring(0,
|
|
548 |
| idx); |
|
549 |
| } |
|
550 |
| |
|
551 |
9
| return uri;
|
|
552 |
| } |
|
553 |
| } |