org.acegisecurity.ui.switchuser
Class SwitchUserProcessingFilter

java.lang.Object
  extended by org.acegisecurity.ui.switchuser.SwitchUserProcessingFilter
All Implemented Interfaces:
javax.servlet.Filter, org.springframework.beans.factory.InitializingBean, org.springframework.context.ApplicationEventPublisherAware, org.springframework.context.MessageSourceAware

public class SwitchUserProcessingFilter
extends java.lang.Object
implements javax.servlet.Filter, org.springframework.beans.factory.InitializingBean, org.springframework.context.ApplicationEventPublisherAware, org.springframework.context.MessageSourceAware

Switch User processing filter responsible for user context switching.

This filter is similar to Unix 'su' however for Acegi-managed web applications. A common use-case for this feature is the ability to allow higher-authority users (i.e. ROLE_ADMIN) to switch to a regular user (i.e. ROLE_USER).

This filter assumes that the user performing the switch will be required to be logged in as normal (i.e. ROLE_ADMIN user). The user will then access a page/controller that enables the administrator to specify who they wish to become (see switchUserUrl).
Note: This URL will be required to have to appropriate security contraints configured so that only users of that role can access (i.e. ROLE_ADMIN).

On successful switch, the user's SecurityContextHolder will be updated to reflect the specified user and will also contain an additinal SwitchUserGrantedAuthority which contains the original user.

To 'exit' from a user context, the user will then need to access a URL (see exitUserUrl) that will switch back to the original user as identified by the ROLE_PREVIOUS_ADMINISTRATOR.

To configure the Switch User Processing Filter, create a bean definition for the Switch User processing filter and add to the filterChainProxy.
Example:

 <bean id="switchUserProcessingFilter" class="org.acegisecurity.ui.switchuser.SwitchUserProcessingFilter">
    <property name="authenticationDao" ref="jdbcDaoImpl" />
    <property name="switchUserUrl"><value>/j_acegi_switch_user</value></property>
    <property name="exitUserUrl"><value>/j_acegi_exit_user</value></property>
    <property name="targetUrl"><value>/index.jsp</value></property></bean>

Version:
$Id: SwitchUserProcessingFilter.java 2637 2008-02-15 14:35:16Z luke_t $
Author:
Mark St.Godard
See Also:
SwitchUserGrantedAuthority

Field Summary
static java.lang.String ACEGI_SECURITY_SWITCH_USERNAME_KEY
           
protected  org.springframework.context.support.MessageSourceAccessor messages
           
static java.lang.String ROLE_PREVIOUS_ADMINISTRATOR
           
 
Constructor Summary
SwitchUserProcessingFilter()
           
 
Method Summary
 void afterPropertiesSet()
           
protected  Authentication attemptExitUser(javax.servlet.http.HttpServletRequest request)
          Attempt to exit from an already switched user.
protected  Authentication attemptSwitchUser(javax.servlet.http.HttpServletRequest request)
          Attempt to switch to another user.
 void destroy()
           
 void doFilter(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, javax.servlet.FilterChain chain)
           
 void init(javax.servlet.FilterConfig ignored)
           
protected  boolean requiresExitUser(javax.servlet.http.HttpServletRequest request)
          Checks the request URI for the presence of exitUserUrl.
protected  boolean requiresSwitchUser(javax.servlet.http.HttpServletRequest request)
          Checks the request URI for the presence of switchUserUrl.
 void setApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher eventPublisher)
           
 void setAuthenticationDetailsSource(AuthenticationDetailsSource authenticationDetailsSource)
           
 void setExitUserUrl(java.lang.String exitUserUrl)
          Set the URL to respond to exit user processing.
 void setMessageSource(org.springframework.context.MessageSource messageSource)
           
 void setSwitchUserAuthorityChanger(SwitchUserAuthorityChanger switchUserAuthorityChanger)
           
 void setSwitchUserUrl(java.lang.String switchUserUrl)
          Set the URL to respond to switch user processing.
 void setTargetUrl(java.lang.String targetUrl)
          Sets the URL to go to after a successful switch / exit user request.
 void setUserDetailsService(UserDetailsService userDetailsService)
          Sets the authentication data access object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ACEGI_SECURITY_SWITCH_USERNAME_KEY

public static final java.lang.String ACEGI_SECURITY_SWITCH_USERNAME_KEY
See Also:
Constant Field Values

ROLE_PREVIOUS_ADMINISTRATOR

