Skip to content

When using stateless session creation policy and form login, a session shouldn't be created when authentication fails #4242

@theloom

Description

@theloom

Summary

When using session creation policy = STATELESS and form login, when authentication fails, the default failure handler creates a session but that is unexpected.

Actual Behavior

SimpleUrlAuthenticationFailureHandler is the default failure handler based on the below configuration. It has allowSessionCreation = true. That causes it to create a session even when session creation policy is stateless: as evidenced by seeing a JSESSIONID in the response from authentication failure.

Expected Behavior

SimpleUrlAuthenticationFailureHandler should obey session creation policy.

There is a workaround because that handler has setter setAllowSessionCreation, so you can manually configure it. But, would it be desirable for it to follow session creation policy instead?

If you implement the workaround, then you no longer see a JSESSIONID in the response from authentication failure.

Configuration

protected void configure(HttpSecurity httpSecurity) throws Exception
{
    httpSecurity.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
    httpSecurity.formLogin()
       .authenticationDetailsSource(createMyAuthenticationDetailsSource())
       .successHandler(createMyStatelessAuthenticationSuccessHandler());
}

Version

Spring Security 4.1.3 (Spring Boot 1.4.2)

Sample

Example workaround if you wire SimpleUrlAuthenticationFailureHandler and setAllowSessionCreation(false):

@Bean(autowire = Autowire.BY_TYPE)
public SimpleUrlAuthenticationFailureHandler createFailureHandler()
{
    SimpleUrlAuthenticationFailureHandler failureHandler = new SimpleUrlAuthenticationFailureHandler();
    failureHandler.setAllowSessionCreation(false);
    return failureHandler;
}

protected void configure(HttpSecurity httpSecurity) throws Exception
{
    httpSecurity.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
    httpSecurity.formLogin()
       .authenticationDetailsSource(createMyAuthenticationDetailsSource())
       .successHandler(createMyStatelessAuthenticationSuccessHandler())
       .failureHandler(createFailureHandler());
}

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions