amara amara - 1 year ago 266
reST (reStructuredText) Question

Generate token with Keycloak

I need to call a method of rest web services that requires authentication.
I have one json file keycloak.json and another comonApi.json file to generate the client with plugin Swagger.

But I do not know what to do with the other file keyclooak.json.

Please how I can generate a token with keycloak.json file for calling methods?

Answer Source

Solution for my question :

import org.keycloak.admin.client.Keycloak;

import org.keycloak.representations.AccessTokenResponse;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.stereotype.Component;


public class KeycloakUtils {

   private String realm;

   private String clientId;

   private String serverUrl;

   private String userName;

   private String password;

   private String clientSecret;

   private Keycloak keycloak;

   public Keycloak getInstance() {
         if (keycloak == null) {
                keycloak = Keycloak.getInstance(serverUrl, realm, userName, password, clientId, clientSecret);
         return keycloak;

   public AccessTokenResponse getAccessToken() {
         return getInstance().tokenManager().grantToken();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download