Keycloak FTL:我们如何在自定义主题的登录模板中访问错误消息

Keycloak FTL: how can we access error messages within the login template on a custom theme

所以我使用 keycloak 作为身份验证机制,并通过修改 login.ftl 文件来自定义登录页面。到目前为止,一切都很好。我正在我的 login.ftl 中导入 template.ftl 文件 - 按照 keycloak 文档的指示 - 到目前为止一切顺利。在该 template.ftl 文件中 - 有一种访问错误消息的方法,如此处所示

          <#if displayMessage && message?has_content && (message.type != 'warning' || !isAppInitiatedAction??)>
          <div class="alert alert-${message.type}">
              <#if message.type = 'success'><span class="${properties.kcFeedbackSuccessIcon!}"></span></#if>
              <#if message.type = 'warning'><span class="${properties.kcFeedbackWarningIcon!}"></span></#if>
              <#if message.type = 'error'><span class="${properties.kcFeedbackErrorIcon!}"></span></#if>
              <#if message.type = 'info'><span class="${properties.kcFeedbackInfoIcon!}"></span></#if>
              <span class="kc-feedback-text">${kcSanitize(message.summary)?no_esc}</span>
          </div>
      </#if>

好的,太棒了!如果我不想处理 template.ftl 文件中的错误消息 ui 怎么办?我在 login.ftl 页面上有一个表单 UI,我想在其中显示错误消息。我如何将该消息向下传递到 login.ftl 文件或访问该错误消息来自 login.ftl 文件?提前感谢任何gui舞蹈。

您可以在 login.ftl 中访问它,就像在 template.ftl

中一样
<#if message?has_content>
    message.summary
    ...
</#if>

我认为您在 login.ftl

中未设置模板变量(displayMessage、isAppInitiatedAction)时遇到问题

如果您要在登录页面中添加新字段并且需要对其进行验证并在字段无效时显示错误消息,则需要编写一个提供程序。自定义提供者必须实现 FormAction 和 FormActionFactory。有一个名为 validate 的方法,您可以在其中编写逻辑来验证字段并为错误消息定义键和值。此密钥可用于您的 login.ftl 文件。 这个帖子对我有帮助