【问题标题】:spring security - custom filter positioningspring security - 自定义过滤器定位
【发布时间】:2013-07-22 14:14:31
【问题描述】:

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

  1. 客户端使用“特殊”URL 参数发送请求(REST API)
  2. 服务器调用第三方服务传递参数并接收用户名
  3. 服务器按名称查找数据库,这是经过身份验证的主体。

我将我的服务器端 (2+3) 分成两部分 - (2) 的自定义过滤器,它获取用户名 - 和一个自定义的 userdetailservice for(3),它通过在数据库中查找名称来构建主体。

但我无法正确构建我的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 支持。配置有点多样化,所以我需要创建自己的服务票证验证器实现

【问题讨论】:

    标签: spring spring-security


    【解决方案1】:

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

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

    改为。

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

    【讨论】:

    • 嗨,卢克。 CAS_FILTER 的位置呢?还是没有区别?
    • 这也可以,但如果这是您拥有的唯一身份验证过滤器,则无关紧要。
    猜你喜欢
    • 2011-08-23
    • 2014-07-28
    • 2014-10-24
    • 1970-01-01
    • 2017-02-02
    • 2011-07-23
    • 2018-05-25
    • 2016-03-08
    • 1970-01-01
    相关资源
    最近更新 更多