1

我使用 App ID 作为身份提供者和授权服务器来保护一些后端 spring-boot 应用程序。我已设法将整个 OAuth 2.0 授权代码流程设置为正常工作,但无法设法将自定义范围包含到访问令牌中。访问令牌中出现的唯一范围是 App ID 默认范围:“openid appid_default appid_readuserattr appid_readprofile appid_writeuserattr appid_authenticated”

我已经使用所需的自定义范围配置了适当的角色,并将此角色与用户配置文件相关联。此外,我已将这些自定义范围与客户端应用程序相关联。App ID 仪表板中的一切似乎都很好。但是,当我以编程方式或通过 curl 调用令牌端点时,我总是在访问令牌中获得相同的默认范围。

阅读Swagger,我应该能够指定密码流和不记名令牌的范围,但我处于 OAuth 2.0 授权代码流中。此外,即使使用密码凭据流,我也无法获得这些自定义范围,尽管我在请求中指定了它们。

有没有人遇到过这些问题?任何帮助将非常感激。

非常感谢,克里斯

4

3 回答 3

1

为了在令牌中查看应用程序配置的范围,您需要使用您配置范围的应用程序以及您分配角色的用户进行身份验证。

这意味着您应该在请求授权标头中使用应用程序的username : client IDpassword : secret,并与您分配匹配角色(包含所需范围)的用户进行身份验证。

向应用程序添加访问控制的步骤:

  1. 转到应用程序并通过添加范围来定义要保护的应用程序。
  2. 通过转到角色和配置文件 > 角色 > 创建角色来创建您的角色。
  3. 通过转到角色和配置文件 > 用户配置文件,将角色分配给特定用户。然后,选择要为其分配角色的用户,然后单击更多选项菜单 > 分配角色。

有关更多信息,请参阅 AppID 访问控制文档:https ://cloud.ibm.com/docs/services/appid?topic=appid-access-control

于 2020-02-03T23:16:39.563 回答
0

我在 us-south 有一个 App ID 实例,并且使用默认的 Cloud Directory,范围对我来说工作正常。

  1. 创建一个新的应用程序(定义你的范围)
  2. 创建角色并关联您的应用程序范围
  3. 将角色分配给用户
  4. 呼叫/token端点
于 2020-02-03T14:52:24.177 回答
0

之前发生在我身上,我发现解决它的一种方法是将角色注入令牌声明,然后指示 Spring Security 提取它们。我在这里详细写过文档解释了第一部分,但要点是这个 cURL 片段:

curl -X PUT "https://$REGION.appid.cloud.ibm.com/management/v4/$TENANT_ID/config/tokens" -H 'Content-Type: application/json' -H "Authorization: Bearer $IAM_TOKEN" -d '{
   "access": {
         "expires_in": 3600
   },
   "refresh": {
         "enabled": true,
         "expires_in": 2592001
   },
   "anonymousAccess": {
         "enabled": false
   },
   "accessTokenClaims": [
         {
         "source": "roles"
         }
   ],
   "idTokenClaims": [
         {
         "source": "saml",
         "sourceClaim": "attributes.uid"
         }
   ]
}'

您也可以在Swagger UI中执行此操作。但请注意,这是一个 PUT 请求,因此它将覆盖您之前的任何配置。理想情况下,运行 GET 请求以获取当前配置,然后将声明添加到其中以避免出现问题。

然后,在 SecurityConfiguration 中,添加这个 JWT 转换器:

protected void configure(HttpSecurity http) throws Exception {
    http
        //...
        .oauth2ResourceServer()
        .jwt()
        .jwtAuthenticationConverter(jwtAuthenticationConverter());
}

Converter jwtAuthenticationConverter() {
    JwtGrantedAuthoritiesConverter converter = new JwtGrantedAuthoritiesConverter();
    converter.setAuthoritiesClaimName("authorities");
    converter.setAuthorityPrefix(""); //so that the role has the same name as the one that comes from App ID
    JwtAuthenticationConverter jwtAuthenticationConverter = new JwtAuthenticationConverter();
    jwtAuthenticationConverter.setJwtGrantedAuthoritiesConverter(converter);
    return jwtAuthenticationConverter;
}

现在 Spring Security 识别了角色,您可以使用注释或 antMatcher 配置来保护端点:

.antMatchers("/api/admin").hasRole("ADMIN")
于 2021-11-24T18:57:05.323 回答