2015-10-10 73 views
1

我想用單個Auth服務器來訪問多個客戶端的多個資源服務器。Spring Security中的多資源服務器配置OAuth

我試圖從相同的auth服務器訪問兩個資源服務器,並且我的資源服務器配置如下所示。

@Bean 
@Scope("prototype") 
protected ResourceServerConfiguration resource1() { 

    ResourceServerConfiguration resource = new ResourceServerConfiguration(); 
    resource.setConfigurers(Arrays.<ResourceServerConfigurer> asList(new ResourceServerConfigurerAdapter() { 
    @Override 
    public void configure(ResourceServerSecurityConfigurer resources) throws Exception { 
     resources.resourceId(RESOURCE_ID1).tokenStore(tokenStore); 
    } 

    @Override 
    public void configure(HttpSecurity http) throws Exception { 
     http 
     .csrf().disable() 
     .requestMatchers().antMatchers("/greeting") 
     .and() 
     .authorizeRequests() 
     .antMatchers("/users").hasRole("ADMIN");     
    } 
} 
resource.setOrder(4); 
    return resource; 
} 

@Bean 
@Scope("prototype") 
protected ResourceServerConfiguration resource2() { 
    ResourceServerConfiguration resource = new ResourceServerConfiguration(); 
    resource.setConfigurers(Arrays.<ResourceServerConfigurer> asList(new ResourceServerConfigurerAdapter() { 
     @Override 
     public void configure(ResourceServerSecurityConfigurer resources) throws Exception { 
      resources.resourceId(RESOURCE_ID2).tokenStore(tokenStore); 
     } 

     @Override 
     public void configure(HttpSecurity http) throws Exception { 
      http 
      .csrf().disable() 
      .requestMatchers().antMatchers("/welcome") 
      .and() 
      .authorizeRequests() 
      .antMatchers("/users").hasRole("ADMIN"); 
     } 
    } 
    resource.setOrder(5); 
    return resource; 
} 

由於WebSecurityConfigurerAdapter的默認順序爲3,因此我已將資源服務器順序分別配置爲4和5。

但配置豆類越來越無效,我可以訪問的資源擁有5階,如果我嘗試訪問資源「/問候語」,我得到以下錯誤「/歡迎」,

{ "timestamp": 1444400211270, "status": 403, "error": "Forbidden", "message": "Expected CSRF token not found. Has your session expired?", "path": "/greeting"} 

如果我互換資源之間的順序,我可以訪問具有最高價值5

注意資源:我有兩個客戶,使人們可以訪問資源1,另一個可以訪問資源2。

請指教我失蹤的事情。

回答

0

從ResourceServerConfigurer的Javadoc中:

應用程序可以提供此接口的多個實例,並在 一般的(如與其他安全configurers),如果超過一個 配置相同的屬性,那麼最後一個勝利。在應用之前,配置器 按{連接順序}排序。

所以也許在兩個配置中的/歡迎路徑上放一個permitAll()。

相關問題