【问题标题】:Spring security - how to mention both form based and basic authenticationSpring security - 如何提及基于表单的身份验证和基本身份验证
【发布时间】:2010-11-20 19:12:27
【问题描述】:

是否可以在 Spring 安全性中使用命名空间配置同时提及基于表单的身份验证和基本身份验证而不覆盖其他身份验证?这样应用程序就可以同时为基于浏览器的请求和远程客户端提供服务。

【问题讨论】:

    标签: spring-security


    【解决方案1】:

    response by @grimesjm 是对的。但是,如果您使用的是 Spring 3.x,则必须将类名调整为:

    <bean id="basicProcessingFilter" class="org.springframework.security.web.authentication.www.BasicAuthenticationFilter">
        <property name="authenticationManager">
            <ref bean="authenticationManager" />
        </property> 
        <property name="authenticationEntryPoint">
            <ref bean="authenticationEntryPoint" />
        </property>
    </bean>
    
    <bean id="authenticationEntryPoint"
        class="org.springframework.security.web.authentication.www.BasicAuthenticationEntryPoint">
        <property name="realmName" value="Your realm here" />
    </bean>
    

    还有

    <sec:http auto-config="true">
        ... your intercept-url here
    
        <sec:custom-filter before="SECURITY_CONTEXT_FILTER" ref="basicProcessingFilter" />
    
        <sec:form-login ... />
        ....
    </sec:http>
    

    我不知道将过滤器放在SECURITY_CONTEXT_FILTER 之前是否是最佳选择。

    【讨论】:

      【解决方案2】:

      您想要的最终结果是可能的,我遇到了完全相同的问题,这是我的解决方案。

      在命名空间中定义表单登录时,它会自动覆盖您通过命名空间应用的任何其他身份验证过滤器。这是通过过滤器链的排序来完成的,查看 spring security 中的 FilterChainOrder.java 以了解该顺序是如何实际应用于每个过滤器的。

      要解决这个问题,请从命名空间中删除 http-basic 标记,然后手动定义 bean 以处理基本身份验证并将其顺序放置在 AuthenticationProcessingFilter 之前,因为这是处理表单登录的 spring 安全过滤器。

      用于处理基本身份验证的 BasicProcessingFilter spring 是一个被动过滤器,这意味着如果缺少凭据,它将继续沿着过滤器链向下,直到找到合适的过滤器来处理请求。

      现在通过手动定义 BasicProcessingFilter bean,我们可以设置它在过滤器链中出现的顺序。 下面是您需要在安全 xml (Spring Security

      <bean id="basicProcessingFilter" class="org.springframework.security.ui.basicauth.BasicProcessingFilter">
          <property name="authenticationManager"><ref bean="authenticationManager"/></property>
           <security:custom-filter before="AUTHENTICATION_PROCESSING_FILTER"/>
          <property name="authenticationEntryPoint"><ref bean="authenticationEntryPoint"/></property>
      </bean>
      
      <bean id="authenticationEntryPoint"
          class="org.springframework.security.ui.basicauth.BasicProcessingFilterEntryPoint">
                    <property name="realmName" value="My Realm Here"/>
      </bean>
      

      另外请注意,如果您的 authenticationManager 引用未找到,您可以为您的命名空间添加一个别名,如下所示。

      <security:authentication-manager alias="authenticationManager"/>
      

      最终结果是基本过滤器将作为被动过滤器应用,如果缺少所需的凭据,它将继续沿着过滤器链向下,然后表单登录过滤器将处理它。

      这种方法的问题在于,如果正确输入了凭据,则返回的响应是来自表单登录过滤器的登录页面。

      但是,这个问题似乎将在 spring security 3.1 版中由 spring 修复,并且不再需要这种解决方法。

      【讨论】:

        【解决方案3】:

        现在可以使用 Spring Security 3.1.0

        【讨论】:

        • 这是正确的,这里有一个链接可以扩展这个答案:stackoverflow.com/questions/3671861/…
        • 我认为目标是让 REST API 支持同一领域内的两种身份验证。定义两个领域就像定义两场战争——它有效,但并不理想。
        【解决方案4】:

        似乎无法使用命名空间配置同时声明表单和基本身份验证。

        spring社区的参考链接: http://forum.springsource.org/showthread.php?t=72724&highlight=form+basic+authentication

        【讨论】:

        • 他们增加了在 Spring Security 版本 3.1.0.M1 中添加多个
        猜你喜欢
        • 2015-02-06
        • 2011-02-11
        • 2016-03-20
        • 1970-01-01
        • 2013-01-01
        • 2017-07-08
        • 2012-08-15
        • 2013-01-11
        • 2015-07-22
        相关资源
        最近更新 更多