通过Spring安全性进行基本身份验证,无需登录表单

时间:2013-04-25 17:22:14

标签: spring spring-security basic-authentication

我正在尝试创建一个将由其他Web服务使用的restful Web服务。理想情况下,当客户端访问服务并且未经过身份验证时,他们应该获得401.我希望用户能够通过向请求添加身份验证标头进行身份验证。我不希望用户填写登录表单,然后发布。我也不希望它在cookie中存储任何登录凭证(即保持状态)它应该在每个请求的auth头发送中。我使用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

我的基本标头令牌是base64(customer1:other)

如何让网络服务接受auth标头,而不是将我重定向到登录页面?

当我从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

3 个答案:

答案 0 :(得分:8)

我需要移除<form-login>并添加<http-basic>。还在我的配置中添加了create-session-“无状态”。

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

答案 1 :(得分:8)

以下是Rest身份验证的非xml配置示例,或者使用表单或基本所需的非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();
    }
...
}

答案 2 :(得分:1)

按照弹簧安全性的jax-rs测试演示 http://svn.apache.org/repos/asf/cxf/trunk/distribution/src/main/release/samples/jax_rs/spring_security/

这是一种简单的身份验证方法,无需提升凭据。

您可以选择实施RequestHandler并覆盖

public Response handleRequest(Message message, ClassResourceInfo resourceClass);

关注:http://cxf.apache.org/docs/secure-jax-rs-services.html以获取完整的方法。