spring-projects / spring-projects/spring-security

Implementing Stateless Sessions with Spring Security and SAML2 Service Provider in Java Application

Open
#13,594 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

status: waiting-for-triage type: bug
Dominant language
Java
Stars
9.6k
Forks
6.3k
Avg merge
2d 11h
Merged PRs (30d)
52

Description

My Java application uses spring security and Saml2 as a service provider. I am able to integrate with the identity provider and get saml assertions correctly. I want to avoid using JSESSIONID for overhead in my application since I have custom session attribute x-auth as cookie.

But, I see that a session is created every time and remains persistent till the user sign's out.

I tried adding the below line to the configure method, http.sessionManagement() .sessionCreationPolicy(SessionCreationPolicy.STATELESS);

But, with this setting I am always being asked to login to Identity Provider.

If I login, it comes back to the page asking me to login again in a loop.

I am not sure if this is the correct behaviour when disabling sessions.

Can anyone provide me with the right approach to use stateless sessions with below code ?


 @Bean
  SecurityFilterChain configure(HttpSecurity http) throws Exception {
    OpenSaml4AuthenticationProvider authenticationProvider = new OpenSaml4AuthenticationProvider();
    authenticationProvider.setResponseAuthenticationConverter(createTokenCredential());

    Saml2MetadataFilter filter = new Saml2MetadataFilter(relyingPartyRegistrationRepository,
        new OpenSamlMetadataResolver());
    http.addFilterBefore(filter, Saml2WebSsoAuthenticationFilter.class);
    loginSuccessHandler.setTargetUrlParameter(Saml2ParameterNames.RELAY_STATE);

    http.authorizeHttpRequests(requests -> requests
            .requestMatchers("/saml2/service-provider-metadata/**")
            .permitAll()
        ).addFilterAfter(authTokenSessionRestore, SecurityContextHolderFilter.class)
        .saml2Login((saml2) -> saml2
            .loginProcessingUrl("/saml/SSO")
            .authenticationManager(new ProviderManager(authenticationProvider))
            .relyingPartyRegistrationRepository(relyingPartyRegistrationRepository)
            .successHandler(loginSuccessHandler)
            .failureHandler(loginFailureHandler)

        ).saml2Logout(withDefaults())
        .logout().logoutSuccessUrl("/login")
        .logoutSuccessHandler(logoutSuccessHandler);



    http.sessionManagement(session -> {
          session.sessionCreationPolicy(SessionCreationPolicy.STATELESS);
    }); // dont create a session for this configuration
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the SecurityFilterChain configure entry point and trace the interaction between SessionCreationPolicy.STATELESS, the SAML login processing URL, and authTokenSessionRestore. Check how authentication is restored after the identity provider callback and whether the configured custom cookie prevents the login loop; done means SAML login completes without repeated redirects or persistent JSESSIONID use.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
authentication, security
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.