代码之家  ›  专栏  ›  技术社区  ›  Kon

使用springoauth支持多个资源id

  •  0
  • Kon  · 技术社区  · 5 年前

    情况。 我们使用springoauth来验证安全令牌(JWT)。令牌有一个 aud 声明一个特定的资源ID。下面的代码正确验证用包含客户端ID的aud声明签名的任何JWT令牌 resourceId-123 :

    class ResourceServerConfig {
    
      @Bean
      protected ResourceServerConfiguration adminResources2() {
        ResourceServerConfiguration resource = new ResourceServerConfiguration() {
          void setConfigurers(List<ResourceServerConfigurer> configurers) {
            super.setConfigurers(configurers)
          }
        }
        resource.setConfigurers(Collections.<ResourceServerConfigurer>singletonList(new ResourceServerConfigurerAdapter() {
          @Override
          void configure(ResourceServerSecurityConfigurer resources) throws Exception {
            resources.resourceId("resourceId-123")
          }
    
          @Override
          void configure(HttpSecurity http) throws Exception {
            http.antMatcher("/path")
                    .authorizeRequests()
                    .anyRequest().authenticated()
          }
        }))
    
        resource.setOrder(3)
        return resource
      }
    }
    

    问题。 我们如何支持同一路径的多个客户端ID(在上面的示例中, /path )? 我已经看到了如何使用不同的客户机ID配置多个bean的示例 不同的

    0 回复  |  直到 5 年前
        1
  •  1
  •   Raniz    5 年前

    我觉得你的做法不对。

    客户端ID与springsecurity中的访问群体不同,一个客户端可以有一个或多个资源ID,JWT可以包含多个访问群体。

    因此,我认为您应该遵循这样一个例子,即不同路径有不同的资源ID,为共享路径创建一个新的资源ID,并将其添加到允许访问它的所有客户机中。

    例子:

    |----------------+-------------|
    | Path           | Resource ID |
    |----------------+-------------|
    | /client1/info  | client1     |
    | /client2/info  | client2     |
    | /shared/status | all-clients |
    |----------------+-------------|
    

    客户:

    |---------+----------------------|
    | Client  | Resource IDs         |
    |---------+----------------------|
    | client1 | client1, all-clients |
    | client2 | client2, all-clients |
    |---------+----------------------|
    

    如果您能提供一个可运行的示例,我可以对其进行修补,那么展示一些东西会更容易,但是由于您发布的代码数量有限,这是我所能做的最好的。

    另一种方法是使用不同的作用域而不是不同的受众。这将消除对多个资源服务器配置的需要,您可以使用类似于 #oauth2.hasScope('admin') 在里面 .access() 或者在 @PreAuthorize