Class AbstractAuthenticationProcessingFilter
- java.lang.Object
-
- org.springframework.web.filter.GenericFilterBean
-
- org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter
-
- All Implemented Interfaces:
javax.servlet.Filter,org.springframework.beans.factory.Aware,org.springframework.beans.factory.BeanNameAware,org.springframework.beans.factory.DisposableBean,org.springframework.beans.factory.InitializingBean,org.springframework.context.ApplicationEventPublisherAware,org.springframework.context.EnvironmentAware,org.springframework.context.MessageSourceAware,org.springframework.core.env.EnvironmentCapable,org.springframework.web.context.ServletContextAware
- Direct Known Subclasses:
UsernamePasswordAuthenticationFilter
public abstract class AbstractAuthenticationProcessingFilter extends org.springframework.web.filter.GenericFilterBean implements org.springframework.context.ApplicationEventPublisherAware, org.springframework.context.MessageSourceAwareAbstract processor of browser-based HTTP-based authentication requests.Authentication Process
The filter requires that you set the authenticationManager property. An AuthenticationManager is required to process the authentication request tokens created by implementing classes.This filter will intercept a request and attempt to perform authentication from that request if the request matches the
setRequiresAuthenticationRequestMatcher(RequestMatcher).Authentication is performed by the
attemptAuthenticationmethod, which must be implemented by subclasses.Authentication Success
If authentication is successful, the resultingAuthenticationobject will be placed into theSecurityContextfor the current thread, which is guaranteed to have already been created by an earlier filter.The configured
AuthenticationSuccessHandlerwill then be called to take the redirect to the appropriate destination after a successful login. The default behaviour is implemented in aSavedRequestAwareAuthenticationSuccessHandlerwhich will make use of any DefaultSavedRequest set by the ExceptionTranslationFilter and redirect the user to the URL contained therein. Otherwise it will redirect to the webapp root "/". You can customize this behaviour by injecting a differently configured instance of this class, or by using a different implementation.See the
successfulAuthentication(HttpServletRequest, HttpServletResponse, FilterChain, Authentication)method for more information.Authentication Failure
If authentication fails, it will delegate to the configuredAuthenticationFailureHandlerto allow the failure information to be conveyed to the client. The default implementation isSimpleUrlAuthenticationFailureHandler, which sends a 401 error code to the client. It may also be configured with a failure URL as an alternative. Again you can inject whatever behaviour you require here.Event Publication
If authentication is successful, anInteractiveAuthenticationSuccessEventwill be published via the application context. No events will be published if authentication was unsuccessful, because this would generally be recorded via anAuthenticationManager-specific application event.Session Authentication
The class has an optionalSessionAuthenticationStrategywhich will be invoked immediately after a successful call toattemptAuthentication(). Different implementationscan be injectedto enable things like session-fixation attack prevention or to control the number of simultaneous sessions a principal may have.
-
-
Field Summary
Fields Modifier and Type Field Description protected org.springframework.security.authentication.AuthenticationDetailsSource<javax.servlet.http.HttpServletRequest,?>authenticationDetailsSourceprotected org.springframework.context.ApplicationEventPublishereventPublisherprotected org.springframework.context.support.MessageSourceAccessormessages
-
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractAuthenticationProcessingFilter(java.lang.String defaultFilterProcessesUrl)protectedAbstractAuthenticationProcessingFilter(java.lang.String defaultFilterProcessesUrl, org.springframework.security.authentication.AuthenticationManager authenticationManager)Creates a new instance with a default filterProcessesUrl and anAuthenticationManagerprotectedAbstractAuthenticationProcessingFilter(RequestMatcher requiresAuthenticationRequestMatcher)Creates a new instanceprotectedAbstractAuthenticationProcessingFilter(RequestMatcher requiresAuthenticationRequestMatcher, org.springframework.security.authentication.AuthenticationManager authenticationManager)Creates a new instance with aRequestMatcherand anAuthenticationManager
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidafterPropertiesSet()abstract org.springframework.security.core.AuthenticationattemptAuthentication(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)Performs actual authentication.voiddoFilter(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, javax.servlet.FilterChain chain)Invokes therequiresAuthenticationmethod to determine whether the request is for authentication and should be handled by this filter.protected booleangetAllowSessionCreation()protected org.springframework.security.authentication.AuthenticationManagergetAuthenticationManager()protected AuthenticationFailureHandlergetFailureHandler()RememberMeServicesgetRememberMeServices()protected AuthenticationSuccessHandlergetSuccessHandler()protected booleanrequiresAuthentication(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)Indicates whether this filter should attempt to process a login request for the current invocation.voidsetAllowSessionCreation(boolean allowSessionCreation)voidsetApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher eventPublisher)voidsetAuthenticationDetailsSource(org.springframework.security.authentication.AuthenticationDetailsSource<javax.servlet.http.HttpServletRequest,?> authenticationDetailsSource)voidsetAuthenticationFailureHandler(AuthenticationFailureHandler failureHandler)voidsetAuthenticationManager(org.springframework.security.authentication.AuthenticationManager authenticationManager)voidsetAuthenticationSuccessHandler(AuthenticationSuccessHandler successHandler)Sets the strategy used to handle a successful authentication.voidsetContinueChainBeforeSuccessfulAuthentication(boolean continueChainBeforeSuccessfulAuthentication)Indicates if the filter chain should be continued prior to delegation tosuccessfulAuthentication(HttpServletRequest, HttpServletResponse, FilterChain, Authentication), which may be useful in certain environment (such as Tapestry applications).voidsetFilterProcessesUrl(java.lang.String filterProcessesUrl)Sets the URL that determines if authentication is requiredvoidsetMessageSource(org.springframework.context.MessageSource messageSource)voidsetRememberMeServices(RememberMeServices rememberMeServices)voidsetRequiresAuthenticationRequestMatcher(RequestMatcher requestMatcher)voidsetSessionAuthenticationStrategy(SessionAuthenticationStrategy sessionStrategy)The session handling strategy which will be invoked immediately after an authentication request is successfully processed by the AuthenticationManager.protected voidsuccessfulAuthentication(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, javax.servlet.FilterChain chain, org.springframework.security.core.Authentication authResult)Default behaviour for successful authentication.protected voidunsuccessfulAuthentication(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, org.springframework.security.core.AuthenticationException failed)Default behaviour for unsuccessful authentication.
-
-
-
Field Detail
-
eventPublisher
protected org.springframework.context.ApplicationEventPublisher eventPublisher
-
authenticationDetailsSource
protected org.springframework.security.authentication.AuthenticationDetailsSource<javax.servlet.http.HttpServletRequest,?> authenticationDetailsSource
-
messages
protected org.springframework.context.support.MessageSourceAccessor messages
-
-
Constructor Detail
-
AbstractAuthenticationProcessingFilter
protected AbstractAuthenticationProcessingFilter(java.lang.String defaultFilterProcessesUrl)
- Parameters:
defaultFilterProcessesUrl- the default value for filterProcessesUrl.
-
AbstractAuthenticationProcessingFilter
protected AbstractAuthenticationProcessingFilter(RequestMatcher requiresAuthenticationRequestMatcher)
Creates a new instance- Parameters:
requiresAuthenticationRequestMatcher- theRequestMatcherused to determine if authentication is required. Cannot be null.
-
AbstractAuthenticationProcessingFilter
protected AbstractAuthenticationProcessingFilter(java.lang.String defaultFilterProcessesUrl, org.springframework.security.authentication.AuthenticationManager authenticationManager)Creates a new instance with a default filterProcessesUrl and anAuthenticationManager- Parameters:
defaultFilterProcessesUrl- the default value for filterProcessesUrl.authenticationManager- theAuthenticationManagerused to authenticate anAuthenticationobject. Cannot be null.
-
AbstractAuthenticationProcessingFilter
protected AbstractAuthenticationProcessingFilter(RequestMatcher requiresAuthenticationRequestMatcher, org.springframework.security.authentication.AuthenticationManager authenticationManager)
Creates a new instance with aRequestMatcherand anAuthenticationManager- Parameters:
requiresAuthenticationRequestMatcher- theRequestMatcherused to determine if authentication is required. Cannot be null.authenticationManager- theAuthenticationManagerused to authenticate anAuthenticationobject. Cannot be null.
-
-
Method Detail
-
afterPropertiesSet
public void afterPropertiesSet()
- Specified by:
afterPropertiesSetin interfaceorg.springframework.beans.factory.InitializingBean- Overrides:
afterPropertiesSetin classorg.springframework.web.filter.GenericFilterBean
-
doFilter
public void doFilter(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, javax.servlet.FilterChain chain) throws java.io.IOException, javax.servlet.ServletExceptionInvokes therequiresAuthenticationmethod to determine whether the request is for authentication and should be handled by this filter. If it is an authentication request, theattemptAuthenticationwill be invoked to perform the authentication. There are then three possible outcomes:- An Authentication object is returned. The configured
SessionAuthenticationStrategywill be invoked (to handle any session-related behaviour such as creating a new session to protect against session-fixation attacks) followed by the invocation ofsuccessfulAuthentication(HttpServletRequest, HttpServletResponse, FilterChain, Authentication)method - An AuthenticationException occurs during authentication. The
unsuccessfulAuthenticationmethod will be invoked - Null is returned, indicating that the authentication process is incomplete. The method will then return immediately, assuming that the subclass has done any necessary work (such as redirects) to continue the authentication process. The assumption is that a later request will be received by this method where the returned Authentication object is not null.
- Specified by:
doFilterin interfacejavax.servlet.Filter- Throws:
java.io.IOExceptionjavax.servlet.ServletException
- An Authentication object is returned. The configured
-
requiresAuthentication
protected boolean requiresAuthentication(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)Indicates whether this filter should attempt to process a login request for the current invocation.It strips any parameters from the "path" section of the request URL (such as the jsessionid parameter in https://host/myapp/index.html;jsessionid=blah) before matching against the
filterProcessesUrlproperty.Subclasses may override for special requirements, such as Tapestry integration.
- Returns:
trueif the filter should attempt authentication,falseotherwise.
-
attemptAuthentication
public abstract org.springframework.security.core.Authentication attemptAuthentication(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws org.springframework.security.core.AuthenticationException, java.io.IOException, javax.servlet.ServletExceptionPerforms actual authentication.The implementation should do one of the following:
- Return a populated authentication token for the authenticated user, indicating successful authentication
- Return null, indicating that the authentication process is still in progress. Before returning, the implementation should perform any additional work required to complete the process.
- Throw an AuthenticationException if the authentication process fails
- Parameters:
request- from which to extract parameters and perform the authenticationresponse- the response, which may be needed if the implementation has to do a redirect as part of a multi-stage authentication process (such as OpenID).- Returns:
- the authenticated user token, or null if authentication is incomplete.
- Throws:
org.springframework.security.core.AuthenticationException- if authentication fails.java.io.IOExceptionjavax.servlet.ServletException
-
successfulAuthentication
protected void successfulAuthentication(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, javax.servlet.FilterChain chain, org.springframework.security.core.Authentication authResult) throws java.io.IOException, javax.servlet.ServletExceptionDefault behaviour for successful authentication.- Sets the successful Authentication object on the
SecurityContextHolder - Informs the configured RememberMeServices of the successful login
- Fires an
InteractiveAuthenticationSuccessEventvia the configured ApplicationEventPublisher - Delegates additional behaviour to the
AuthenticationSuccessHandler.
FilterChainafter successful authentication.- Parameters:
request-response-chain-authResult- the object returned from the attemptAuthentication method.- Throws:
java.io.IOExceptionjavax.servlet.ServletException
- Sets the successful Authentication object on the
-
unsuccessfulAuthentication
protected void unsuccessfulAuthentication(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, org.springframework.security.core.AuthenticationException failed) throws java.io.IOException, javax.servlet.ServletExceptionDefault behaviour for unsuccessful authentication.- Clears the
SecurityContextHolder - Stores the exception in the session (if it exists or allowSesssionCreation is set to true)
- Informs the configured RememberMeServices of the failed login
- Delegates additional behaviour to the
AuthenticationFailureHandler.
- Throws:
java.io.IOExceptionjavax.servlet.ServletException
- Clears the
-
getAuthenticationManager
protected org.springframework.security.authentication.AuthenticationManager getAuthenticationManager()
-
setAuthenticationManager
public void setAuthenticationManager(org.springframework.security.authentication.AuthenticationManager authenticationManager)
-
setFilterProcessesUrl
public void setFilterProcessesUrl(java.lang.String filterProcessesUrl)
Sets the URL that determines if authentication is required- Parameters:
filterProcessesUrl-
-
setRequiresAuthenticationRequestMatcher
public final void setRequiresAuthenticationRequestMatcher(RequestMatcher requestMatcher)
-
getRememberMeServices
public RememberMeServices getRememberMeServices()
-
setRememberMeServices
public void setRememberMeServices(RememberMeServices rememberMeServices)
-
setContinueChainBeforeSuccessfulAuthentication
public void setContinueChainBeforeSuccessfulAuthentication(boolean continueChainBeforeSuccessfulAuthentication)
Indicates if the filter chain should be continued prior to delegation tosuccessfulAuthentication(HttpServletRequest, HttpServletResponse, FilterChain, Authentication), which may be useful in certain environment (such as Tapestry applications). Defaults tofalse.
-
setApplicationEventPublisher
public void setApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher eventPublisher)
- Specified by:
setApplicationEventPublisherin interfaceorg.springframework.context.ApplicationEventPublisherAware
-
setAuthenticationDetailsSource
public void setAuthenticationDetailsSource(org.springframework.security.authentication.AuthenticationDetailsSource<javax.servlet.http.HttpServletRequest,?> authenticationDetailsSource)
-
setMessageSource
public void setMessageSource(org.springframework.context.MessageSource messageSource)
- Specified by:
setMessageSourcein interfaceorg.springframework.context.MessageSourceAware
-
getAllowSessionCreation
protected boolean getAllowSessionCreation()
-
setAllowSessionCreation
public void setAllowSessionCreation(boolean allowSessionCreation)
-
setSessionAuthenticationStrategy
public void setSessionAuthenticationStrategy(SessionAuthenticationStrategy sessionStrategy)
The session handling strategy which will be invoked immediately after an authentication request is successfully processed by the AuthenticationManager. Used, for example, to handle changing of the session identifier to prevent session fixation attacks.- Parameters:
sessionStrategy- the implementation to use. If not set a null implementation is used.
-
setAuthenticationSuccessHandler
public void setAuthenticationSuccessHandler(AuthenticationSuccessHandler successHandler)
Sets the strategy used to handle a successful authentication. By default aSavedRequestAwareAuthenticationSuccessHandleris used.
-
setAuthenticationFailureHandler
public void setAuthenticationFailureHandler(AuthenticationFailureHandler failureHandler)
-
getSuccessHandler
protected AuthenticationSuccessHandler getSuccessHandler()
-
getFailureHandler
protected AuthenticationFailureHandler getFailureHandler()
-
-