Skip to content

NPE in HttpSecurity#addFilterBefore when mixing custom DSL and standard #12637

Closed
@lrozenblyum

Description

@lrozenblyum

Describe the bug
We get an NPE when we try to add a new filter using standard DSL relative to another filter that was registered via custom DSL.
This looks like a different scenario than #9787

To Reproduce
Spring Security version: 5.8.1

// here we have to use a custom DSL because getting AuthenticationManager is really painful now
private class CustomConfigurer extends AbstractHttpConfigurer<CustomConfigurer, HttpSecurity> {
        @Override
        public void configure(HttpSecurity http) throws Exception {
	        AuthenticationManager authenticationManager = http.getSharedObject(AuthenticationManager.class);
        
	        http.addFilterBefore(new CustomFilter(authenticationManager), LogoutFilter.class);
        }
}

@Bean SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http.apply(new CustomConfigurer());
        httpSecurity.addFilterBefore(new Filter2(), CustomFilter.class); // NPE here inside addFilterBefore
        return http.build();
}

Expected behavior

  1. I would expect it's possible to mix registration of filters via standard and custom DSL.
  2. In any case org.springframework.security.config.annotation.web.builders.HttpSecurity.addFilterAtOffsetOf(Filter, int, Class<? extends Filter>) NPE seem not to be graceful - compare with org.springframework.security.config.annotation.web.builders.HttpSecurity.addFilter(Filter) which at least validates and throws some more meaningful exception

A workaround exists: the second filter must be also applied via a custom DSL.

Metadata

Metadata

Assignees

Labels

in: configAn issue in spring-security-configtype: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions