使用 keycloak 管理客户端显示用户电子邮件不起作用(UnrecognizedPropertyException)

Displaying a User Email using keycloak Admin Client not working(UnrecognizedPropertyException)

我正在尝试显示一个 userEmail() 或显示有关我们领域中某个用户的所有信息。

我正在尝试调用此方法:-

@Test 
    public void displayUser(){

        UsersResource users=kc.realm("SpringBoot").users();
        UserResource user=users.get("b3479699-430b-4cd3-be96-d26db584d207"); //Succeeds, we have the user
        UserRepresentation ur=user.toRepresentation();
        System.out.println(ur.getEmail());


    }

但是它失败了,给我这个错误:-|

javax.ws.rs.client.ResponseProcessingException: javax.ws.rs.ProcessingException: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "notBefore" (class org.keycloak.representations.idm.UserRepresentation), not marked as ignorable (25 known properties: "disableableCredentialTypes", "enabled", "emailVerified", "origin", "self", "applicationRoles", "createdTimestamp", "clientRoles", "groups", "username", "totp", "id", "email", "federationLink", "serviceAccountClientId", "lastName", "clientConsents", "socialLinks", "realmRoles", "attributes", "firstName", "credentials", "requiredActions", "federatedIdentities", "access"])
 at [Source: org.apache.http.conn.EofSensorInputStream@4ee203eb; line: 1, column: 248] (through reference chain: org.keycloak.representations.idm.UserRepresentation["notBefore"])
        at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:62)
        at com.fasterxml.jackson.databind.DeserializationContext.handleUnknownProperty(DeserializationContext.java:834)
        at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:1093)
        at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1489)
        at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1467)
        at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:282)
        at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:140)
        at com.fasterxml.jackson.databind.ObjectReader._bind(ObjectReader.java:1583)
        at com.fasterxml.jackson.databind.ObjectReader.readValue(ObjectReader.java:964)
        at org.jboss.resteasy.plugins.providers.jackson.ResteasyJackson2Provider.readFrom(ResteasyJackson2Provider.java:127)
        at org.jboss.resteasy.core.interception.AbstractReaderInterceptorContext.readFrom(AbstractReaderInterceptorContext.java:61)
        at org.jboss.resteasy.core.interception.AbstractReaderInterceptorContext.proceed(AbstractReaderInterceptorContext.java:53)
        at org.jboss.resteasy.plugins.interceptors.encoding.GZIPDecodingInterceptor.aroundReadFrom(GZIPDecodingInterceptor.java:59)
        at org.jboss.resteasy.core.interception.AbstractReaderInterceptorContext.proceed(AbstractReaderInterceptorContext.java:55)
        at org.jboss.resteasy.client.jaxrs.internal.ClientResponse.readFrom(ClientResponse.java:251)
        at org.jboss.resteasy.client.jaxrs.internal.ClientResponse.readEntity(ClientResponse.java:181)
        at org.jboss.resteasy.specimpl.BuiltResponse.readEntity(BuiltResponse.java:213)
        at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.extractResult(ClientInvocation.java:105)
        at org.jboss.resteasy.client.jaxrs.internal.proxy.extractors.BodyEntityExtractor.extractEntity(BodyEntityExtractor.java:60)
        at org.jboss.resteasy.client.jaxrs.internal.proxy.ClientInvoker.invoke(ClientInvoker.java:104)
        at org.jboss.resteasy.client.jaxrs.internal.proxy.ClientProxy.invoke(ClientProxy.java:76)
        at com.sun.proxy.$Proxy31.toRepresentation(Unknown Source)
        at com.example.keycloakaccess2.KeycloakAccess2ApplicationTests.displayUser(KeycloakAccess2ApplicationTests.java:134)

我想知道是否有正确的方法来显示有关用户的信息,甚至显示我们领域中的用户!

感谢您的帮助。

为了让它更显眼,我重复在评论中找到的 Sébastien 的 Blanc 回答。

keycloak adapterclient 依赖项不是同一个版本,因此 Jackson 得到了一些无法映射到 class 的字段(在这种情况下字段 notBefore 在 UserRepresentation class) 中不可用。

因此,在您的 pom.xml 中,确保依赖项相同。例如:

<dependency>
    <groupId>org.keycloak</groupId>
    <artifactId>keycloak-spring-security-adapter</artifactId>
    <version>3.4.0.Final</version>
</dependency>
<dependency>
    <groupId>org.keycloak</groupId>
    <artifactId>keycloak-admin-client</artifactId>
    <version>3.4.0.Final</version>
</dependency>