Skip to content

Prevent Servlet filter re-registration #19605

@lukas-krecan

Description

@lukas-krecan

We are trying to register two servlet filters implemented by the same class on two different paths with different configuration

    @Bean
    fun authFilter1(): FilterRegistrationBean<*> {
        val filter = BasicAuthFilter("username1", "password1")
        val reg = FilterRegistrationBean(filter)
        reg.addUrlPatterns("/path1")
        return reg
    }

    @Bean
    fun authFilter2(): FilterRegistrationBean<*> {
        val filter = BasicAuthFilter("username2", "password2")
        val reg = FilterRegistrationBean(filter)
        reg.addUrlPatterns("/path2")
        return reg
    }

If we register the filters it like this, Spring Boot uses the filter class name to deduce the filter name and since both filters are implemented by the same class, only one filter is effectively registered which is surprising. Basically it's a silent failure.

I would prefer Spring Boot to throw an exception in such case or event better, generate a unique name for each filter.

Of course, once the user is aware of the issue, it can be simply fixed by explicitly specifying the filter name in FilterRegistrationBean.

Metadata

Metadata

Assignees

Labels

status: supersededAn issue that has been superseded by another

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions