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

标签 spring spring-security basic-authentication

我正在尝试创建一个可供其他 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/

相关文章:

spring boot ant匹配器参数

java - Spring安全库之间的差异

java - InjectedMock 中的 NullPointerException

java - 作业调度程序未调用

java - 使用 Struts2 和 hibernate 创建 Web 服务(SOAP 或 REST)

java - 当超过此主体的最大 session 数时,Spring 安全重定向

java - 关于 Spring web.xml <context-param> 和 <listener> 标签的一些信息(引用一个 Hello World 示例)

c# - 使用 HttpClient 时出现未经授权的错误

c# - 使用 gRPC 进行基于自定义 channel 的身份验证

java - HTTPClient 在使用 Basic Auth 时发出两个请求?