所有KeyCloak Admin API调用上的NullPointerException

时间:2018-08-22 01:15:54

标签: java security nullpointerexception keycloak

我正在尝试使用Java客户端调用KeyCloak Admin API。无论我尝试哪种操作-创建领域,创建用户帐户等-我都会收到以下异常:

  

javax.ws.rs.ProcessingException:java.lang.NullPointerException在   org.keycloak.admin.client.resource.BearerAuthFilter.filter(BearerAuthFilter.java:53)     在   org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.filterRequest(ClientInvocation.java:573)     在   org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.invoke(ClientInvocation.java:438)     在   org.jboss.resteasy.client.jaxrs.internal.proxy.ClientInvoker.invoke(ClientInvoker.java:102)     在   org.jboss.resteasy.client.jaxrs.internal.proxy.ClientProxy.invoke(ClientProxy.java:76)     com.sun.proxy。$ Proxy214.create(未知来源)

我的KeyCloak代码看起来像这样...

Keycloak kc = KeycloakBuilder.builder().realm("master").clientId("admin-cli").username("admin") .password("password").serverUrl("http://localhost:8880/auth")
                .resteasyClient(new ResteasyClientBuilder().connectionPoolSize(10).build()).build();

RealmRepresentation realm = new RealmRepresentation();
        realm.setDisplayName(displayName);
        realm.setDisplayNameHtml(displayName);
        realm.setRealm(realmName);
        realm.setEnabled(enabled);

kc.realms().create(realm);

这是我看到的异常。您对这里可能出什么问题有任何想法吗?我看到针对admin API的所有操作上的异常。我知道数据可用,因为我可以对admin API进行curl调用,并且可以正常工作。

curl -vki -H "Authorization: Bearer XXXXX" http://localhost:8880/auth/admin/realms/master

打电话时,我看到了预期的结果。使用Java客户端时,我只会失败。知道这里发生了什么吗?

UPDATE

我增加了KeyCloak实例上的调试日志。尝试创建安全领域后,我看到以下消息。 (注意:无论执行什么操作,我都会看到类似的错误)

14:36:16,553 DEBUG [org.keycloak.transaction.JtaTransactionWrapper] (default task-24) new JtaTransactionWrapper
14:36:16,553 DEBUG [org.keycloak.transaction.JtaTransactionWrapper] (default task-24) was existing? false
14:36:16,557 DEBUG [org.keycloak.authentication.AuthenticationProcessor] (default task-24) AUTHENTICATE CLIENT
14:36:16,557 DEBUG [org.keycloak.authentication.ClientAuthenticationFlow] (default task-24) client authenticator: client-secret
14:36:16,557 DEBUG [org.keycloak.authentication.ClientAuthenticationFlow] (default task-24) client authenticator SUCCESS: client-secret
14:36:16,557 DEBUG [org.keycloak.authentication.ClientAuthenticationFlow] (default task-24) Client admin-cli authenticated by client-secret
14:36:16,558 DEBUG [org.keycloak.authentication.AuthenticationProcessor] (default task-24) AUTHENTICATE ONLY
14:36:16,559 DEBUG [org.keycloak.authentication.DefaultAuthenticationFlow] (default task-24) processFlow
14:36:16,559 DEBUG [org.keycloak.authentication.DefaultAuthenticationFlow] (default task-24) check execution: direct-grant-validate-username requirement: REQUIRED
14:36:16,559 DEBUG [org.keycloak.authentication.DefaultAuthenticationFlow] (default task-24) authenticator: direct-grant-validate-username
14:36:16,560 DEBUG [org.keycloak.authentication.DefaultAuthenticationFlow] (default task-24) invoke authenticator.authenticate: direct-grant-validate-username
14:36:16,560 DEBUG [org.keycloak.authentication.DefaultAuthenticationFlow] (default task-24) authenticator SUCCESS: direct-grant-validate-username
14:36:16,561 DEBUG [org.keycloak.authentication.DefaultAuthenticationFlow] (default task-24) check execution: direct-grant-validate-password requirement: REQUIRED
14:36:16,561 DEBUG [org.keycloak.authentication.DefaultAuthenticationFlow] (default task-24) authenticator: direct-grant-validate-password
14:36:16,561 DEBUG [org.keycloak.authentication.DefaultAuthenticationFlow] (default task-24) invoke authenticator.authenticate: direct-grant-validate-password
14:36:16,658 DEBUG [org.keycloak.authentication.DefaultAuthenticationFlow] (default task-24) authenticator SUCCESS: direct-grant-validate-password
14:36:16,658 DEBUG [org.keycloak.authentication.DefaultAuthenticationFlow] (default task-24) check execution: direct-grant-validate-otp requirement: OPTIONAL
14:36:16,659 DEBUG [org.keycloak.authentication.DefaultAuthenticationFlow] (default task-24) authenticator: direct-grant-validate-otp
14:36:16,659 DEBUG [org.keycloak.authentication.DefaultAuthenticationFlow] (default task-24) invoke authenticator.authenticate: direct-grant-validate-otp
14:36:16,659 DEBUG [org.keycloak.authentication.DefaultAuthenticationFlow] (default task-24) authenticator ATTEMPTED: direct-grant-validate-otp
14:36:16,661 DEBUG [org.keycloak.services.managers.AuthenticationSessionManager] (default task-24) Removing authSession 'df66e278-8ffe-47ab-84d1-36ffe9152021'. Expire restart cookie: true
14:36:16,673 DEBUG [org.keycloak.events] (default task-24) type=LOGIN, realmId=master, clientId=admin-cli, userId=c830af10-3cfd-4ce0-b1aa-83a857a290d7, ipAddress=172.18.0.1, auth_method=openid-connect, token_id=8876c7ad-3ea4-4356-8fc2-f9b50112b952, grant_type=password, refresh_token_type=Refresh, scope='profile email', refresh_token_id=4c2325fc-0bb5-40d9-af55-4985546fd39a, client_auth_method=client-secret, username=kcadmin
14:36:16,673 DEBUG [org.keycloak.transaction.JtaTransactionWrapper] (default task-24) JtaTransactionWrapper  commit
14:36:16,676 DEBUG [org.keycloak.transaction.JtaTransactionWrapper] (default task-24) JtaTransactionWrapper end

