4

firstly i'd Like to apologise for typos, writing thing on a tablet pc on a plain.

I'm building a springboot app secured by a Keycloak service. I need my end-user to be able to create an keycloak User via my Frontend so I've tried to build it via the keycloak admin dependency

<dependency>
            <groupId>org.keycloak</groupId>
            <artifactId>keycloak-admin-client</artifactId>
            <version>11.0.2</version>
        </dependency>

I'm almost done but getting a nasty HTTP 409 on

    private void createClientRole(String clientRole, Keycloak kc) {
        RoleRepresentation clientRoleRepresentation = new RoleRepresentation();
        clientRoleRepresentation.setName(clientRole);
        clientRoleRepresentation.setClientRole(true);
        kc.realm(this.realm).clients().findByClientId(clientId).forEach(clientRepresentation ->
kc.realm(this.realm).clients().get(clientRepresentation.getId()).roles().create(clientRoleRepresentation)
        );//<== Here
    }

There is no further explanation, don't know what's happening there. Any idea?

Kind regards, Rosario

R. Polito
  • 544
  • 6
  • 21

2 Answers2

6

HTTP 409 response means conflict:

The request could not be completed due to a conflict with the current state of the target resource. This code is used in situations where the user might be able to resolve the conflict and resubmit the request.

You should check Keycloak server logs for more details. I will bet that role with that particular name already exists and that is a root cause for 409 response. It is a blind guess - you didn't posted any Keycloak server error logs, so don't blame me if I'm wrong in this point.

Jan Garaj
  • 25,598
  • 3
  • 38
  • 59
  • You hit the nail on the head! I thought I just "assign" a role that's already created there... Do you have an example how to to that or a link to documentation? Can't find anything online :(( – R. Polito Sep 14 '20 at 05:03
  • https://www.keycloak.org/docs-api/11.0/rest-api/index.html or check Keycloak UI is working to see real example how REST API can be used. – Jan Garaj Sep 14 '20 at 06:32
  • I'm using the Java api, not the rest api. Haven't found the correct endpoint in the api doc yet but try to reconstruct it via admin console. If you have an example how to do it via Java api it would be nice! – R. Polito Sep 15 '20 at 18:23
  • If you don't have access to the server log you are lost. There can be many reasons for a conflict and you need to guess which of your parameters was wrong :-/ – Datz Dec 07 '22 at 16:25
0

I faced with same, in my case it was related with not unique user's email. From keycloak logs: Caused by:

org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "uk_dykn684sl8up1crfei6eckhd7"
  Detail: Key (realm_id, email_constraint)=(<REALM>, <EMAIL>) already exists.
    at org.postgresql.jdbc@42.2.5//org.postgresql.core.v3.QueryExecutorImpl.receiveErrorRespon
Procrastinator
  • 2,526
  • 30
  • 27
  • 36