弹簧安全定制过滤器位置

时间:2014-04-16 03:41:09

标签: java spring filter spring-security

如何在链顶部的Spring Security链中插入多个自定义过滤器?

我可以使用position="FIRST"after="FIRST"插入一个但是如何在链顶部添加两个或三个?如果我尝试多个after="FIRST"我会遇到冲突错误,我无法将多个过滤器放在同一位置。

    <custom-filter ref="customExceptionJSONFilter" position="FIRST"/>
    <custom-filter ref="logHeadersFilter" after="FIRST"/>
    <custom-filter ref="thirdCustomFilter" after="FIRST"/>       <---- this causes a conflict

有没有基于另一个过滤器的定位方法?或者某种说法&#34; FIRST + 1&#34;,&#34; FIRST + 2&#34;等?

1 个答案:

答案 0 :(得分:1)

最好的方法是将要插入的过滤器包装到单个Filter中。您可以从Spring Security中的VirtualFilterChain复制代码来实现它:https://github.com/spring-projects/spring-security/blob/6be4e3a9fc99d676f367a5e9eed3ea61fbba122c/web/src/main/java/org/springframework/security/web/FilterChainProxy.java#L306