Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add multiple custom-filter in Spring Security 3?

I need add two custom filters for FORM_LOGIN_FILTER, e.g.

<custom-filter after="FORM_LOGIN_FILTER" ref="myUsernamePasswordAuthenticationFilter" />
<custom-filter after="FORM_LOGIN_FILTER" ref="myUsernamePasswordAuthenticationFilter2" />

What I expect the filter sequences is:
1. Predefind FORM_LOGIN_FILTER
2. myUsernamePasswordAuthenticationFilter
3. myUsernamePasswordAuthenticationFilter2

But above will cause configuration error. So, anyone knows how to write the right config? Thanks!

like image 452
Paganini Avatar asked Aug 26 '10 15:08

Paganini


People also ask

How do I apply multiple filters in Spring boot?

If you add multiple filters in a sequence, they will all run one at a time. The order in which the configured filters are run is important. The annotation @Order specifies the order in which the multiple filters are executed. The request url pattern is another way to configure multiple filters.

Is WebSecurityConfigurerAdapter deprecated?

The type WebSecurityConfigurerAdapter is deprecatedWell, it's because the developers of Spring framework encourage users to move towards a component-based security configuration.

What is FilterChain in Spring?

Spring Security maintains a filter chain internally where each of the filters has a particular responsibility and filters are added or removed from the configuration depending on which services are required. The ordering of the filters is important as there are dependencies between them.


1 Answers

Use Spring's CompositeFilter to wrap your custom filters list, and then put that filter on relevant position on SecurityFilterChain.

E.g. like this:

<bean id="customFilters" class="org.springframework.web.filter.CompositeFilter">
    <property name="filters">
        <list>
            <ref bean="myUsernamePasswordAuthenticationFilter"/>
            <ref bean="myUsernamePasswordAuthenticationFilter2"/>
        </list>
    </property>
</bean>
...
<custom-filter after="FORM_LOGIN_FILTER" ref="customFilters" />
like image 131
maraswrona Avatar answered Sep 28 '22 18:09

maraswrona