由于 Keycloak v24 自定义 UserAttributes 默认情况下未打开(https://github.com/keycloak/keycloak/issues/9889)。这些选项的解释如下:https://www.keycloak.org/docs/latest/server_admin/#_understanding-management-and-unmanagement-attributes
我使用 Python3 Keycloak 模块 (https://pypi.org/project/python-keycloak/) 与 keycloak REST API 进行通信。但是,在创建领域时,我无法设置 UnmanagementAttributePolicy 来启用这些自定义用户属性,并且官方文档相当缺乏(https://www.keycloak.org/docs-api/24.0.1/rest-api/index .html#UnmanagedAttributePolicy).
有人知道该怎么做吗?预先感谢!
python-keycloak
中没有配置文件API,但您可以通过raw_put()(PUT REST API)来完成
配置文件 PUT 的有效负载也具有先前的属性。 所以我先调用 GET API,添加非托管属性,然后调用 PUT API。
from keycloak import KeycloakOpenIDConnection, KeycloakAdmin
import json
keycloak_connection = KeycloakOpenIDConnection(
server_url='http://localhost:8080',
username='admin',
password='admin',
realm_name='master',
client_id='admin-cli',
verify=True
)
keycloak_admin = KeycloakAdmin(connection=keycloak_connection)
keycloak_admin.change_current_realm('my-realm')
current = keycloak_admin.get_current_realm()
print('current realm : ' + current)
# Get current profile
profile_url = 'http://localhost:8080/admin/realms/my-realm/users/profile'
profiles = keycloak_connection.raw_get(profile_url)
attributes = profiles.json()['attributes']
# Add unmanaged Attribute
attributes.append({
'name': 'custom',
'displayName': '${custom}',
'validations': {'length': {'max': 255}},
'annotations': {},
'permissions': { 'view': ['admin'], 'edit': ['admin', 'user'] },
'multivalued': False
})
# new profile's payload
new_profiles = {
'attributes' : attributes,
'groups': profiles.json()['groups']
}
# Update profile
result = keycloak_connection.raw_put(profile_url,json.dumps(new_profiles))
print(result)
# Get new profile
update_profiles = keycloak_connection.raw_get(profile_url)
print(json.dumps(update_profiles.json()))
添加了自定义属性
详细自定义属性
在这里
{
"name": "custom",
"displayName": "${custom}",
"validations": {
"length": {
"max": 255
}
},
"annotations": {},
"permissions": {
"view": [
"admin"
],
"edit": [
"admin",
"user"
]
},
"multivalued": false
}