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