【发布时间】:2021-07-08 08:30:05
【问题描述】:
在我看来,使用 Keycloak,您可以通过多种不同方式进行授权。但是,我仍然试图弄清楚所有这些是如何工作的。
我们有数百个 API,它们都绑定到 GET、DELETE、PATCH 和 POST。到目前为止我发现为了映射这些HTTP方法,就是将它们添加为scope。
所以假设我们有一个带有这个 URI 的 API:/api/investor/{id}。这可以是GET、DELETE 或PATCH。现在这就是我们应该配置它的方式(为简单起见,我只是尝试用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