2015-12-09 91 views
0

我对Spring很感兴趣,并且试图使用spring-security-oauth2构建OAuth服务器。spring-security-oauth2中的HttpSecurity配置问题

我主要引用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 

我加入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(); 
    } 
} 

OK一些HttpSecurity配置,它工作正常。但是,我想在我的Resource Server中保护greeting 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

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

回答

0

因为anyRequest().access("#oauth2.hasScope('scope1')");在你ResourceServerConfiguration每个请求都需要与OAuth的范围进行验证,包括//login(因为OAuth安全配置是在春季安全链的正常配置后)。如果你想保护/greeting使用OAuth,你应该像这样在你的ResourceServerConfiguration

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

我改变了你说的代码。 '/ greeting'现在受到保护,但我在'SecurityConfig.java'中的配置不起作用。我可以在不登录的情况下访问'/ hello'。 –

+0

您在添加OAuth配置之前无法做到这一点? –

+0

我不能,当我尝试访问没有登录的'/ hello'时,它应该重定向到'/ login',就像我在'SecurityConfig.java'中配置的一样。 –

0

我之前经历了同样的问题。

为了解决这个问题,我换成requestMatchers().antMatchers(...).and()antMatcher(...)

请尝试以下配置:

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

哦,它确实有效!所以如果我想保护很多资源,我只需要在每个'.antMatcher()'之间使用'.and()'? –

2

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

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

@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')"); 

} 
0

我遇到了同样的问题,接受的答案确实解决了这个问题。但是,目前的解决方案应该使用spring-security-oauth2版本2.0.9.RELEASE。这个版本包含a fix,它们都可以在Spring Security 3.2和4中使用。0.