如何在Spring Security 3中添加多个自定义过滤器?

2022-09-03 16:26:14

我需要为FORM_LOGIN_FILTER添加两个自定义过滤器,例如

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

我期望的过滤器序列是:
1.预定义的FORM_LOGIN_FILTER
2。myUsernamePasswordAuthenticationFilter
3.myUsernamePasswordAuthenticationFilter2

但以上会导致配置错误。那么,有人知道如何编写正确的配置吗?谢谢!


答案 1

使用 Spring's 包装您的自定义筛选器列表,然后将该筛选器放在 上的相关位置。CompositeFilterSecurityFilterChain

例如,像这样:

<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" />

答案 2

请执行下列操作:

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

这应该把它们放在你想要的地方。


推荐