Anoop M Nair
Anoop M Nair

Reputation: 1087

Disabling custom filter based on cofiguration

I have 5 custom filters and I registered them using FilterRegistrationBean of spring.see the code below.

@Bean
    public FilterRegistrationBean myFilter() {

        FilterRegistrationBean registration = new FilterRegistrationBean();
        Filter myFilter = new CustomPermissionfilter();
        beanFactory.autowireBean(myFilter);
        registration.setFilter(myFilter);
        registration.setOrder(2);
        return registration;
    }

like this I registered all my filters.

Now my requirement is based on a configuration I should disable some of the filters that I registered earlier.

config file

custom.filters=CustomPermissionfilter,permissionFilter,IPvalidationFilter

What required is I should disable all other custom filters than specified above

I tried BeanFactoryPostProcessor filterDisablingPostProcessor() but this approach failed because this only load default filter registerd in context. Please help

Upvotes: 2

Views: 525

Answers (1)

Bohdan Levchenko
Bohdan Levchenko

Reputation: 3561

I believe the easiest solution is to make your filter bean definitions Conditional. The most logical Conditional* is @ConditionalOnProperty but since you have everything in one string it won't work. So you can use @ConditionalOnExpression instead.

@Bean
@ConditionalOnExpression("#{environment.getProperty('custom.filters').contains('CustomPermissionfilter')}")
public FilterRegistrationBean myFilter() {
    FilterRegistrationBean registration = new FilterRegistrationBean();
    Filter                 myFilter     = new CustomPermissionfilter();
    beanFactory.autowireBean(myFilter);
    registration.setFilter(myFilter);
    registration.setOrder(2);
    return registration;
}

Upvotes: 4

Related Questions