更新2

似乎此问题与在Java EE容器中运行此代码有关。如果我在独立类中运行完全相同的代码,则可以正常工作。但是,如果我将代码复制并粘贴到servlet并尝试运行它,则会收到NullPointerException。遇到问题时,我正在Wildfly 13上运行代码。通过Arquillian运行集成测试时,我也看到此问题。我的假设是它必须与依赖项有关,但是我对需要更改的内容不知所措。

4 个答案:

答案 0 :(得分:4)

如果要保留对WildFly 13的Java EE 8支持,只需创建空的jackson提供程序:

public class CustomJacksonProvider extends ResteasyJackson2Provider {

}

并使用register()的{​​{1}}方法将其添加到KeyCloak构建器中:

ResteasyClientBuilder

您还需要添加一些轻松的依赖项(例如Keycloak kc = KeycloakBuilder.builder() .realm("master") .clientId("admin-cli") .username("admin") .password("password") .serverUrl("http://localhost:8880/auth") .resteasyClient(new ResteasyClientBuilder().connectionPoolSize(10).register(new CustomJacksonProvider()).build()) .build(); )才能使用scope=providedResteasyClientBuilder

ResteasyClientBuilder

答案 1 :(得分:2)

简单地(可能甚至太多)放置一个与Resteasy库相关的错误,其中JsonBindingProvider的优先级高于ResteasyJackson2Provider,这使得json负载中的某些属性无法映射适当地,他们的价值观迷失了。因此,NullPointerException。

如果您使用的是JBoss或WildFly,最简单的解决方案应该是从resteasy-json-binding-provider文件的部署中排除jboss-deployment-structure.xml模块:

<jboss-deployment-structure>
    <deployment>
        <exclusions>
            <module name="org.jboss.resteasy.resteasy-json-binding-provider"/>
        </exclusions>
    </deployment>
</jboss-deployment-structure>

答案 2 :(得分:0)

您没有授权标头。请尝试

Keycloak kc = KeycloakBuilder.builder().realm("master").clientId("admin-cli").username("admin") .password("password").authorization("authorization").serverUrl("http://localhost:8880/auth")
            .resteasyClient(new ResteasyClientBuilder().connectionPoolSize(10).build()).build();

请注意

.authorization("authorization")

答案 3 :(得分:0)

解决了!!显然,我的Wildfly 13.0实例已配置为启用Java EE 8支持。这似乎是问题的根本原因,因为删除该标志使所有测试用例都能按预期工作。不知道为什么会出现问题,但这绝对是潜在的问题。