gpt4 book ai didi

spring - 通过没有表单登录的 Spring Security 休息基本身份验证

转载 作者:IT老高 更新时间:2023-10-28 13:56:46 25 4
gpt4 key购买 nike

我正在尝试创建一个可供其他 Web 服务使用的 Restful Web 服务。理想情况下,当客户端访问服务并且未经过身份验证时,他们应该得到 401。我希望用户能够通过向请求添加身份验证 header 来进行身份验证。我不希望用户填写登录表单并发布。我也不希望它在 cookie 中存储任何登录凭据(即保持状态),它应该都在随每个请求发送的 auth header 中。我已经使用 spring roo 创建了 web 服务。

我目前所拥有的(取自 spring security 3.1 教程之一),当用户收到 401 时,会提示他们登录页面,然后发布页面,获取他们随每个请求发送的 cookie .

这是我的 Spring Security xml。

 <http use-expressions="true">
<intercept-url pattern="/customers/**" access="isAuthenticated()" />
<intercept-url pattern="/**" access="denyAll" />
<form-login />
</http>
<authentication-manager>
<authentication-provider>
<user-service>
<user name="alice" password="other" authorities="user" />
<user name="custome1" password="other" authorities="user" />
</user-service>
</authentication-provider>
</authentication-manager>

当我从 curl 发送请求时,我得到以下信息:

 $ curl -i -H "Accept: application/json" -H "Authorization: Basic Y3VzdG9tZXIxOm90aGVy" http://localhost:8080/Secured/customers

HTTP/1.1 302 Found
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=B4F983464F68199FA0160DBE6279F440; Path=/Secured/; HttpOnly
Location: http://localhost:8080/Secured/spring_security_login;jsessionid=B4F983464F68199FA0160DBE6279F440
Content-Length: 0
Date: Thu, 25 Apr 2013 17:18:48 GMT

我的基本 header 标记是 base64(customer1:other)

如何让 Web 服务接受 auth header ,而不是将我重定向到登录页面?

当我从 security.xml 中删除时,我得到以下信息:

excpetion:org.springframework.beans.factory.parsing.BeanDefinitionParsingException:
Configuration problem: No AuthenticationEntryPoint could be established.
Please make sure you have a login mechanism configured through the namespace
(such as form-login) or specify a custom AuthenticationEntryPoint with the
'entry-point-ref' attribute

最佳答案

我需要删除 <form-login>并添加 <http-basic> .还在我的配置中添加了 create-session-"stateless"。

 <http use-expressions="true" create-session="stateless" >
<intercept-url pattern="/customers/**" access="isAuthenticated()" />
<intercept-url pattern="/**" access="denyAll" />
<http-basic/>
</http>

关于spring - 通过没有表单登录的 Spring Security 休息基本身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16220971/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com