Skip to content

Add the ability to swallow exceptions for specific dispatcher types #11623

@marcusdacoregio

Description

@marcusdacoregio

The FilterSecurityInterceptor and AuthorizationFilter now apply to every request by default.

This led to a problem from the Spring Boot's perspective:

Consider the following configuration:

@Bean
SecurityFilterChain appSecurity(HttpSecurity http) {
    http.authorizeHttpRequests(requests -> requests
        .antMatchers("/public/**").permitAll()
        .anyRequest().authenticated()
    );
    return http.build();
}

If a request is made to GET /public/notfound with no credentials, then we expect a 404 - Not Found. Instead, Spring Boot will handle the exception and forward the request to /error with DispatcherType.ERROR. The /error endpoint is protected, an AuthenticationException is thrown and ExceptionTranslationFilter transforms it to a 401 - Unauthorized.

We should consider adding an option to ExceptionTranslationFilter that configures it to swallow the Spring Security exceptions from specified DispatcherTypes. Something like:

@Bean
SecurityFilterChain appSecurity(HttpSecurity http) {
    ...
    http.exceptionHandling(exception -> exception
        .swallowExceptionsForDispatcherTypes(List.of(DispatcherType.ERROR))
    );
    return http.build();
}

This way we keep the original response status code but apply all the authorization rules to that endpoint.

See:

Metadata

Metadata

Labels

status: declinedA suggestion or change that we don't feel we should currently apply

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions