spring-projects / spring-projects/spring-security

HttpSecurity bean has no option to disable defaults

Open
#11,633 12 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Starting from 5.7 the WebSecurityConfigurerAdapter has been deprecated in favour of using a custom bean for creating a SecurityFilterChain that will inject an HttpSecurity and build it.

The WebSecurityConfigurerAdapter has a constructor field disableDefaults which was used to determine whether defaults should be applied to the configuration or not.

When that flag was set to true then the default configuration and default configurers would not be applied to it. When using the new recommended approach there is no way to disable these defaults.

What would be the recommended approach for disabling the defaults.

Does it perhaps make sense to have an interface like:

public interface HttpSecurityProvider {

    default HttpSecurity create() {
        return create(false);
    }

    HttpSecurity create(boolean disableDefaults);
}

Then instead of doing:

@Configuration
public class SecurityConfiguration {

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http
            .authorizeHttpRequests((authz) -> authz
                .anyRequest().authenticated()
            )
            .httpBasic(withDefaults());
        return http.build();
    }

}

we can do:

@Configuration
public class SecurityConfiguration {

    @Bean
    public SecurityFilterChain filterChain(HttpSecurityProvider httpProvider) throws Exception {
        HttpSecurity http = httpProvider.create(true);
        http
            .authorizeHttpRequests((authz) -> authz
                .anyRequest().authenticated()
            )
            .httpBasic(withDefaults());
        return http.build();
    }

}

The approach is only a potential idea. It doesn't have to look like that. However, I do think that it might make sense to expose a functionality that was possible to be used like before.

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 by comparing WebSecurityConfigurerAdapter's disableDefaults behavior with the HttpSecurity and SecurityFilterChain entry points described in the issue. Determine whether a supported way to disable defaults should be exposed and define the expected configuration behavior before proposing an API.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
security
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.