问题描述
因此,我使用keycloak作为身份验证机制,并通过修改login.ftl文件来自定义登录页面。到现在为止还挺好。到目前为止,我已经按照keycloak的文档指示,将template.ftl文件导入了我的login.ftl文件。在该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文件访问该错误消息?预先感谢您的指导。
解决方法
您可以像在template.ftl中一样在login.ftl中访问它
<#if message?has_content>
message.summary
...
</#if>
我认为您在login.ftl中未设置模板变量(displayMessage,isAppInitiatedAction)时遇到了问题
,如果要在登录页面中添加新字段,并且需要对其进行验证并在该字段无效时显示错误消息,则需要编写提供程序。自定义提供程序必须实现FormAction和FormActionFactory。有一种称为validate的方法,您可以在内部编写逻辑以验证字段,还可以为错误消息定义键和值。可以在您的login.ftl文件中使用此密钥。 这个主题对我很有帮助Keycloak - Custom form action not visible in flow