启动SpringBoot项目时抛出如下异常:

Caused by: java.lang.ClassNotFoundException: org.keycloak.adapters.springsecurity.KeycloakSecurityComponents
	at java.net.URLClassLoader.findClass(URLClassLoader.java:382) ~[na:1.8.0_271]
	at java.lang.ClassLoader.loadClass(ClassLoader.java:418) ~[na:1.8.0_271]
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355) ~[na:1.8.0_271]
	at java.lang.ClassLoader.loadClass(ClassLoader.java:351) ~[na:1.8.0_271]
	at java.lang.Class.forName0(Native Method) ~[na:1.8.0_271]
	at java.lang.Class.forName(Class.java:348) ~[na:1.8.0_271]

导致该异常的主要原因是keycloak相关依赖没有引入,在pom文件中引入以下依赖:

<dependency>
      <groupId>org.keycloak</groupId>
      <artifactId>keycloak-spring-boot-starter</artifactId>
      <version>10.0.0</version>
</dependency>

添加上述依赖之后,相关异常即可解决。如果还有其他异常出现,同样添加相关依赖即可。比如在我的项目中相关一组依赖配置如下:

<dependency>
    <groupId>org.keycloak</groupId>
    <artifactId>keycloak-spring-boot-starter</artifactId>
    <version>10.0.0</version>
</dependency>
<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-core</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-config</artifactId>
</dependency>

如果上述方法依旧无法解决,可尝试配置初始化话对应的类:

@Configuration
public class KeycloakConfig {

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


java.lang.ClassNotFoundException: org.keycloak.adapters.springsecurity.KeycloakSecurityComponents异常插图

关注公众号:程序新视界,一个让你软实力、硬技术同步提升的平台

除非注明,否则均为程序新视界原创文章,转载必须以链接形式标明本文链接

本文链接:https://choupangxia.com/2020/12/10/springboot-keycloak/