我正在尝试使用单个认证服务器从多个客户端访问多个资源服务器。
我试图从同一个认证服务器访问两个资源服务器,我的资源服务器配置如下:
@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。
但是配置的bean正在被覆盖,我可以访问顺序为5的资源"/welcome",如果我尝试访问资源"/greeting",我得到以下错误,
{ "timestamp": 1444400211270, "status": 403, "error": "Forbidden", "message": "Expected CSRF token not found. Has your session expired?", "path": "/greeting"}
如果我交换资源之间的顺序,我可以访问值最高的资源5。
注意:我有两个客户端,一个可以访问RESOURCE1,另一个可以访问RESOURCE2。
请告诉我缺少的东西
From Javadoc of ResourceServerConfigurer
:
应用程序可以提供该接口的多个实例一般(与其他安全配置器一样),如果有多个配置相同的属性,那么最后一个属性获胜。的configurers在应用前用
Order
进行排序。
所以可能在/welcome
路径上放一个permitAll()