将 Keycloak Admin Client 3.1.0.Final 与 Spring Boot 1.5.1 集成

Integrate Keycloak Admin Client 3.1.0.Final with Spring Boot 1.5.1

我正在尝试将 KeyCloak Admin Client 与 Spring Boot

集成

但是当我尝试创建一个新帐户时抛出异常:

Caused by: javax.ws.rs.ProcessingException: RESTEASY003145: Unable to find a MessageBodyReader of content-type application/json and type class org.keycloak.representations.AccessTokenResponse

所以我尝试像这样为 KeyCloak 显式注册 Jackson Provider:

            KeycloakBuilder
            .builder()
            .serverUrl(SERVER_URL)
            .realm(REALM)
            .username(USERNAME)
            .password(PASSWORD)
            .clientId(CLIENT_ID)
            .resteasyClient(new ResteasyClientBuilder()
                  .providerFactory(factory.register(ResteasyJackson2Provider.class))
                    .connectionPoolSize(10)
                    .build())
            .build();

但是我无法导入 ResteasyJackson2Provider.class

pom.xml

<dependency>
    <groupId>org.keycloak</groupId>
    <artifactId>keycloak-spring-security-adapter</artifactId>
    <version>3.1.0.Final</version>
</dependency>
<dependency>
    <groupId>org.keycloak</groupId>
    <artifactId>keycloak-admin-client</artifactId>
    <version>3.1.0.Final</version>
</dependency>
<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-jaxrs</artifactId>
    <version>3.1.3.Final</version>
</dependency>
<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-client</artifactId>
    <version>3.1.3.Final</version>
</dependency>
<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-jackson2-provider</artifactId>
    <version>3.1.3.Final</version>
</dependency>

顺便说一句,如果我使用 resteasy-jackson-provider,我会得到这个异常:

javax.ws.rs.client.ResponseProcessingException: javax.ws.rs.ProcessingException: org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "access_token" (Class org.keycloak.representations.AccessTokenResponse), not marked as ignorable
 at [Source: org.jboss.resteasy.client.jaxrs.internal.ClientResponse$InputStreamWrapper@7cc842b0; line: 1, column: 18] (through reference chain: org.keycloak.representations.AccessTokenResponse["access_token"])

经过一段时间,我想出了解决这个问题的方法:

先用standalone工程(空maven工程)测试,问题依旧,因为resteasy-jackson-provider。它应该是resteasy-jackson2-provider(注意它的resteasy-jackson2-provider)

目前,我知道问题出自 Spring Boot,RESTeasy。最后,我想出了这个工作 pom.xml:

    <dependency>
        <groupId>org.keycloak</groupId>
        <artifactId>keycloak-admin-client</artifactId>
        <version>3.1.0.Final</version>
    </dependency>
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-client</artifactId>
        <version>3.1.3.Final</version>
    </dependency>
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-jackson2-provider</artifactId>
        <version>3.1.3.Final</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.8.8</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.8.8</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.8.8</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.jaxrs</groupId>
        <artifactId>jackson-jaxrs-json-provider</artifactId>
        <version>2.8.8</version>
    </dependency>

还有一点要注意,您应该在 Keycloak 中使用 admin-cli 客户端,因为默认情况下,它 直接访问授权已启用

顺便说一句,这是 Java 配置:

            KeycloakBuilder
            .builder()
            .serverUrl("localhost:8080/auth")
            .realm("master")
            .username("YOUR_USERNAME")
            .password("YOUR_PASSWORD")
            .clientId("admin-cli")
            .build();

工作示例:https://github.com/phuongtailtranminh/Keycloak-Admin-Client-Spring-Boot-Demo