【问题标题】:Is it possible to map permissions to HTTP methods in Keycloak integrated with Spring Boot?是否可以将权限映射到与 Spring Boot 集成的 Keycloak 中的 HTTP 方法?
【发布时间】:2021-07-08 08:30:05
【问题描述】:

在我看来,使用 Keycloak,您可以通过多种不同方式进行授权。但是,我仍然试图弄清楚所有这些是如何工作的。

我们有数百个 API,它们都绑定到 GET、DELETE、PATCH 和 POST。到目前为止我发现为了映射这些HTTP方法,就是将它们添加为scope

所以假设我们有一个带有这个 URI 的 API:/api/investor/{id}。这可以是GETDELETEPATCH。现在这就是我们应该配置它的方式(为简单起见,我只是尝试用linked 显示它们,因为它们是在 Keyclaok 配置中分配的,所以请忽略方向,因为它可能与实际方向不同Keycloak 类图):


user-one =linked=> call-centre-read-only-role =linked=> read-only-policy =linked=> 
    view-permission =linked=> investor-resource-read(/api/investor/{id}) =linked=> scope(GET)

现在我们需要另一个Resource 定义,用于具有读写权限的用户


user-two =linked=> admin-read-write-role =linked=> readwrite-policy =linked=> 
    write-permission =linked=> investor-resource-write(/api/investor/{id}) =linked=> scope(GET,DELETE,PATCH)

正如您所见,相同的资源(具有相同的 URI)被复制,这是因为从我们的角度来看,权限在 scope 中定义为 HTTP 方法。

鉴于我们有许多 API,这似乎非常复杂且难以配置。但是,我认为如果我们必须这样定义它会更好:

user-one =linked=> call-centre-read-only-role =linked=> read-only-policy =linked=> 
    read-permission:scope(GET) =linked=> investor-resource(/api/investor/{id}) 

对于管理员:

user-two =linked=> admin-read-write-role =linked=> readwrite-policy =linked=> 
    update-permission:scope(PATCH) =linked=> investor-resource(/api/investor/{id}) 

user-two =linked=> admin-read-write-role =linked=> readwrite-policy =linked=> 
    delete-permission:scope(DELETE) =linked=> investor-resource(/api/investor/{id}) 

这样我们可以重新使用相同的Resource 定义以获得多个权限。现在的问题是,通过这种配置,Keycloak 适配器是否可以识别 HTTP 方法并据此授权用户。如果没有,我们还能怎么做才能重用Resources?

【问题讨论】:

    标签: spring-boot keycloak


    【解决方案1】:

    我认为 Keycloak 适配器可以开箱即用。您可能需要查看documentation on policy enforcers

    有了这个,你可以通过如下配置将你的 http 方法映射到一个作用域:

    {
      "policy-enforcer": {
        ...
        "paths": [
          {
            "name" : "Investor resource",
            "path" : "/api/investor/{id}",
            "methods" : [
              {
                "method": "GET",
                "scopes" : ["urn:my.custom.app:scopes:view"]
              },
              {
                "method": "PUT",
                "scopes" : ["urn:my.custom.app:scopes:view", "urn:my.custom.app:update"]
              },
              {
                "method": "DELETE",
                "scopes" : ["urn:my.custom.app:scopes:remove"]
              }
            ]
          },
          ...
      }
    }
    

    【讨论】:

    • 它会针对Resource 执行此操作,并且当我们拥有基于范围的权限时它会忽略它。如果我们有一个映射到 GET 的基于范围的权限,但资源映射到 GETDELETE,则它不会为 DELETE 请求返回 DENIED,因为您的资源具有 get 的两个范围/删除
    猜你喜欢
    • 2019-04-28
    • 2022-11-17
    • 2019-03-28
    • 2020-01-11
    • 2023-03-09
    • 2022-11-01
    • 1970-01-01
    • 2013-04-07
    • 2011-06-14
    相关资源
    最近更新 更多