通过spring security进行Rest基本身份验证,无需表单登录
我正在尝试创建一个restful web服务,它将被其他web服务使用。理想情况下,当客户机访问服务且未经过身份验证时,他们应该获得401。我希望用户能够通过向请求添加身份验证头来进行身份验证。我不希望用户填写登录表单,然后发布。我也不希望它在cookie中存储任何登录凭据(即保持状态),它应该在每个请求发送的auth头中。我已经使用SpringRoo创建了web服务 我目前所做的(摘自SpringSecurity3.1教程之一),当用户获得401时,会向他们提示一个登录页面,然后发布页面,获得他们随每个请求发送的cookie 这是我的spring安全xml通过spring security进行Rest基本身份验证,无需表单登录,spring,spring-security,basic-authentication,Spring,Spring Security,Basic Authentication,我正在尝试创建一个restful web服务,它将被其他web服务使用。理想情况下,当客户机访问服务且未经过身份验证时,他们应该获得401。我希望用户能够通过向请求添加身份验证头来进行身份验证。我不希望用户填写登录表单,然后发布。我也不希望它在cookie中存储任何登录凭据(即保持状态),它应该在每个请求发送的auth头中。我已经使用SpringRoo创建了web服务 我目前所做的(摘自SpringSecurity3.1教程之一),当用户获得401时,会向他们提示一个登录页面,然后发布页面,获得
<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>
其中,我的基本头令牌是base64(customer1:other)
如何让web服务接受auth头,而不将我重定向到登录页面
从security.xml中删除时,会得到以下结果:
$ 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
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
我需要删除
并添加
。还将创建会话-“无状态”添加到我的配置中
<http use-expressions="true" create-session="stateless" >
<intercept-url pattern="/customers/**" access="isAuthenticated()" />
<intercept-url pattern="/**" access="denyAll" />
<http-basic/>
</http>
使用spring security进行jax rs的测试演示 这是一种简单的身份验证方法,无需升级凭据 您可以执行
RequestHandler
并重写
public Response handleRequest(Message message, ClassResourceInfo resourceClass);
下面是一个用于Rest身份验证的非xml配置示例,或者使用表单或基本配置,无论需要什么:
.and().httpBasic();
这就是魔法!)
@Configuration
@EnableWebMvcSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/someurl").hasRole("ADMIN")
.antMatchers("/anotherurl").hasRole("USER")
.antMatchers("/", "main").permitAll().anyRequest().authenticated()
.and()
.httpBasic();
http.formLogin()
.loginPage("/login").permitAll()
.and()
.logout().permitAll();
}
...
}