Anoop M Nair
Anoop M Nair

Reputation: 1087

Dynamically add a new filter

Is it possible to add a new filter in the filter chain dynamically? I mean by checking a condition/configuration can we include or exclude a specific filter from the filter chain?

Upvotes: 0

Views: 3049

Answers (4)

ganesan dharmalingam
ganesan dharmalingam

Reputation: 1223

I did something like this Using CompositeFilter.

 private Filter ssoFilter() {

        return new GenericFilter() {
            @Override
            public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
                List<Filter> identityProviderFilters = identityProviderFilterProvider.getAllIdentityProviderFilters();
                CompositeFilter filter = new CompositeFilter();
                filter.setFilters(identityProviderFilters);
                filter.doFilter(request, response, chain);
            }
        };
    }

Where the filters are decided based on database entry.

Upvotes: 1

Anoop M Nair
Anoop M Nair

Reputation: 1087

This time spring and @ConditionalOnExpression annotation help me to load a filter bean based on the configuration.

Steps that I followed

Define a bean at my configuration class(class having @Configuration)

Use @ConditionalOnExpression for loading the bean dynamically (Based on a configuration file)

See the below code snippet

  @Bean
        @ConditionalOnExpression("#{environment.getProperty('security.oauth2.custom.filters').contains('XTokenFilter')}")
        public FilterRegistrationBean xxTokenFilter() {
            FilterRegistrationBean registrationBean = new FilterRegistrationBean();
            XTokenFilter xTokenFilter = new XTokenFilter();
            xTokenFilter.setAuthenticationManager(XOuthenticationManager());
            beanFactory.autowireBean(xTokenFilter );


    registrationBean.setFilter(xTokenFilter);
            registrationBean.setOrder(findOrderOfCustomFilters("XTokenFilter"));
            return registrationBean;
        }

This bean will load if and only if the property "security.oauth2.custom.filters" contains value XTokenFilter.

Upvotes: 0

pamykyta
pamykyta

Reputation: 658

I think it is not possible in the regular web.xml file.

You can think about usage of ServletContainerInitializer. It is a way to programmatically declare you web application config.

It means that you can programmatically set servlets, filters and other things that you can define in the regular web.xml.

E.g. in this question is described how to declare servlets programmatically Map servlet programmatically instead of using web.xml or annotations

If you use Spring framework you can also think about using spring's callbacks to do this. Check this article out How to specify url-pattern for Servlet filters in WebApplicationInitializer?

Upvotes: 1

Volodymyr Demenkov
Volodymyr Demenkov

Reputation: 197

I believe simple solution for such requirement is to add filter with condition in doFilter method to filter chain. You will always have this filter in chain, but when condition is not satisfied filter will just pass control to the next filter.

Upvotes: 0

Related Questions