Keycloak 通过 API 和 409 错误向用户添加属性

Keycloak add attributes to user via API and 409 error

我正在尝试向位于 Keycloak:10.0.1.

的现有用户添加属性
  @Override
public void addAttribute(String userId, String value) throws UserDoesNotExistException {
    log.info("Trying add attribute to user: " + userId);
    Keycloak keycloak = getClient();
    RealmResource realmResource = keycloak.realm(configuration.getKeycloakRealm());
    final UserResource userResource = realmResource.users().get(userId);
    final UserRepresentation user = userResource.toRepresentation();
    if (user.getAttributes() == null) {
        user.setAttributes(new HashMap<>());
    }
    user.getAttributes().put(Const.SOME_ATTRIBUTE, Collections.singletonList(value));
    userResource.update(user);
    log.info("Attribute to user: " + userId + " added successfully");
}

我收到错误 HTTP-409:

Caused by: javax.ws.rs.ClientErrorException: HTTP 409 Conflict
    at org.jboss.resteasy.resteasy-jaxrs@3.6.3.Final//org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.handleErrorStatus(ClientInvocation.java:237)
    at org.jboss.resteasy.resteasy-jaxrs@3.6.3.Final//org.jboss.resteasy.client.jaxrs.internal.proxy.extractors.DefaultEntityExtractorFactory.extractEntity(DefaultEntityExtractorFactory.java:50)
    at org.jboss.resteasy.resteasy-jaxrs@3.6.3.Final//org.jboss.resteasy.client.jaxrs.internal.proxy.ClientInvoker.invokeSync(ClientInvoker.java:150)
    at org.jboss.resteasy.resteasy-jaxrs@3.6.3.Final//org.jboss.resteasy.client.jaxrs.internal.proxy.ClientInvoker.invoke(ClientInvoker.java:112)
    at org.jboss.resteasy.resteasy-jaxrs@3.6.3.Final//org.jboss.resteasy.client.jaxrs.internal.proxy.ClientProxy.invoke(ClientProxy.java:76)

解决方案:属性值太长。你必须在 keycloak 数据库中更改大小。

ALTER TABLE user_attribute ALTER COLUMN value TYPE TEXT;

默认类型是VARCHAR(255)