Class SessionManagementConfigurer<H extends HttpSecurityBuilder<H>>
java.lang.Object
org.springframework.security.config.annotation.SecurityConfigurerAdapter<DefaultSecurityFilterChain,B>
org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer<SessionManagementConfigurer<H>,H>
org.springframework.security.config.annotation.web.configurers.SessionManagementConfigurer<H>
- All Implemented Interfaces:
SecurityConfigurer<DefaultSecurityFilterChain,H>
public final class SessionManagementConfigurer<H extends HttpSecurityBuilder<H>>
extends AbstractHttpConfigurer<SessionManagementConfigurer<H>,H>
Allows configuring session management.
Security Filters
The following Filters are populatedSessionManagementFilterConcurrentSessionFilterif there are restrictions on how many concurrent sessions a user can have
Shared Objects Created
The following shared objects are created:Shared Objects Used
SecurityContextRepositoryAuthenticationTrustResolveris optionally used to populate theHttpSessionSecurityContextRepositoryandSessionManagementFilter
- Since:
- 3.2
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionfinal classAllows configuring controlling of multiple sessions.final classAllows configuring SessionFixation protection -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaddSessionAuthenticationStrategy(SessionAuthenticationStrategy sessionAuthenticationStrategy) Adds an additionalSessionAuthenticationStrategyto be used within theCompositeSessionAuthenticationStrategy.voidConfigure theSecurityBuilderby setting the necessary properties on theSecurityBuilder.enableSessionUrlRewriting(boolean enableSessionUrlRewriting) If set to true, allows HTTP sessions to be rewritten in the URLs when usingHttpServletResponse.encodeRedirectURL(String)orHttpServletResponse.encodeURL(String), otherwise disallows HTTP sessions to be included in the URL.voidInitialize theSecurityBuilder.invalidSessionStrategy(InvalidSessionStrategy invalidSessionStrategy) Setting this attribute will inject the provided invalidSessionStrategy into theSessionManagementFilter.invalidSessionUrl(String invalidSessionUrl) Setting this attribute will inject theSessionManagementFilterwith aSimpleRedirectInvalidSessionStrategyconfigured with the attribute value.maximumSessions(int maximumSessions) Controls the maximum number of sessions for a user.requireExplicitAuthenticationStrategy(boolean requireExplicitAuthenticationStrategy) Setting this means that explicit invocation ofSessionAuthenticationStrategyis required.sessionAuthenticationErrorUrl(String sessionAuthenticationErrorUrl) Defines the URL of the error page which should be shown when the SessionAuthenticationStrategy raises an exception.sessionAuthenticationFailureHandler(AuthenticationFailureHandler sessionAuthenticationFailureHandler) Defines theAuthenticationFailureHandlerwhich will be used when the SessionAuthenticationStrategy raises an exception.sessionAuthenticationStrategy(SessionAuthenticationStrategy sessionAuthenticationStrategy) Allows explicitly specifying theSessionAuthenticationStrategy.sessionConcurrency(Customizer<SessionManagementConfigurer<H>.ConcurrencyControlConfigurer> sessionConcurrencyCustomizer) Controls the maximum number of sessions for a user.sessionCreationPolicy(SessionCreationPolicy sessionCreationPolicy) Allows specifying theSessionCreationPolicyAllows changing the defaultSessionFixationProtectionStrategy.sessionFixation(Customizer<SessionManagementConfigurer<H>.SessionFixationConfigurer> sessionFixationCustomizer) Allows configuring session fixation protection.Methods inherited from class org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer
disable, getSecurityContextHolderStrategy, withObjectPostProcessor, withObjectPostProcessorMethods inherited from class org.springframework.security.config.annotation.SecurityConfigurerAdapter
addObjectPostProcessor, addObjectPostProcessor, and, getBuilder, postProcess, setBuilder
-
Constructor Details
-
SessionManagementConfigurer
public SessionManagementConfigurer()Creates a new instance- See Also:
-
-
Method Details
-
invalidSessionUrl
Setting this attribute will inject theSessionManagementFilterwith aSimpleRedirectInvalidSessionStrategyconfigured with the attribute value. When an invalid session ID is submitted, the strategy will be invoked, redirecting to the configured URL.- Parameters:
invalidSessionUrl- the URL to redirect to when an invalid session is detected- Returns:
- the
SessionManagementConfigurerfor further customization
-
requireExplicitAuthenticationStrategy
public SessionManagementConfigurer<H> requireExplicitAuthenticationStrategy(boolean requireExplicitAuthenticationStrategy) Setting this means that explicit invocation ofSessionAuthenticationStrategyis required.- Parameters:
requireExplicitAuthenticationStrategy- require explicit invocation ofSessionAuthenticationStrategy- Returns:
- the
SessionManagementConfigurerfor further customization
-
invalidSessionStrategy
public SessionManagementConfigurer<H> invalidSessionStrategy(InvalidSessionStrategy invalidSessionStrategy) Setting this attribute will inject the provided invalidSessionStrategy into theSessionManagementFilter. When an invalid session ID is submitted, the strategy will be invoked, redirecting to the configured URL.- Parameters:
invalidSessionStrategy- the strategy to use when an invalid session ID is submitted.- Returns:
- the
SessionManagementConfigurerfor further customization
-
sessionAuthenticationErrorUrl
public SessionManagementConfigurer<H> sessionAuthenticationErrorUrl(String sessionAuthenticationErrorUrl) Defines the URL of the error page which should be shown when the SessionAuthenticationStrategy raises an exception. If not set, an unauthorized (402) error code will be returned to the client. Note that this attribute doesn't apply if the error occurs during a form-based login, where the URL for authentication failure will take precedence.- Parameters:
sessionAuthenticationErrorUrl- the URL to redirect to- Returns:
- the
SessionManagementConfigurerfor further customization
-
sessionAuthenticationFailureHandler
public SessionManagementConfigurer<H> sessionAuthenticationFailureHandler(AuthenticationFailureHandler sessionAuthenticationFailureHandler) Defines theAuthenticationFailureHandlerwhich will be used when the SessionAuthenticationStrategy raises an exception. If not set, an unauthorized (402) error code will be returned to the client. Note that this attribute doesn't apply if the error occurs during a form-based login, where the URL for authentication failure will take precedence.- Parameters:
sessionAuthenticationFailureHandler- the handler to use- Returns:
- the
SessionManagementConfigurerfor further customization
-
enableSessionUrlRewriting
If set to true, allows HTTP sessions to be rewritten in the URLs when usingHttpServletResponse.encodeRedirectURL(String)orHttpServletResponse.encodeURL(String), otherwise disallows HTTP sessions to be included in the URL. This prevents leaking information to external domains.This is achieved by guarding
HttpServletResponse.encodeURL(java.lang.String)andHttpServletResponse.encodeRedirectURL(java.lang.String)invocations. Any code that also overrides either of these two methods, likeResourceUrlEncodingFilter, needs to come after the security filter chain or risk being skipped.- Parameters:
enableSessionUrlRewriting- true if should allow the JSESSIONID to be rewritten into the URLs, else false (default)- Returns:
- the
SessionManagementConfigurerfor further customization - See Also:
-
sessionCreationPolicy
public SessionManagementConfigurer<H> sessionCreationPolicy(SessionCreationPolicy sessionCreationPolicy) Allows specifying theSessionCreationPolicy- Parameters:
sessionCreationPolicy- theSessionCreationPolicyto use. Cannot be null.- Returns:
- the
SessionManagementConfigurerfor further customizations - Throws:
IllegalArgumentException- ifSessionCreationPolicyis null.- See Also:
-
sessionAuthenticationStrategy
public SessionManagementConfigurer<H> sessionAuthenticationStrategy(SessionAuthenticationStrategy sessionAuthenticationStrategy) Allows explicitly specifying theSessionAuthenticationStrategy. The default is to useChangeSessionIdAuthenticationStrategy. If restricting the maximum number of sessions is configured, thenCompositeSessionAuthenticationStrategydelegating toConcurrentSessionControlAuthenticationStrategy, the default OR suppliedSessionAuthenticationStrategyandRegisterSessionAuthenticationStrategy.NOTE: Supplying a custom
SessionAuthenticationStrategywill override the default session fixation strategy.- Parameters:
sessionAuthenticationStrategy-- Returns:
- the
SessionManagementConfigurerfor further customizations
-
addSessionAuthenticationStrategy
public SessionManagementConfigurer<H> addSessionAuthenticationStrategy(SessionAuthenticationStrategy sessionAuthenticationStrategy) Adds an additionalSessionAuthenticationStrategyto be used within theCompositeSessionAuthenticationStrategy.- Parameters:
sessionAuthenticationStrategy-- Returns:
- the
SessionManagementConfigurerfor further customizations
-
sessionFixation
Allows changing the defaultSessionFixationProtectionStrategy.- Returns:
- the
SessionManagementConfigurer<H extends HttpSecurityBuilder<H>>.SessionFixationConfigurerfor further customizations
-
sessionFixation
public SessionManagementConfigurer<H> sessionFixation(Customizer<SessionManagementConfigurer<H>.SessionFixationConfigurer> sessionFixationCustomizer) Allows configuring session fixation protection.- Parameters:
sessionFixationCustomizer- theCustomizerto provide more options for theSessionManagementConfigurer<H extends HttpSecurityBuilder<H>>.SessionFixationConfigurer- Returns:
- the
SessionManagementConfigurerfor further customizations
-
maximumSessions
public SessionManagementConfigurer<H>.ConcurrencyControlConfigurer maximumSessions(int maximumSessions) Controls the maximum number of sessions for a user. The default is to allow any number of sessions.- Parameters:
maximumSessions- the maximum number of sessions for a user- Returns:
- the
SessionManagementConfigurerfor further customizations
-
sessionConcurrency
public SessionManagementConfigurer<H> sessionConcurrency(Customizer<SessionManagementConfigurer<H>.ConcurrencyControlConfigurer> sessionConcurrencyCustomizer) Controls the maximum number of sessions for a user. The default is to allow any number of users.- Parameters:
sessionConcurrencyCustomizer- theCustomizerto provide more options for theSessionManagementConfigurer<H extends HttpSecurityBuilder<H>>.ConcurrencyControlConfigurer- Returns:
- the
SessionManagementConfigurerfor further customizations
-
init
Description copied from interface:SecurityConfigurerInitialize theSecurityBuilder. Here only shared state should be created and modified, but not properties on theSecurityBuilderused for building the object. This ensures that theSecurityConfigurer.configure(SecurityBuilder)method uses the correct shared objects when building. Configurers should be applied here.- Specified by:
initin interfaceSecurityConfigurer<DefaultSecurityFilterChain,H extends HttpSecurityBuilder<H>> - Overrides:
initin classSecurityConfigurerAdapter<DefaultSecurityFilterChain,H extends HttpSecurityBuilder<H>>
-
configure
Description copied from interface:SecurityConfigurerConfigure theSecurityBuilderby setting the necessary properties on theSecurityBuilder.- Specified by:
configurein interfaceSecurityConfigurer<DefaultSecurityFilterChain,H extends HttpSecurityBuilder<H>> - Overrides:
configurein classSecurityConfigurerAdapter<DefaultSecurityFilterChain,H extends HttpSecurityBuilder<H>>
-