14

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

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

这是我的弹簧安全 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)

如何让 Web 服务接受 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
4

3 回答 3

9

这是用于 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();
    }
...
}
于 2014-11-12T01:00:31.967 回答
9

我需要删除<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>
于 2013-04-25T17:48:29.327 回答
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获取完整的方法集。

于 2013-04-25T18:05:21.570 回答