1

I'm trying to setup java spring boot with keycloak but it can't find this file: org.keycloak.adapters.springboot.KeycloakSpringBootConfigResolver

Here's my code:

src/main/java/com/example/tutorial/config/KeyCloakConfiguration.java

package com.example.tutorial.config;

import org.keycloak.adapters.KeycloakConfigResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.keycloak.adapters.springboot.KeycloakSpringBootConfigResolver;


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

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.10</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>tutorial</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>tutorial</name>
    <description>Tutorial project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
        <spring-security.version>5.6.1</spring-security.version>
        <spring.version>5.3.9</spring.version>
    </properties>
    <dependencies>
        <!-- Spring Security dependencies -->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-core</artifactId>
            <version>${spring-security.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>${spring-security.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>${spring-security.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- Other dependencies -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.22</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.keycloak</groupId>
            <artifactId>keycloak-core</artifactId>
            <version>16.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.keycloak</groupId>
            <artifactId>keycloak-common</artifactId>
            <version>16.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.keycloak</groupId>
            <artifactId>keycloak-spring-boot-starter</artifactId>
            <version>16.1.0</version>
            <exclusions>
                <exclusion>
                    <groupId>org.keycloak</groupId>
                    <artifactId>keycloak-spring-boot-2-adapter</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.keycloak</groupId>
            <artifactId>keycloak-admin-client</artifactId>
            <version>16.1.0</version>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.keycloak.bom</groupId>
                <artifactId>keycloak-adapter-bom</artifactId>
                <version>16.1.0</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

I've tried to run mvn clean install -U -DskipTests and try to refresh dependency on IntelliJ by right-click on my project and select "Maven" > "Reload Project".

But it doesn't resolve the problem.

Please help, thanks.

fudu
  • 617
  • 1
  • 10
  • 19
  • Keycloak adapters where deprecated early 2022. Don't use it. https://stackoverflow.com/questions/74571191/use-keycloak-spring-adapter-with-spring-boot-3/74572732#74572732 – ch4mp Apr 13 '23 at 07:22

1 Answers1

1

The specific jar containing the missing class (org.keycloak.adapters.springboot.KeycloakSpringBootConfigResolver) - org.keycloak:keycloak-spring-boot-2-adapter has been excluded from org.keycloak:keycloak-spring-boot-starter.

Remove this exclusion:

        <dependency>
            <groupId>org.keycloak</groupId>
            <artifactId>keycloak-spring-boot-starter</artifactId>
            <exclusions>
<!--                 <exclusion> -->
<!--                     <groupId>org.keycloak</groupId> -->
<!--                     <artifactId>keycloak-spring-boot-2-adapter</artifactId> -->
<!--                 </exclusion> -->
            </exclusions>
        </dependency>

It may have been excluded for a good reason but some version is required.

John Williams
  • 4,252
  • 2
  • 9
  • 18
  • That reason could be an attempt to migrate to Spring Boot 3 / Security 6 with which this adapters are not compatible (uses `WebSecurityConfigurerAdapter` which is deprecated in Boot `2.7.x` and was removed in `3.0.0`) Again, it was deprecated early 2022, it is time to completely remove `keycloak-spring-boot-starter`. See [this other answer](https://stackoverflow.com/questions/74571191/use-keycloak-spring-adapter-with-spring-boot-3/74572732#74572732) for alternatives – ch4mp Apr 14 '23 at 16:26