【问题标题】:Multiple Resource server configuration in Spring security OAuthSpring security OAuth中的多个资源服务器配置
【发布时间】:2016-01-08 03:31:51
【问题描述】:

我正在尝试使用单个身份验证服务器从多个客户端访问多个资源服务器。

我正在尝试从同一个身份验证服务器访问两个资源服务器,我的资源服务器配置如下。

@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。

请指教我缺少的东西。

【问题讨论】:

    标签: oauth spring-security spring-boot


    【解决方案1】:

    来自ResourceServerConfigurer的Javadoc:

    应用程序可以提供此接口的多个实例,并且在 一般(与其他安全配置器一样),如果有多个 配置相同的属性,则最后一个获胜。配置者 在应用之前按Order排序。

    因此,可能在两种配置中的/welcome 路径上都放置一个permitAll()

    【讨论】:

      猜你喜欢
      • 2017-02-13
      • 2017-03-25
      • 2020-03-23
      • 2020-11-13
      • 2017-03-29
      • 2015-02-19
      • 1970-01-01
      • 2013-12-21
      • 2019-04-29
      相关资源
      最近更新 更多