spring-projects / spring-projects/spring-security

Update docs for stateless session management in 6.x

Open
#13,867 4 comments 0 reactions 1 assignee View on GitHub

@marameref is already working on this.

Since Oct 13, 2023.

in: docs type: enhancement
Dominant language
Java
Stars
9.6k
Forks
6.3k
Avg merge
2d 11h
Merged PRs (30d)
52

Description

Describe the bug
Spring Security uses RequestAttributeSecurityContextRepository instead of the NullSecurityContextRepository for Stateless sessions.

To Reproduce
Run the program with minimal configuration:

@Configuration
class SecurityConfig {
    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http, UserDetailsService userDetailsService) throws Exception {
        http.authorizeHttpRequests(requests -> requests
                .anyRequest().authenticated()
        );
        http.userDetailsService(userDetailsService);
        http.httpBasic(Customizer.withDefaults());
        http.sessionManagement(sessionConfig -> sessionConfig.sessionCreationPolicy(SessionCreationPolicy.STATELESS));
        http.csrf(csrfCustomizer -> csrfCustomizer.disable());
        return http.build();
    }

    @Bean
    UserDetailsService userDetailsService() {
        InMemoryUserDetailsManager inMemoryUserDetailsManager = new InMemoryUserDetailsManager();
        inMemoryUserDetailsManager.createUser(
                User.builder().username("john").password("pass").roles("USER").build()
        );
        return inMemoryUserDetailsManager;
    }
}

Expected behavior
According to the documentation, Spring should use NullSecurityContextRepository but RequestAttributeSecurityContextRepository is used instead

Sample
There is a test in below repository that checks SecurityContextRepository used by BasicAuthenticationFilter

https://github.com/slawekludwiczak/spring-security-basic-repository

PS. maybe I don't understand something, but it is a bit weird for me that for Stateless sessions there is SessionManagementFilter registered

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.