Keycloak admin client get 404 HTTP error when getting access token

210 Views Asked by At

i'm using keycloak with quarkus and in my integration test i'm using dev service to build keycloak server. My problem is taht when i want to get access token with keycloak admin client i get 404 HTTPand at the same time when i'm using postman i get the token.

My class:

package com.smartech.shopping.backend.bank.controller;

import com.smartech.shopping.backend.bank.controller.mapper.ErrorMapper;
import com.smartech.shopping.backend.bank.controller.rest.AuthenticationApi;
import com.smartech.shopping.backend.bank.entity.CredentialDto;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.ws.rs.core.Response;
import lombok.RequiredArgsConstructor;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.keycloak.OAuth2Constants;
import org.keycloak.admin.client.Keycloak;
import org.keycloak.admin.client.KeycloakBuilder;
import org.mapstruct.factory.Mappers;

@ApplicationScoped
@RequiredArgsConstructor
public class AuthenticationApiImpl implements AuthenticationApi {

    @ConfigProperty(name = "quarkus.oidc.auth-server-url")
    String serverUrl;
    @ConfigProperty(name = "quarkus.keycloak.realm-name")
    String realm;
    private final ErrorMapper errorMapper = Mappers.getMapper(ErrorMapper.class);

    @Override
    public Response authentication(CredentialDto credentialDto) {
        try(Keycloak keycloak =
                    KeycloakBuilder.builder()
                            .clientId(credentialDto.getClientId())
                            .clientSecret(credentialDto.getClientSecret())
                            .realm(realm)
                            .serverUrl(serverUrl)
                            .grantType(OAuth2Constants.CLIENT_CREDENTIALS)
                            .build()){
            return Response
                    .ok(keycloak.tokenManager().getAccessToken())
                    .build();
        }
    }
}

My application.properties

##----------TEST----------------##
%test.quarkus.keycloak.admin-client.enabled=true
%test.quarkus.keycloak.admin-client.server-url=http://localhost:8080
%test.quarkus.keycloak.admin-client.realm=quarkus
%test.quarkus.keycloak.admin-client.client-id=quarkus
%test.quarkus.keycloak.admin-client.client-secret=secret
%test.quarkus.keycloak.devservices.enabled=true
%test.quarkus.oidc.credentials.secret=iJZaGcPoHhkccbs9mLo4Mhu1w0TmamZl
%test.quarkus.keycloak.devservices.realm-path=realms/shopping-realm.json
%test.quarkus.keycloak.realm-name=quarkus

##----------PROD----------------##
%prod.quarkus.oidc.token-path=/protocol/openid-connect/token
%prod.quarkus.oidc.auth-server-url=http://localhost:8080/auth/realms/shopping-realm

And my pom.xml

    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-resteasy-jackson</artifactId>
    </dependency>
    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-oidc</artifactId>
    </dependency>
    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-keycloak-authorization</artifactId>
    </dependency>
    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-keycloak-admin-client</artifactId>
    </dependency>

In the tutorial they use quarkus-resteasy-reactive-jackson and i'm using quarkus-resteasy-jackson. I don't know if it,s the problem.

Ùthank you!

1

There are 1 best solutions below

0
On

The problem was the properties quarkus.oidc.auth-server-url and quarkus.keycloak.realm-name. I set them correctly and it is working find now