public static final java.lang.String ROLE_PREVIOUS_ADMINISTRATOR
See Also:
Constant Field Values

messages

protected org.springframework.context.support.MessageSourceAccessor messages
Constructor Detail

SwitchUserProcessingFilter

public SwitchUserProcessingFilter()
Method Detail

afterPropertiesSet

public void afterPropertiesSet()
                        throws java.lang.Exception
Specified by:
afterPropertiesSet in interface org.springframework.beans.factory.InitializingBean
Throws:
java.lang.Exception

attemptExitUser

protected Authentication attemptExitUser(javax.servlet.http.HttpServletRequest request)
                                  throws AuthenticationCredentialsNotFoundException
Attempt to exit from an already switched user.

Parameters:
request - The http servlet request
Returns:
The original Authentication object or null otherwise.
Throws:
AuthenticationCredentialsNotFoundException - If no Authentication associated with this request.

attemptSwitchUser

protected Authentication attemptSwitchUser(javax.servlet.http.HttpServletRequest request)
                                    throws AuthenticationException
Attempt to switch to another user. If the user does not exist or is not active, return null.

Parameters:
request - The http request
Returns:
The new Authentication request if successfully switched to another user, null otherwise.
Throws:
AuthenticationException
UsernameNotFoundException - If the target user is not found.
LockedException - DOCUMENT ME!
DisabledException - If the target user is disabled.
AccountExpiredException - If the target user account is expired.
CredentialsExpiredException - If the target user credentials are expired.

destroy

public void destroy()
Specified by:
destroy in interface javax.servlet.Filter

doFilter

public void doFilter(javax.servlet.ServletRequest request,
                     javax.servlet.ServletResponse response,
                     javax.servlet.FilterChain chain)
              throws java.io.IOException,
                     javax.servlet.ServletException
Specified by:
doFilter in interface javax.servlet.Filter
Throws:
java.io.IOException
javax.servlet.ServletException
See Also:
Filter.doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)

init

public void init(javax.servlet.FilterConfig ignored)
          throws javax.servlet.ServletException
Specified by:
init in interface javax.servlet.Filter
Throws:
javax.servlet.ServletException

requiresExitUser

protected boolean requiresExitUser(javax.servlet.http.HttpServletRequest request)
Checks the request URI for the presence of exitUserUrl.

Parameters:
request - The http servlet request
Returns:
true if the request requires a exit user, false otherwise.
See Also:
exitUserUrl

requiresSwitchUser

protected boolean requiresSwitchUser(javax.servlet.http.HttpServletRequest request)
Checks the request URI for the presence of switchUserUrl.

Parameters:
request - The http servlet request
Returns:
true if the request requires a switch, false otherwise.
See Also:
switchUserUrl

setApplicationEventPublisher

public void setApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher eventPublisher)
                                  throws org.springframework.beans.BeansException
Specified by:
setApplicationEventPublisher in interface org.springframework.context.ApplicationEventPublisherAware
Throws:
org.springframework.beans.BeansException

setAuthenticationDetailsSource

public void setAuthenticationDetailsSource(AuthenticationDetailsSource authenticationDetailsSource)

setExitUserUrl

public void setExitUserUrl(java.lang.String exitUserUrl)
Set the URL to respond to exit user processing.

Parameters:
exitUserUrl - The exit user URL.

setMessageSource

public void setMessageSource(org.springframework.context.MessageSource messageSource)
Specified by:
setMessageSource in interface org.springframework.context.MessageSourceAware

setSwitchUserUrl

public void setSwitchUserUrl(java.lang.String switchUserUrl)
Set the URL to respond to switch user processing.

Parameters:
switchUserUrl - The switch user URL.

setTargetUrl

public void setTargetUrl(java.lang.String targetUrl)
Sets the URL to go to after a successful switch / exit user request.

Parameters:
targetUrl - The target url.

setUserDetailsService

public void setUserDetailsService(UserDetailsService userDetailsService)
Sets the authentication data access object.

Parameters:
userDetailsService - The UserDetailsService to use

setSwitchUserAuthorityChanger

public void setSwitchUserAuthorityChanger(SwitchUserAuthorityChanger switchUserAuthorityChanger)
Parameters:
switchUserAuthorityChanger - to use to fine-tune the authorities granted to subclasses (may be null if SwitchUserProcessingFilter shoudl not fine-tune the authorities)


Copyright © 2004-2012 Interface21, Inc. All Rights Reserved.