0

I'm encountering an error when trying to consume an authenticated service. Here is the full error:

java.lang.RuntimeException: Must set 'realm' in config
    at org.keycloak.adapters.KeycloakDeploymentBuilder.internalBuild(KeycloakDeploymentBuilder.java:57) ~[keycloak-adapter-core-12.0.1.jar:12.0.1]
    at org.keycloak.adapters.KeycloakDeploymentBuilder.build(KeycloakDeploymentBuilder.java:202) ~[keycloak-adapter-core-12.0.1.jar:12.0.1]
    at org.keycloak.adapters.springboot.KeycloakSpringBootConfigResolver.resolve(KeycloakSpringBootConfigResolver.java:41) ~[keycloak-spring-boot-adapter-core-12.0.1.jar:12.0.1]
    at org.keycloak.adapters.springsecurity.config.KeycloakSpringConfigResolverWrapper.resolve(KeycloakSpringConfigResolverWrapper.java:40) ~[keycloak-spring-security-adapter-12.0.1.jar:12.0.1]
    at org.keycloak.adapters.AdapterDeploymentContext.resolveDeployment(AdapterDeploymentContext.java:89) ~[keycloak-adapter-core-12.0.1.jar:12.0.1]

I generate the access token and then try to consume the service with the token, but Spring generates an error saying to set the 'realm' property. Here are my properties and some of the code:

Properties

server.port=8081

spring.security.oauth2.client.registration.client.client-id=client
spring.security.oauth2.client.registration.client.authorization-grant-type=authorization_code
spring.security.oauth2.client.registration.client.scope=openid,email,profile,roles
spring.security.oauth2.client.registration.client.provider=keycloak

spring.security.oauth2.client.provider.keycloak.authorization-uri=http://localhost:8085/realms/user/protocol/openid-connect/auth
spring.security.oauth2.client.provider.keycloak.token-uri=http://localhost:8085/realms/user/protocol/openid-connect/token
spring.security.oauth2.client.provider.keycloak.user-info-uri=http://localhost:8085/realms/user/protocol/openid-connect/userinfo
spring.security.oauth2.client.provider.keycloak.jwk-set-uri=http://localhost:8085/realms/user/protocol/openid-connect/certs

Security configuration code:

package com.djamware.oauthresource.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.cors()
            .and()
            .authorizeRequests()
            .antMatchers(HttpMethod.GET, "/api/board")
            .hasAuthority("user")
            .anyRequest()
            .authenticated()
            .and()
            .oauth2ResourceServer()
            .jwt();
    }
}

Any help in resolving this issue would be appreciated. Thank you.

So far, I have tried generating an access token and then using that token to consume the authenticated service. My expectation was that the service would be consumed successfully as the token should authenticate the request. However, I ran into the java.lang.RuntimeException: Must set 'realm' in config error

Gastón Schabas
  • 2,153
  • 1
  • 10
  • 17
  • You are using the very deprecated Keycloak adapters for Spring (exception in `org.keycloak.adapters...`). It was abandoned more than a year ago and is incompatible with Spring Security 6 (Spring Boot 3), plus you're not even providing the Keycloak properties for these adapters. See [this other answer](https://stackoverflow.com/a/74572732/619830) for an alternative to Keycloak adapters (which works with Spring Security 6 out of the box and Spring Security 5 with little adaptation) – ch4mp Jun 24 '23 at 15:35

0 Answers0