tkec
tkec

Reputation: 147

Forbid spring boot add a filter with @Bean to filterchain

I create a filter for shiro, use @Bean, but spring boot add it to servlet filterchain.

I used it in shiro filterChainDefinitions:

shiroFilterFactoryBean.getFilters().put("apiValid", getApiValidFilter()); 

@Bean(name = "apiValid") 
public ApiValidFilter getApiValidFilter() { 
    return new ApiValidFilter(); 
} 

I want the filter as a spring bean, to get benefit from spring, like use @Autowired, but do not want it add to filterchain automatic.

How to config spring boot to pervent this behavior?

Upvotes: 0

Views: 434

Answers (1)

Andy Wilkinson
Andy Wilkinson

Reputation: 116111

As described in the documentation, you can create a FilterRegistrationBean for the Filter and mark it as disabled:

@Bean
public FilterRegistrationBean registration(MyFilter filter) {
    FilterRegistrationBean registration = new FilterRegistrationBean(filter);
    registration.setEnabled(false);
    return registration;
}

Upvotes: 1

Related Questions