jhipster/spring keycloak integration - setting custom redirect_uri

Viewed 2414

I have a spring / jhipster application, with a button to login to keycloak. When I click the login button, it brings me to keycloak, and the URL would look something like

https://keycloak.mydomain.com/auth/realms/my-realm/protocol/openid-connect/auth?response_type=code&client_id=myclient&redirect_uri=http://localhost:8080/login/oauth2/code/oidc

So in the above, I want to change the redirect_uri=http://localhost:8080/login/oauth2/code/oidc to something else.

So I added the below code in src/main/java/../config/AuthorizationServerConfig.java:

@Configuration
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

    @Bean
    PasswordEncoder passwordEncoder() {
        return PasswordEncoderFactories.createDelegatingPasswordEncoder();
    }

    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients
                .inMemory()
                .withClient("myclient")
                .secret(passwordEncoder().encode("mysecret"))
                .scopes("resource:read")
                .authorizedGrantTypes("authorization_code")
                .redirectUris("https://new-url-I-want.com");
    }

}

But even with the above change, the redirect_uri doesn't change at all. Anyone know why this is?


And more information on why I made those changes:

With OIDC's authorization code flow, the service provider (in this case my website) provides the identity provider (Keycloak) with the URI to redirect the user back to after successful authentication. However, per the Spring docs, "With the default configuration, while the Authorization Code Flow is technically allowed, it is not completely configured": https://docs.spring.io/spring-security-oauth2-boot/docs/current/reference/html5/#oauth2-boot-authorization-server-authorization-code-grant

The docs go on to state that "OAuth2 Boot does not support configuring a redirect URI as a property — say, alongside client-id and client-secret." so,

"To add a redirect URI, you need to specify the client by using either InMemoryClientDetailsService or JdbcClientDetailsService": https://docs.spring.io/spring-security-oauth2-boot/docs/current/reference/html5/#registering-a-redirect-uri-with-the-client

2 Answers

add the follwing lines inside the method SecurityConfiguration.configure

       .and()
        .oauth2Login()
            .defaultSuccessUrl("/url_to_redirect")

That is:

   public class SecurityConfiguration extends WebSecurityConfigurerAdapter { ...

@Override
public void configure(WebSecurity web) { ... }

@Override
public void configure(HttpSecurity http) throws Exception {
    ...
      .and()
        .authorizeRequests()
        .antMatchers("/api/auth-info").permitAll()
        .antMatchers("/api/**").authenticated()
        .antMatchers("/management/health").permitAll()
        .antMatchers("/management/info").permitAll()
        .antMatchers("/management/prometheus").permitAll()
        .antMatchers("/management/**").hasAuthority(AuthoritiesConstants.ADMIN)
    .and()
        .oauth2Login()
          .defaultSuccessUrl("/url_to_redirect")  // <--- HERE
    
}

You can add another redirect uri by adding the following property and src/main/resources/config/application.yml

  security:
    oauth2:
      client:
        provider:
          oidc:
            issuer-uri: http://localhost:9080/auth/realms/jhipster
        registration:
          oidc:
            client-id: web_app
            client-secret: web_app
            redirect-uri: http://localhost:8080/my-custom-redirect

Furthermore you have to configure the custom redirect uri for the oauth2 login in SecurityConfiguration as follows

...
.and()
   .oauth2Login()
       .redirectionEndpoint()
        .baseUri("/my-custom-redirect")
    .and()
.and()
    .oauth2ResourceServer()
...
Related