Package org.apache.shiro.web.servlet
Class AbstractShiroFilter
- java.lang.Object
-
- org.apache.shiro.web.servlet.ServletContextSupport
-
- org.apache.shiro.web.servlet.AbstractFilter
-
- org.apache.shiro.web.servlet.NameableFilter
-
- org.apache.shiro.web.servlet.OncePerRequestFilter
-
- org.apache.shiro.web.servlet.AbstractShiroFilter
-
- All Implemented Interfaces:
javax.servlet.Filter,org.apache.shiro.util.Nameable
- Direct Known Subclasses:
IniShiroFilter,ShiroFilter
public abstract class AbstractShiroFilter extends OncePerRequestFilter
Abstract base class that provides all standard Shiro request filtering behavior and expects subclasses to implement configuration-specific logic (INI, XML, .properties, etc). Subclasses should perform configuration and construction logic in an overriddeninit()method implementation. That implementation should make available any constructedSecurityManagerandFilterChainResolverby callingsetSecurityManager(org.apache.shiro.web.mgt.WebSecurityManager)andsetFilterChainResolver(org.apache.shiro.web.filter.mgt.FilterChainResolver)methods respectively.Static SecurityManager
By default theSecurityManagerinstance enabled by this filter will not be enabled in static memory via theSecurityUtils.setSecurityManagermethod. Instead, it is expected that Subject instances will always be constructed on a request-processing thread via instances of this Filter class. However, if you need to constructSubjectinstances on separate (non request-processing) threads, it might be easiest to enable the SecurityManager to be available in static memory via theSecurityUtils.getSecurityManager()method. You can do this by additionally specifying aninit-param:<filter> ... other config here ... <init-param> <param-name>staticSecurityManagerEnabled</param-name> <param-value>true</param-value> </init-param> </filter>See the Shiro Subject documentation for more information as to if you would do this, particularly the sections on theSubject.Builderand Thread Association.- Since:
- 1.0
- See Also:
- Subject documentation
-
-
Field Summary
-
Fields inherited from class org.apache.shiro.web.servlet.OncePerRequestFilter
ALREADY_FILTERED_SUFFIX
-
Fields inherited from class org.apache.shiro.web.servlet.AbstractFilter
filterConfig
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractShiroFilter()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected WebSecurityManagercreateDefaultSecurityManager()protected WebSubjectcreateSubject(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response)Creates aWebSubjectinstance to associate with the incoming request/response pair which will be used throughout the request/response execution.protected voiddoFilterInternal(javax.servlet.ServletRequest servletRequest, javax.servlet.ServletResponse servletResponse, javax.servlet.FilterChain chain)doFilterInternalimplementation that sets-up, executes, and cleans-up a Shiro-filtered request.protected voidexecuteChain(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, javax.servlet.FilterChain origChain)Executes aFilterChainfor the given request.protected javax.servlet.FilterChaingetExecutionChain(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, javax.servlet.FilterChain origChain)Returns theFilterChainto execute for the given request.FilterChainResolvergetFilterChainResolver()WebSecurityManagergetSecurityManager()voidinit()protected booleanisHttpSessions()booleanisStaticSecurityManagerEnabled()Returnstrueif the constructedsecurityManagerreference should be bound to static memory (viaSecurityUtils.setSecurityManager),falseotherwise.protected voidonFilterConfigSet()Template method to be overridden by subclasses to perform initialization logic at start-up.protected javax.servlet.ServletRequestprepareServletRequest(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, javax.servlet.FilterChain chain)Prepares theServletRequestinstance that will be passed to theFilterChainfor request processing.protected javax.servlet.ServletResponseprepareServletResponse(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, javax.servlet.FilterChain chain)Prepares theServletResponseinstance that will be passed to theFilterChainfor request processing.voidsetFilterChainResolver(FilterChainResolver filterChainResolver)voidsetSecurityManager(WebSecurityManager sm)voidsetStaticSecurityManagerEnabled(boolean staticSecurityManagerEnabled)Sets if the constructedsecurityManagerreference should be bound to static memory (viaSecurityUtils.setSecurityManager).protected voidupdateSessionLastAccessTime(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response)Updates any 'native' Session's last access time that might exist to the timestamp when this method is called.protected javax.servlet.ServletRequestwrapServletRequest(javax.servlet.http.HttpServletRequest orig)Wraps the original HttpServletRequest in aShiroHttpServletRequest, which is required for supporting Servlet Specification behavior backed by aSubjectinstance.protected javax.servlet.ServletResponsewrapServletResponse(javax.servlet.http.HttpServletResponse orig, ShiroHttpServletRequest request)Returns a newShiroHttpServletResponseinstance, wrapping theorigargument, in order to provide correct URL rewriting behavior required by the Servlet Specification when using Shiro-based sessions (and not Servlet Container HTTP-based sessions).-
Methods inherited from class org.apache.shiro.web.servlet.OncePerRequestFilter
doFilter, getAlreadyFilteredAttributeName, isEnabled, isEnabled, setEnabled, shouldNotFilter
-
Methods inherited from class org.apache.shiro.web.servlet.NameableFilter
getName, setName, toStringBuilder
-
Methods inherited from class org.apache.shiro.web.servlet.AbstractFilter
destroy, getFilterConfig, getInitParam, init, setFilterConfig
-
Methods inherited from class org.apache.shiro.web.servlet.ServletContextSupport
getContextAttribute, getContextInitParam, getServletContext, removeContextAttribute, setContextAttribute, setServletContext, toString
-
-
-
-
Method Detail
-
getSecurityManager
public WebSecurityManager getSecurityManager()
-
setSecurityManager
public void setSecurityManager(WebSecurityManager sm)
-
getFilterChainResolver
public FilterChainResolver getFilterChainResolver()
-
setFilterChainResolver
public void setFilterChainResolver(FilterChainResolver filterChainResolver)
-
isStaticSecurityManagerEnabled
public boolean isStaticSecurityManagerEnabled()
Returnstrueif the constructedsecurityManagerreference should be bound to static memory (viaSecurityUtils.setSecurityManager),falseotherwise. The default value isfalse.- Returns:
trueif the constructedsecurityManagerreference should be bound to static memory (viaSecurityUtils.setSecurityManager),falseotherwise.- Since:
- 1.2
- See Also:
- SHIRO-287
-
setStaticSecurityManagerEnabled
public void setStaticSecurityManagerEnabled(boolean staticSecurityManagerEnabled)
Sets if the constructedsecurityManagerreference should be bound to static memory (viaSecurityUtils.setSecurityManager). The default value isfalse.- Parameters:
staticSecurityManagerEnabled- if the constructedsecurityManagerreference should be bound to static memory (viaSecurityUtils.setSecurityManager).- Since:
- 1.2
- See Also:
- SHIRO-287
-
onFilterConfigSet
protected final void onFilterConfigSet() throws ExceptionDescription copied from class:AbstractFilterTemplate method to be overridden by subclasses to perform initialization logic at start-up. TheServletContextandFilterConfigwill be accessible (and non-null) at the time this method is invoked via thegetServletContext()andgetFilterConfig()methods respectively.init-paramvalues may be conveniently obtained via theAbstractFilter.getInitParam(String)method.- Overrides:
onFilterConfigSetin classAbstractFilter- Throws:
Exception- if the subclass has an error upon initialization.
-
createDefaultSecurityManager
protected WebSecurityManager createDefaultSecurityManager()
-
isHttpSessions
protected boolean isHttpSessions()
-
wrapServletRequest
protected javax.servlet.ServletRequest wrapServletRequest(javax.servlet.http.HttpServletRequest orig)
Wraps the original HttpServletRequest in aShiroHttpServletRequest, which is required for supporting Servlet Specification behavior backed by aSubjectinstance.- Parameters:
orig- the original Servlet Container-provided incomingHttpServletRequestinstance.- Returns:
ShiroHttpServletRequestinstance wrapping the original.- Since:
- 1.0
-
prepareServletRequest
protected javax.servlet.ServletRequest prepareServletRequest(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, javax.servlet.FilterChain chain)Prepares theServletRequestinstance that will be passed to theFilterChainfor request processing. If theServletRequestis an instance ofHttpServletRequest, the value returned from this method is obtained by callingwrapServletRequest(javax.servlet.http.HttpServletRequest)to allow Shiro-specific HTTP behavior, otherwise the originalServletRequestargument is returned.- Parameters:
request- the incoming ServletRequestresponse- the outgoing ServletResponsechain- the Servlet Container providedFilterChainthat will receive the returned request.- Returns:
- the
ServletRequestinstance that will be passed to theFilterChainfor request processing. - Since:
- 1.0
-
wrapServletResponse
protected javax.servlet.ServletResponse wrapServletResponse(javax.servlet.http.HttpServletResponse orig, ShiroHttpServletRequest request)Returns a newShiroHttpServletResponseinstance, wrapping theorigargument, in order to provide correct URL rewriting behavior required by the Servlet Specification when using Shiro-based sessions (and not Servlet Container HTTP-based sessions).- Parameters:
orig- the originalHttpServletResponseinstance provided by the Servlet Container.request- theShiroHttpServletRequestinstance wrapping the original request.- Returns:
- the wrapped ServletResponse instance to use during
FilterChainexecution. - Since:
- 1.0
-
prepareServletResponse
protected javax.servlet.ServletResponse prepareServletResponse(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, javax.servlet.FilterChain chain)Prepares theServletResponseinstance that will be passed to theFilterChainfor request processing. This implementation delegates towrapServletRequest(javax.servlet.http.HttpServletRequest)only if Shiro-based sessions are enabled (that is, !isHttpSessions()) and the request instance is aShiroHttpServletRequest. This ensures that any URL rewriting that occurs is handled correctly using the Shiro-managed Session's sessionId and not a servlet container session ID. If HTTP-based sessions are enabled (the default), then this method does nothing and just returns theServletResponseargument as-is, relying on the default Servlet Container URL rewriting logic.- Parameters:
request- the incoming ServletRequestresponse- the outgoing ServletResponsechain- the Servlet Container providedFilterChainthat will receive the returned request.- Returns:
- the
ServletResponseinstance that will be passed to theFilterChainduring request processing. - Since:
- 1.0
-
createSubject
protected WebSubject createSubject(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response)
Creates aWebSubjectinstance to associate with the incoming request/response pair which will be used throughout the request/response execution.- Parameters:
request- the incomingServletRequestresponse- the outgoingServletResponse- Returns:
- the
WebSubjectinstance to associate with the request/response execution - Since:
- 1.0
-
updateSessionLastAccessTime
protected void updateSessionLastAccessTime(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response)Updates any 'native' Session's last access time that might exist to the timestamp when this method is called. If native sessions are not enabled (that is, standard Servlet container sessions are being used) or there is no session (subject.getSession(false) == null), this method does nothing. This method implementation merely callsSession.on the session.touch()- Parameters:
request- incoming request - ignored, but available to subclasses that might wish to override this methodresponse- outgoing response - ignored, but available to subclasses that might wish to override this method- Since:
- 1.0
-
doFilterInternal
protected void doFilterInternal(javax.servlet.ServletRequest servletRequest, javax.servlet.ServletResponse servletResponse, javax.servlet.FilterChain chain) throws javax.servlet.ServletException, IOExceptiondoFilterInternalimplementation that sets-up, executes, and cleans-up a Shiro-filtered request. It performs the following ordered operations:Preparesthe incomingServletRequestfor use during Shiro's processingPreparesthe outgoingServletResponsefor use during Shiro's processing-
CreatesaSubjectinstance based on the specified request/response pair. - Finally
executestheupdateSessionLastAccessTime(javax.servlet.ServletRequest, javax.servlet.ServletResponse)andexecuteChain(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)methods
Subject.execute(Runnable)call in step #4 is used as an implementation technique to guarantee proper thread binding and restoration is completed successfully.- Specified by:
doFilterInternalin classOncePerRequestFilter- Parameters:
servletRequest- the incomingServletRequestservletResponse- the outgoingServletResponsechain- the container-providedFilterChainto execute- Throws:
IOException- if an IO error occursjavax.servlet.ServletException- if an Throwable other than an IOException
-
getExecutionChain
protected javax.servlet.FilterChain getExecutionChain(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, javax.servlet.FilterChain origChain)Returns theFilterChainto execute for the given request. TheorigChainargument is the originalFilterChainsupplied by the Servlet Container, but it may be modified to provide more behavior by pre-pending further chains according to the Shiro configuration. This implementation returns the chain that will actually be executed by acquiring the chain from afilterChainResolver. The resolver determines exactly which chain to execute, typically based on URL configuration. If no chain is returned from the resolver call (returnsnull), then theorigChainwill be returned by default.- Parameters:
request- the incoming ServletRequestresponse- the outgoing ServletResponseorigChain- the originalFilterChainprovided by the Servlet Container- Returns:
- the
FilterChainto execute for the given request - Since:
- 1.0
-
executeChain
protected void executeChain(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, javax.servlet.FilterChain origChain) throws IOException, javax.servlet.ServletExceptionExecutes aFilterChainfor the given request. This implementation first delegates toto allow the application's Shiro configuration to determine exactly how the chain should execute. The resulting value from that call is then executed directly by calling the returnedgetExecutionChainFilterChain'sdoFiltermethod. That is:FilterChain chain =
getExecutionChain(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)(request, response, origChain); chain.doFilter(request,response);- Parameters:
request- the incoming ServletRequestresponse- the outgoing ServletResponseorigChain- the Servlet Container-provided chain that may be wrapped further by an application-configured chain of Filters.- Throws:
IOException- if the underlyingchain.doFiltercall results in an IOExceptionjavax.servlet.ServletException- if the underlyingchain.doFiltercall results in a ServletException- Since:
- 1.0
-
-