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

时间:2017-06-06 03:51:39

标签: spring-boot resteasy keycloak

我试图将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"])

1 个答案:

答案 0 :(得分:3)

过了一会儿,我找到了解决这个问题的方法:

首先,我使用独立项目(空maven项目)对其进行了测试,由于resteasy-jackson-provider,问题仍然存在。它应该是resteasy-jackson2-provider(注意它的重新 - jackson2 - 提供者)

目前,我知道这个问题不知何故来自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 客户端,因为默认情况下, 启用Direct Access Grants

顺便说一句,这是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