Spring security:表单登录前的基本身份验证

时间:2015-03-26 13:25:18

标签: java spring spring-security

我想在显示网站之前添加(临时演示)基本身份验证。因此,用户可以输入一些凭据(如demo/demo)来打开网站,然后使用正常的form-login身份验证。 我尝试在正常表单登录之前添加此http

<http pattern="/**" use-expressions="true" authentication-manager-ref="basicAuthManager">
   <intercept-url pattern="/**" access="hasRole('ROLE_USER')" />  
   <http-basic />
</http>

<http auto-config="true" use-expressions="true">
...
<form-login .../>
</http>

但我有例外:

A universal match pattern ('/**') is defined before other patterns in the filter chain,  
causing them to be ignored. Please check the ordering in your <security:http>
namespace or FilterChainProxy bean configuration

似乎我不能两次使用pattern="/**"? 有可能做我想做的事吗?
在成功进行基本身份验证后,我也不需要在安全上下文中设置主体。

2 个答案:

答案 0 :(得分:0)

Spring可能会接受多种身份验证方案。但据我所知,不可能要求双重身份验证(例如,同时使用BASIC和FORM登录)。所以不可能做你想做的事。

最简单的解决方案是在servlet容器(例如Tomcat)中或在代理HTTP服务器(例如Apache或Nginx)中的servlet容器前定义BASIC身份验证。

在servlet容器前面安装HTTP服务器是一个好习惯,所以我会选择第二个选项。


如果您仍然想在应用程序中攻击BASIC身份验证,则可以轻松编写自己的身份验证servlet过滤器。这应该非常简单。

答案 1 :(得分:0)

我认为你有两个问题:

  1. 您有两个http代码。我不是百分百肯定,但我怀疑这是不对的
  2. 您不需要http标记中的模式。删除后再试一试,只留下pattern级别的intercept-url属性。此外,由于您不会操纵Principal,因此您需要使用isAuthenticated()而不是hasRole(...)。像这样:

    <http auto-config="true" use-expressions="true" authentication-manager-ref="basicAuthManager">
       <intercept-url pattern="/**" access="isAuthenticated()" />  
       <http-basic />
    </http>