Class SessionRepositoryFilter<S extends Session>
java.lang.Object
org.springframework.session.web.http.SessionRepositoryFilter<S>
- Type Parameters:
S- theSessiontype.
- All Implemented Interfaces:
javax.servlet.Filter
Switches the
HttpSession implementation to be backed by a
Session.
The SessionRepositoryFilter wraps the
HttpServletRequest and overrides the methods to get an
HttpSession to be backed by a
Session returned by the
SessionRepository.
The SessionRepositoryFilter uses a HttpSessionIdResolver (default
CookieHttpSessionIdResolver) to bridge logic between an
HttpSession and the
Session abstraction. Specifically:
- The session id is looked up using
HttpSessionIdResolver.resolveSessionIds(javax.servlet.http.HttpServletRequest). The default is to look in a cookie named SESSION. - The session id of newly created
Sessionis sent to the client usingHttpSessionIdResolver.setSessionId(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, String) - The client is notified that the session id is no longer valid with
HttpSessionIdResolver.expireSession(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
The SessionRepositoryFilter must be placed before any Filter that access the HttpSession or that might commit the response to ensure the session is overridden and persisted properly.
- Since:
- 1.0
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringSuffix that gets appended to the filter name for the "already filtered" request attribute.static final intThe default filter order.static final StringInvalid session id (not backed by the session repository) request attribute name.static final StringThe session repository request attribute name. -
Constructor Summary
ConstructorsConstructorDescriptionSessionRepositoryFilter(SessionRepository<S> sessionRepository) Creates a new instance. -
Method Summary
Modifier and TypeMethodDescriptionvoiddestroy()final voiddoFilter(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, javax.servlet.FilterChain filterChain) ThisdoFilterimplementation stores a request attribute for "already filtered", proceeding without filtering again if the attribute is already there.protected voiddoFilterInternal(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, javax.servlet.FilterChain filterChain) Same contract as fordoFilter, but guaranteed to be just invoked once per request within a single request thread.protected voiddoFilterNestedErrorDispatch(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, javax.servlet.FilterChain filterChain) Typically an ERROR dispatch happens after the REQUEST dispatch completes, and the filter chain starts anew.protected StringReturn the name of the request attribute that identifies that a request is already filtered.voidinit(javax.servlet.FilterConfig config) voidsetHttpSessionIdResolver(HttpSessionIdResolver httpSessionIdResolver) Sets theHttpSessionIdResolverto be used.
-
Field Details
-
SESSION_REPOSITORY_ATTR
The session repository request attribute name. -
INVALID_SESSION_ID_ATTR
Invalid session id (not backed by the session repository) request attribute name. -
DEFAULT_ORDER
public static final int DEFAULT_ORDERThe default filter order.- See Also:
-
ALREADY_FILTERED_SUFFIX
Suffix that gets appended to the filter name for the "already filtered" request attribute.- See Also:
-
-
Constructor Details
-
SessionRepositoryFilter
Creates a new instance.- Parameters:
sessionRepository- theSessionRepositoryto use. Cannot be null.
-
-
Method Details
-
setHttpSessionIdResolver
Sets theHttpSessionIdResolverto be used. The default is aCookieHttpSessionIdResolver.- Parameters:
httpSessionIdResolver- theHttpSessionIdResolverto use. Cannot be null.
-
doFilterInternal
protected void doFilterInternal(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, javax.servlet.FilterChain filterChain) throws javax.servlet.ServletException, IOException Same contract as fordoFilter, but guaranteed to be just invoked once per request within a single request thread.Provides HttpServletRequest and HttpServletResponse arguments instead of the default ServletRequest and ServletResponse ones.
- Parameters:
request- the requestresponse- the responsefilterChain- the FilterChain- Throws:
javax.servlet.ServletException- thrown when a non-I/O exception has occurredIOException- thrown when an I/O exception of some sort has occurred- See Also:
-
Filter.doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
-
doFilterNestedErrorDispatch
protected void doFilterNestedErrorDispatch(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, javax.servlet.FilterChain filterChain) throws javax.servlet.ServletException, IOException Typically an ERROR dispatch happens after the REQUEST dispatch completes, and the filter chain starts anew. On some servers however the ERROR dispatch may be nested within the REQUEST dispatch, e.g. as a result of callingsendErroron the response. In that case we are still in the filter chain, on the same thread, but the request and response have been switched to the original, unwrapped ones.Sub-classes may use this method to filter such nested ERROR dispatches and re-apply wrapping on the request or response.
ThreadLocalcontext, if any, should still be active as we are still nested within the filter chain.- Parameters:
request- the requestresponse- the responsefilterChain- the filter chain- Throws:
javax.servlet.ServletException- if request is not HTTP requestIOException- in case of I/O operation exception
-
doFilter
public final void doFilter(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, javax.servlet.FilterChain filterChain) throws javax.servlet.ServletException, IOException ThisdoFilterimplementation stores a request attribute for "already filtered", proceeding without filtering again if the attribute is already there.- Specified by:
doFilterin interfacejavax.servlet.Filter- Parameters:
request- the requestresponse- the responsefilterChain- the filter chain- Throws:
javax.servlet.ServletException- if request is not HTTP requestIOException- in case of I/O operation exception
-
getAlreadyFilteredAttributeName
Return the name of the request attribute that identifies that a request is already filtered.The default implementation takes the configured name of the concrete filter instance and appends ".FILTERED". If the filter is not fully initialized, it falls back to its class name.
- Returns:
- the name of request attribute indicating already filtered request
- See Also:
-
init
public void init(javax.servlet.FilterConfig config) - Specified by:
initin interfacejavax.servlet.Filter
-
destroy
public void destroy()- Specified by:
destroyin interfacejavax.servlet.Filter
-