spring-security-oauth2中的HttpSecurity配置问题

时间:2015-12-09 14:30:04

标签: spring spring-security spring-security-oauth2

我对Spring很新,我尝试使用spring-security-oauth2构建OAuth服务器。

我主要参考spring.io给出的示例和教程。

  

https://github.com/spring-projects/spring-security-oauth/tree/master/samples/oauth2   http://spring.io/guides/tutorials/spring-boot-oauth2/

但是,我遇到了HttpSecurity配置的一些问题。

我的文件夹结构如下。

├─java
│  └─com
│      └─example
│              Greeting.java
│              GreetingController.java
│              MvcConfig.java
│              OAuth2ServerConfig.java
│              SecurityConfig.java
│              SocialApplication.java
│
└─resources
    │  application.yml
    │
    └─templates
            hello.html
            home.html
            login.html

我在HttpSecurity

中添加了一些SecurityConfig.java配置
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter{
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
        .authorizeRequests()
            .antMatchers("/", "/home").permitAll()
            .anyRequest().authenticated()
            .and()
        .formLogin()
            .loginPage("/login")
            .permitAll()
            .and()
        .logout()
            .permitAll();
    }
}

好的,它运作正常。但是,我想在我的greeting中保护Resource Server api(这是我刚刚从另一个演示中复制的简单的api api)。 所以我在HttpSecurity

中添加了一些OAuth2ServerConfig.java配置
@Configuration
@EnableResourceServer
protected static class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {

    @Override
    public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
        resources.resourceId(RESOURCE_ID);
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {
        // @formatter:off
        http
                .requestMatchers().antMatchers("/greeting")
                .and()
                .authorizeRequests()
                .anyRequest().access("#oauth2.hasScope('scope1')");
        // @formatter:on
    }

}

似乎我只保护/greeting。但是,完成此操作后,我甚至无法访问//login。它说Full authentication is required to access this resource

我是否错过了一些配置或做错了什么?

4 个答案:

答案 0 :(得分:3)

好吧,我找到了解决方案。 这种情况在issue中提到。 这是Spring-Security 4.0.3中的一个错误。

解决问题的方法是在配置requestMatcher()时使用requestMatchers()而不是Resource Server。 这是一个例子。

@Override
public void configure(HttpSecurity http) throws Exception {
    http
              .requestMatcher(
                  new OrRequestMatcher(
                      new AntPathRequestMatcher("/greeting"),
                      new AntPathRequestMatcher("/greeting2")
                  )
              )
              .authorizeRequests()
              .antMatchers("/greeting").access("#oauth2.hasScope('scope1')")
              .antMatchers("/greeting2").access("#oauth2.hasScope('scope2')");

}

答案 1 :(得分:1)

我之前遇到过同样的问题。

要解决此问题,我将requestMatchers().antMatchers(...).and()替换为antMatcher(...)

请尝试以下配置:

public void configure(HttpSecurity http) throws Exception {
    // @formatter:off
    http
        .antMatcher("/greeting")
        .authorizeRequests()
            .anyRequest().access("#oauth2.hasScope('scope1')");
    // @formatter:on
}

答案 2 :(得分:0)

由于anyRequest().access("#oauth2.hasScope('scope1')");在您的ResourceServerConfiguration中,每个请求都需要通过OAuth范围进行身份验证,包括//login(因为OAuth安全配置位于Spring安全链中的正常配置)。如果您想使用OAuth保护/greeting,您应该在ResourceServerConfiguration:{/ p>

@Override
public void configure(HttpSecurity http) throws Exception {
    // @formatter:off
    http
        .authorizeRequests()
            .antMatchers("/greeting").authenticated();
    // @formatter:on
}

答案 3 :(得分:0)

我遇到了同样的问题,接受的答案确实解决了这个问题。但是,当前的解决方案应该是使用spring-security-oauth2版本2.0.9.RELEASE。此版本包括a fix,它应该在Spring Security 3.2和4.0中都有效。

相关问题