如何在Keycloak的注册表单中添加用户属性?

huangapple go评论48阅读模式
英文:

How to add user attributes to registration form in Keycloak?

问题

I've created a new realm (out of the box and using user account for login/registration) in Keycloak and enabled the user profile, added a new attribute - shop.

The user attribute.

Because this attribute is required, I want it to be in the default registration form.

Is this possible to do?

It will be nice to have the user attributes in the JWT token as well.

英文:

I've created a new realm (out of the box and using user account for login/registration) in Keycloak and enabled the user profile, added a new attribute - shop

如何在Keycloak的注册表单中添加用户属性?

The user attribute
如何在Keycloak的注册表单中添加用户属性?

Because this attribute is required, I want it to be in the default registration form

如何在Keycloak的注册表单中添加用户属性?

Is this possible to do?

It will be nice to have the user attributes in the JWT token as well

答案1

得分: 3

您可以通过添加自定义主题(可以通过覆盖主题来实现,但他们不建议覆盖主题)向注册表单添加字段。

在主题文件夹中添加一个自定义主题。

自定义./themes/custom-theme/login/register.ftl文件以添加以下自定义字段:
(请按照链接进行操作)

<div class="${properties.kcFormGroupClass!}">
                <div class="${properties.kcLabelWrapperClass!}">
                    <label for="user.attributes.dob" class="${properties.kcLabelClass!}">
                    出生日期</label>
                </div>

                <div class="${properties.kcInputWrapperClass!}">
                    <input type="date" class="${properties.kcInputClass!}" 
                    id="user.attributes.dob" name="user.attributes.dob" 
                    value="${(register.formData['user.attributes.dob']!'')}"/>
                </div>
            </div>

然后在成功注册后,出生日期将作为用户属性添加。

英文:

You can add a field to the registration form by adding a custom theme (You might do it by overriding the main theme, but they don't recommend overriding the main theme).

Add a custom theme in the themes folder.

Customize your ./themes/custom-theme/login/register.ftl file to add a custom field like the below:
(Please follow the link)

<div class="${properties.kcFormGroupClass!}">
                <div class="${properties.kcLabelWrapperClass!}">
                    <label for="user.attributes.dob" class="${properties.kcLabelClass!}">
                    Date of birth</label>
                </div>

                <div class="${properties.kcInputWrapperClass!}">
                    <input type="date" class="${properties.kcInputClass!}" 
                    id="user.attributes.dob" name="user.attributes.dob" 
                    value="${(register.formData['user.attributes.dob']!'')}"/>
                </div>
            </div>

Then the dob will be added as user attributes after a successful registration.

huangapple
  • 本文由 发表于 2023年4月6日 19:24:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75948965.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定