Generate a keycloak register link in a spring boot app

Viewed 1907

I'm securing a web app build with spring boot with the keycloak-spring-boot-starter which creates me /sso/login and /sso/logout urls. But I also would like to provide a link to the keycloak registration page - but there is no /sso/register link.

How do I implement this link? I could not find the place in the keycloak-adapter sources where the login/logout resource is implemented so a point to that would maybe enough.

1 Answers

You can create your own register link like below code ;

        String nonce = UUID.randomUUID().toString();
    MessageDigest md = null;
    try {
        md = MessageDigest.getInstance("SHA-256");
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
    String input = nonce + clientId;
    byte[] check = md.digest(input.getBytes(StandardCharsets.UTF_8));
    String hash = Base64Url.encode(check);

    return KeycloakUriBuilder.fromUri(keycloakEndpoint)
                .path("/realms/{realm-name}/protocol/openid-connect/registrations")
                .queryParam("response_type", "code") // Autherization Code Flow
                .queryParam("scope", "openid") // Add additional scopes if needed
                .queryParam("nonce", nonce)
                .queryParam("hash", hash)
                .queryParam("client_id", clientId)
                .queryParam("redirect_uri", "http://sample.javaspringboot.com/" + "redirect/" + redirectUrl).build(realm).toString();
Related