Kshitiz Sharma
Kshitiz Sharma

Reputation: 792

Unable to get the access token from KeyCloak

I am trying to get the access token from the Keycloak server using postman.

I have been following this link and created the realm, user, role credentials as mentioned in the tutorial.

But when I make the request, it goes on indefinitely, without returning anything.

edit: I tried grant_type as "password" and "client_credentials" as well. But no luck.

enter image description here

Upvotes: 3

Views: 24223

Answers (2)

Subodh Joshi
Subodh Joshi

Reputation: 13492

Here is the right way to get the Token from PostMan

https://<IP ADDRESS>:<PORT>/auth/realms/master/protocol/openid-connect/token

In Body Section select x-www-form-urlencoded

{
   "username": "username",
   "password": "password",
   "client_id": "APP-NAME",
   "grant_type": "password",
   "client_secret": "462fe347-d47f-4365-94ee-6aefff994ef2"
}

For more clear cut solution use like this enter image description here

If you want to from through `curl` command then use below command 
curl -X POST -k -H 'Content-Type: application/x-www-form-urlencoded' -i 'https://<IP>:<PORT>/auth/realms/master/protocol/openid-connect/token' --data 'username=username&password=password&client_id=CLIENT-ID&grant_type=password&client_secret=462fe347-d47f-4365-94ee-6aefff994ef2'

**EDIT **

As discussed with OP in SO chat at last found solution ,OP was creating user with temp password so every-time user login it have to change the password thats why its failing .After OP changed the disable the option its started working enter image description here

Upvotes: 3

Alex P.
Alex P.

Reputation: 3171

In this example you can't use client_credentials because you don't have a client secret here you can read about Client Credentials Grant. It's also used mainly for server to server communication. You should not use password grant but for the examples sake its ok I guess. Password Grant

so this is what I get enter image description here

The question is how have you configured your spring-boot application? Here are the application.properties like mentioned in the tutorial:

keycloak.realm=SpringBootKeycloak
keycloak.auth-server-url=http://localhost:8080/auth/
keycloak.resource=login-app
keycloak.public-client=true

and this is how my SecurityConfig looks like

@Configuration
@EnableWebSecurity
@ComponentScan(
        basePackageClasses = KeycloakSecurityComponents.class,
        excludeFilters = @ComponentScan.Filter(type = FilterType.REGEX, pattern = "org.keycloak.adapters.springsecurity.management.HttpSessionManager"))
public class SecurityConfig extends KeycloakWebSecurityConfigurerAdapter {

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        KeycloakAuthenticationProvider keycloakAuthenticationProvider = keycloakAuthenticationProvider();
        keycloakAuthenticationProvider.setGrantedAuthoritiesMapper(new SimpleAuthorityMapper());
        auth.authenticationProvider(keycloakAuthenticationProvider);
    }

    @Override
    protected SessionAuthenticationStrategy sessionAuthenticationStrategy() {
        return new RegisterSessionAuthenticationStrategy(new SessionRegistryImpl());
    }

    @Bean
    public KeycloakSpringBootConfigResolver KeycloakConfigResolver() {
        return new KeycloakSpringBootConfigResolver();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        super.configure(http);
        http.authorizeRequests()
                .antMatchers("/*")
                .hasRole("user").anyRequest().permitAll();

    }
}

Upvotes: 2

Related Questions