Spring安全性 - 如何提及基于表单和基本身份验证

nov*_*ice 13 spring-security

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

Gui*_*ido 10

通过@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>
Run Code Online (Sandbox Code Playgroud)

<sec:http auto-config="true">
    ... your intercept-url here

    <sec:custom-filter before="SECURITY_CONTEXT_FILTER" ref="basicProcessingFilter" />

    <sec:form-login ... />
    ....
</sec:http>
Run Code Online (Sandbox Code Playgroud)

我不知道之前放置过滤器是否SECURITY_CONTEXT_FILTER是最好的选择.


小智 6

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

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

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

BasicProcessingFilter spring提供处理Basic身份验证是一种被动过滤器,这意味着如果缺少凭据,它将继续沿着过滤器链继续,直到它找到适当的过滤器来处理请求.

现在通过手动定义BasicProcessingFilter bean,我们可以设置它在过滤器链中出现的顺序.下面是您需要在security xml中提供的其他xml声明的示例(Spring Security <3.x)

<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>
Run Code Online (Sandbox Code Playgroud)

另请注意,如果未找到authenticationManager引用,则可以为命名空间添加别名,如下所示.

<security:authentication-manager alias="authenticationManager"/>
Run Code Online (Sandbox Code Playgroud)

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

此方法的问题在于,如果正确输入凭据,则响应是来自表单登录过滤器的登录页面.

但是,看起来这个问题将由春季安全版3.1中的弹簧修复,并且将不再需要这种工作.


nov*_*ice 0

似乎不可能使用命名空间配置来声明表单和基本身份验证。

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