You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Keycloak 21自定义注册模板未生效问题求助

问题:Keycloak自定义注册模板不生效,仍显示默认页面

我在自定义注册认证器中编写了以下代码:

@Override
public void buildPage(FormContext context, LoginFormsProvider form) {
    logger.info("Starting custom form...");
    form.createForm("custom_registration.ftl");
    logger.info("custom form created.");
}

我已将模板文件放在Keycloak服务器的/themes/custom_theme/login/custom_registration.ftl路径下,并且执行了以下命令:

kc.bat build
kc.bat start-dev

但系统还是显示默认的注册页面,而非我的自定义模板。

从日志可以看到,custom_registration模板已经被渲染,但随后立即加载了默认的注册模板:

2024-01-22 15:33:53,809 INFO  [org.keycloak.x.authenticator.CustomForm] (executor-thread-0) Creating custom form...
2024-01-22 15:33:53,912 DEBUG [freemarker.cache] (executor-thread-0) Couldn't find template in cache for "custom_registration.ftl"("en_US", UTF-8, parsed); will try to load it.
2024-01-22 15:33:53,913 DEBUG [freemarker.cache] (executor-thread-0) TemplateLoader.findTemplateSource("custom_registration_en_US.ftl"): Not found
2024-01-22 15:33:53,915 DEBUG [freemarker.cache] (executor-thread-0) TemplateLoader.findTemplateSource("custom_registration_en.ftl"): Not found
2024-01-22 15:33:53,915 DEBUG [freemarker.cache] (executor-thread-0) TemplateLoader.findTemplateSource("custom_registration.ftl"): Found
2024-01-22 15:33:53,915 DEBUG [freemarker.cache] (executor-thread-0) Loading template for "custom_registration.ftl"("en_US", UTF-8, parsed) from "file:/C:/Users/User/Downloads/keycloak-21.1.2/bin/../themes/custom_theme/login/custom_registration.ftl"
2024-01-22 15:33:54,002 DEBUG [freemarker.cache] (executor-thread-0) Couldn't find template in cache for "template.ftl"("en_US", UTF-8, parsed); will try to load it.
2024-01-22 15:33:54,003 DEBUG [freemarker.cache] (executor-thread-0) TemplateLoader.findTemplateSource("template_en_US.ftl"): Not found
2024-01-22 15:33:54,006 DEBUG [freemarker.cache] (executor-thread-0) TemplateLoader.findTemplateSource("template_en.ftl"): Not found
2024-01-22 15:33:54,007 DEBUG [freemarker.cache] (executor-thread-0) TemplateLoader.findTemplateSource("template.ftl"): Found
2024-01-22 15:33:54,007 DEBUG [freemarker.cache] (executor-thread-0) Loading template for "template.ftl"("en_US", UTF-8, parsed) from "jar:file:/C:/Users/User/Downloads/keycloak-21.1.2/lib/lib/main/org.keycloak.keycloak-themes-21.1.2.jar!/theme/base/login/template.ftl"
2024-01-22 15:33:54,153 INFO  [org.keycloak.x.CustomForm] (executor-thread-0) custom form created.
2024-01-22 15:33:54,179 DEBUG [freemarker.cache] (executor-thread-0) Couldn't find template in cache for "register.ftl"("en_US", UTF-8, parsed); will try to load it.
2024-01-22 15:33:54,180 DEBUG [freemarker.cache] (executor-thread-0) TemplateLoader.findTemplateSource("register_en_US.ftl"): Not found
2024-01-22 15:33:54,180 DEBUG [freemarker.cache] (executor-thread-0) TemplateLoader.findTemplateSource("register_en.ftl"): Not found
2024-01-22 15:33:54,181 DEBUG [freemarker.cache] (executor-thread-0) TemplateLoader.findTemplateSource("register.ftl"): Found
2024-01-22 15:33:54,181 DEBUG [freemarker.cache] (executor-thread-0) Loading template for "register.ftl"("en_US", UTF-8, parsed) from "jar:file:/C:/Users/Operaciones/Downloads/keycloak-21.1.2/lib/lib/main/org.keycloak.keycloak-themes-21.1.2.jar!/theme/base/login/register.ftl"
2024-01-22 15:33:54,189 DEBUG [freemarker.cache] (executor-thread-0) Couldn't find template in cache for "template.ftl"("en_US", UTF-8, parsed); will try to load it.
2024-01-22 15:33:54,191 DEBUG [freemarker.cache] (executor-thread-0) TemplateLoader.findTemplateSource("template_en_US.ftl"): Not found
2024-01-22 15:33:54,192 DEBUG [freemarker.cache] (executor-thread-0) TemplateLoader.findTemplateSource("template_en.ftl"): Not found
2024-01-22 15:33:54,192 DEBUG [freemarker.cache] (executor-thread-0) TemplateLoader.findTemplateSource("template.ftl"): Found
2024-01-22 15:33:54,192 DEBUG [freemarker.cache] (executor-thread-0) Loading template for "template.ftl"("en_US", UTF-8, parsed) from "jar:file:/C:/Users/User/Downloads/keycloak-21.1.2/lib/lib/main/org.keycloak.keycloak-themes-21.1.2.jar!/theme/base/login/template.ftl"

请问我遗漏了什么配置或步骤吗?


解决方案

从日志能看出,自定义模板确实被加载了,但之后默认的register.ftl又被加载,说明你的自定义认证器没有终止认证流程,导致后续的默认注册流程继续执行了。

你需要在buildPage方法末尾添加流程终止的代码,告诉Keycloak当前步骤已经完成,不需要继续执行后续的认证环节:

@Override
public void buildPage(FormContext context, LoginFormsProvider form) {
    logger.info("Starting custom form...");
    form.createForm("custom_registration.ftl");
    // 添加这行代码终止当前流程
    context.success();
    logger.info("custom form created.");
}

另外,还要确保以下几点:

  • 你的自定义认证器已经正确添加到Keycloak的认证流程中,并且设置为必填,优先级高于默认的注册认证器
  • 确认custom_theme已经在Keycloak的管理控制台中设置为Realm的默认主题
  • 检查custom_registration.ftl模板是否正确继承了主题的template.ftl,避免渲染异常

内容的提问来源于stack exchange,提问作者Diego Portillo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 11:22:03