弹簧安全 - 定制过滤器定位

时间:2013-07-23 13:19:40

标签: spring spring-security

我需要以这种方式自定义我的身份验证过程:

  1. 客户端使用“特殊”URL参数
  2. 发送请求(REST API)
  3. 服务器调用传递参数并接收用户名的第三方服务
  4. 服务器按名称查找数据库,这是经过身份验证的主体。
  5. 我将服务器端(2 + 3)拆分为两部分 - (2)的自定义过滤器,获取用户名 - 以及(3)的自定义userdetailservice,通过在数据库中查找名称来构建主体

    但我无法正确构建我的security.xml - 每次它似乎根本不处理过滤器。我认为问题出在第一个(http)节点,但我无法理解我应该为过滤器设置什么位置。这是我的配置:

    <http use-expressions="true" auto-config="true" authentication-manager-ref="authenticationManager">
        <intercept-url pattern="/*" access="isAuthenticated" />
        <custom-filter ref="casServiceTicketFilter" position="FIRST"/>
    </http>
    
    <authentication-manager alias="authenticationManager">
        <authentication-provider user-service-ref="wliAuthenticationService"/>
    </authentication-manager>
    
    <b:bean id="casServiceTicketFilter" class="org.WLICASAuthenticationFilter">
        <b:property name="casTicketValidateURL" value="${cas.ticket.validate.url}"/>
        <b:property name="authenticationManager" ref="authenticationManager"/>
    </b:bean>
    
    <b:bean id="wliAuthenticationService" class="org.WLIUserDetailService"/>
    

    PS-请不要告诉我Spring有开箱即用的CAS支持。它有点各种配置,所以我需要创建自己的服务票证验证器实现

1 个答案:

答案 0 :(得分:2)

您的自定义身份验证过滤器不应位于过滤器链中的第一位。它需要在SecurityContextPersistenceFilter之后。使用

<custom-filter ref="casServiceTicketFilter" after="SECURITY_CONTEXT_FILTER"/>

代替。

如果启用调试日志记录,您应该能够清楚地看到为每个请求调用过滤器的顺序以及是否调用了这些过滤器。

相关问题