为什么带有 Spring 引导教程的 Keycloak 不起作用?

Why won't the Keycloak with Spring Boot tutorial work?

我一直在尝试关注 the Keycloak tutorial from DZone (EDIT: which seems to be a nearly verbatim repost from the tutorial on the official Keycloak blog,显然是同一个作者),但我 运行 遇到了以下问题:

我做错了什么?

该教程中有许多细微但重要的内容 inconsistencies/typos。按出现顺序:

主要教程

  1. index.html 的位置给出为 /src/resources/static。如果您使用 Spring Initializr,预制代码将在 src 下同时具有 maintest 目录,因此正确的位置将改为 src/main/resources/static .

  2. 在控制器的代码中,第 9 行是

    return "products";
    

    这告诉 Spring 使用 products.html。实际的 HTML 文件将是 product,单数,因为它是从 product.ftl 生成的。将 return 值减去 's'。

  3. application.properties 文件中,给定的行之一是

    keycloak.realm=springboot
    

    早些时候,在 Keycloak 管理面板中,指南建议将领域命名为 "SpringBoot"。这些值需要匹配,并且 they are case-sensitive。改变其中一个。

  4. 设置完成后,说明说

    Now browse to “http://localhost:8080

    正确的 URL 是 http://localhost:8081,因为 server.portapplication.properties 中设置为 8081。管理控制台应该仍在使用端口 8080。

进行这些更改后,测试应用程序应按所述方式加载和运行。

添加 Spring 安全性

在新的 getProducts() 方法中复制时,请注意不要重新引入 products/product 拼写错误。

如今,要使官方文档正常工作,您可以使用 SOLO 的答案并补充以下代码:

将 keycloak 添加到 POM

<dependency>
    <groupId>org.keycloak</groupId>
    <artifactId>keycloak-spring-boot-starter</artifactId>
</dependency>

将依赖管理添加到 POM

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.keycloak.bom</groupId>
            <artifactId>keycloak-adapter-bom</artifactId>
            <version>10.0.2</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

将 .ftl 格式更改为 .ftlh 并像这样使用:

<html>
<h2>My products</h2>
<ul>
    <#list products as product>
        <li>${product}</li>
    </#list>
</ul>
<p>
    <a href="/logout">Logout</a>
</p>