2015-12-09 92 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.