【问题标题】:How to use more than one custom filter invoked after each other?如何使用多个自定义过滤器依次调用?
【发布时间】:2011-04-25 07:30:49
【问题描述】:

大家好,我正在使用 spring security 3.0.2 我有一个自定义过滤器,它的顺序是最后一个 我想在那个过滤器之后添加另一个过滤器,下面的配置对吗?

<custom-filter position="LAST" ref="filter1"/>
<custom-filter after="LAST" ref="filter2"/>

【问题讨论】:

    标签: spring spring-mvc spring-security


    【解决方案1】:

    查看我自己的代码后,我注意到我没有使用 'ref' 属性,而是将此标记放在我的 bean 定义中,如下所示:

    <bean id="ntlmFilter" class="org.springframework.security.ntlm.samples.failover.NtlmProcessingFilter">
        <sec:custom-filter position="NTLM_FILTER" />
        <property name="authenticationManager" ref="authenticationManager" />
        <property name="retryOnAuthFailure" value="false" />
        <property name="securityConfiguration" ref="securityConfiguration" />
    </bean>
    

    来源:http://github.com/aloiscochard/spring-security-ntlm-samples/blob/master/spring-security-ntlm-samples-failover/src/main/resources/applicationContext-security.xml

    即使是针对 spring-security 2,在版本 3 中的行为也是一样的。

    您可以在 org.springframework.security.config.http.SecurityFilters 枚举中找到所有可能的位置:

    http://grepcode.com/file/repo1.maven.org/maven2/org.springframework.security/spring-security-config/3.0.2.RELEASE/org/springframework/security/config/http/SecurityFilters.java

    您可以使用此枚举中已定义的一些位置来定义必须设置自定义过滤器的顺序。

    例如:

    • LAST 之前一个过滤器,LAST 一个过滤器(但 LAST 之后不行!LAST 之后不能有任何东西!)
    • 或在 SWITCH_USER_FILTER 之前和之后的一个过滤器。

    不知道您将标签放在哪里?但我喜欢将它们直接放在过滤器 bean 中......更易于维护:-)

    希望对你有帮助!

    PS:由于位置是基于整数的,你也许可以用数字代替枚举值(警告我们正确的位置数字,看看 SecurityFilters 枚举内部的逻辑),不确定是否被接受......

    【讨论】:

    • 我照你说的做了,我得到了以下错误 - cvc-attribute.3:元素'custom-filter' 上的属性'after' 的值'filter1' 对其无效类型,“命名安全过滤器”。
    • 更多描述:- cvc-enumeration-valid:值 'filter1' 对于枚举 '[FIRST, CHANNEL_FILTER,.... 不是 facet-valid
    • 哎呀,我要查看我的代码,似乎我的内存已损坏
    • 对不起,我错了。我用正确的代码编辑了我的回复!
    猜你喜欢
    • 2015-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多