Intellij Idea 如何配置Keycloak扩展开发环境?

ztmd8pv5  于 2023-08-03  发布在  其他
关注(0)|答案(2)|浏览(209)

我希望用一些自定义的东西来扩展Keycloak(即org.keycloak.authentication.RequiredActionsProvider接口的实现)。
我正在为以下配置而奋斗:

  • 产生DEBUG输出
  • 最终使用IDE调试我的实现(即IntelliJIdea社区)

我在docker容器中启动keycloak:

version: '3.0'
services:
  keycloak:
    image: quay.io/keycloak/keycloak:${keycloak_version:-latest}
    environment:
      KC_HOSTNAME: auth.lvh.me
      KEYCLOAK_ADMIN: admin
      KEYCLOAK_ADMIN_PASSWORD: Password1#
      DEBUG: true
      PRINT_ENV: true
    ports: [ "8080:8080", "8787:8787" ]
    command: [ "start-dev", "--log-level", "DEBUG",  "--debug" ]
    volumes:
      - ./target:/opt/keycloak/providers
    working_dir: /opt/keycloak
    networks:
      default:
        aliases:
          - auth.lvh.me

字符串
我使用基本的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.lrkwz.keycloak</groupId>
    <artifactId>keycloak-required-actions</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <keycloak.version>21.1.0</keycloak.version>
        <maven.compiler.version>3.10.1</maven.compiler.version>
        <maven.compiler.release>11</maven.compiler.release>
        <maven-shade.version>3.2.4</maven-shade.version>
        <maven-surefire.version>3.0.0-M9</maven-surefire.version>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.github.dasniko</groupId>
                <artifactId>keycloak-spi-bom</artifactId>
                <version>${keycloak.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.keycloak</groupId>
            <artifactId>keycloak-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.keycloak</groupId>
            <artifactId>keycloak-server-spi</artifactId>
        </dependency>
        <dependency>
            <groupId>org.keycloak</groupId>
            <artifactId>keycloak-server-spi-private</artifactId>
        </dependency>
        <dependency>
            <groupId>org.keycloak</groupId>
            <artifactId>keycloak-services</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

    <repositories>
        <repository>
            <id>jitpack.io</id>
            <url>https://jitpack.io</url>
        </repository>
    </repositories>

    <build>
        <finalName>${project.groupId}-${project.artifactId}</finalName>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>${maven.compiler.version}</version>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-shade-plugin</artifactId>
                    <version>${maven-shade.version}</version>
                    <configuration>
                        <artifactSet>
                            <excludes>
                                <exclude>org.keycloak*</exclude>
                                <exclude>org.apache.httpcomponents:httpcore</exclude>
                            </excludes>
                        </artifactSet>
                    </configuration>
                    <executions>
                        <execution>
                            <goals>
                                <goal>shade</goal>
                            </goals>
                            <phase>package</phase>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>${maven-surefire.version}</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>


结果:

  • KC启动时未打印环境
  • 只有错误级别的日志记录显示为容器输出
  • 无法附加IDE调试器

我还想 * 在更改代码时**热重载 * 我的组件有一个更流畅的开发体验(目前我需要mvn package +停止和启动容器)。
参考文件:

  1. https://www.keycloak.org/server/logging
  2. https://github.com/keycloak/keycloak/blob/main/quarkus/dist/src/main/content/bin/kc.sh
vlf7wbxs

vlf7wbxs1#

感谢您使用我定制的keycloak-spi-bom;)
您的pom.xml看起来不错,供您参考(如果还没有这样做),请参阅我的Keycloak扩展的示例存储库:https://github.com/dasniko/keycloak-extensions-demo
在我的本地Docker Compose环境中,我还设置了环境变量DEBUG_PORT: '*:8787'。这与DEBUG: 'true' env var一起应该足够了。不需要command参数。
对于日志级别,你的CLI参数应该可以工作,但是,我总是使用通过env vars的方式,它是KC_LOG_LEVEL: DEBUG
关于热钉仓:坏消息,因为Quarkus没有更多的热重装可能。不幸的是,在重新构建JAR之后,您必须重新启动整个容器。

4uqofj5v

4uqofj5v2#

感谢@dasniko为我指明了正确的方向(以及无价的keycloak-spi-bom!).
这里的主要问题是auth.lvh.me的使用:调试配置触发器仅适用于localhost
所以docker-compose.yml应该是:

version: '3.0'
services:
  keycloak:
    image: quay.io/keycloak/keycloak:${keycloak_version:-latest}
    environment:
      KEYCLOAK_ADMIN: admin
      KEYCLOAK_ADMIN_PASSWORD: Password1#
      DEBUG: true
      DEBUG_PORT: '*:8787'
      KC_LOG_LEVEL: DEBUG
    ports: [ "8080:8080", "8787:8787" ]
    command: [ "start-dev" ]
    volumes:
      - ./target:/opt/keycloak/providers
    working_dir: /opt/keycloak

字符串

相关问题