Spring Security HTTP基本身份验证

8

我正在尝试使用Spring Security进行简单的基本身份验证。我已经正确配置了命名空间,并且服务器上没有异常。在我的“servlet.xml”文件中,我为Spring Security设置了以下内容:

<security:http>
    <security:http-basic></security:http-basic>
    <security:intercept-url method="POST" pattern="/**" access="ROLE_USER" />
</security:http>


<security:authentication-manager alias="authenticationManager">
    <security:authentication-provider>
        <security:user-service>
            <security:user name="cucu" password="tas" authorities="ROLE_USER" />
            <security:user name="bob" password="bobspassword" authorities="ROLE_USER" />
        </security:user-service>
    </security:authentication-provider>
</security:authentication-manager>

几乎一切都很完美:那些不是POST方法的方式不会提示任何登录表单,而POST方法则会提示。问题是,无论是还是都无法在那里登录。有人能看出我错在哪里吗?
提前致谢!;-)

你正在使用哪个版本的Spring Security? - Gandalf
抱歉,我忘记了,Spring Security 3。 - raspayu
你能否发布实际错误的堆栈跟踪/输出? - Gandalf
抱歉,我认为我应该更加详细地描述一下。问题是,即使我使用这两个用户进行登录,我仍然会收到401未经授权的错误。感谢您的时间,甘道夫;-) - raspayu
你的问题�能是我�以快速谷歌到的最清晰�最简�的�置HTTP基本认�的示例。点��� - Jonik
多用途问题! :D 干杯,伙计! - raspayu
2个回答

26

自动应答

我头疼了两天,试图解决这个问题的代码......

看来问题不在代码上。我正在使用Weblogic,并且Weblogic会捕获"authorization"标头的请求,因此它不会到达我的身份验证管理器。我尝试使用glassfish,它完美地运行。

搜寻一些信息,我在下一个博客中找到了一个有用的条目: http://yplakosh.blogspot.com/2009/05/how-to-fix-basic-authentication-issue.html

将以下行添加到我的Weblogic服务器的config.xml文件(<security-configuration>部分):

<enforce-valid-basic-auth-credentials>false</enforce-valid-basic-auth-credentials>

Weblogic将不再捕获基本身份验证凭据,因此它将由您的身份验证管理器处理。

希望它可以为任何人节省一些时间 :-)


5
非常感谢您发布这个解决方案。我刚开始遇到这个问题,在谷歌搜索后找到了您的帖子。 - Justin Kredible
呵呵,我很高兴能帮助你节省一些时间;-) - raspayu
截至2019年,似乎问题仍然存在。 - Nice Books

1

尝试:

<http auto-config="true>
   <security:intercept-url method="POST" pattern="/**" access="ROLE_USER" />
   <http-basic />
</http>

谢谢你的建议。我也尝试了将自动配置设置为true,但我认为我还缺少其他东西...但不知道是什么。我认为我的代码非常简单,不应该有问题。 - raspayu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接