在我添加 spring-security-oauth2
到我的 pom.xml 之后:
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
<version>2.2.0.RELEASE</version>
</dependency>
我开始得到
Exception in thread "main" java.lang.IllegalArgumentException: Cannot instantiate interface org.springframework.context.ApplicationListener : org.springframework.boot.context.logging.ClasspathLoggingApplicationListener
at org.springframework.boot.SpringApplication.createSpringFactoriesInstances(SpringApplication.java:439)
at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:418)
at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:409)
at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:268)
at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:247)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1245)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1233)
运行我的 @SpringBootApplication
之后。请注意,我使用的是 Spring Boot 2.0.0.M5,我认为这可能是问题的根源(wrt 版本)。
这是我正在使用的整个 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>server</artifactId>
<groupId>mahlzeit</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>api</artifactId>
<properties>
<spring.boot.version>2.0.0.M5</spring.boot.version>
</properties>
<dependencies>
<!-- Spring Framework Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring.boot.version}</version>
<scope>test</scope>
</dependency>
<!-- Spring Framework -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-jwt</artifactId>
<version>1.0.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
<version>2.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4.1208-jdbc42-atlassian-hosted</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<!-- Required since this is currently using Spring RC version -->
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/libs-milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
我怀疑这可能与版本冲突有关——stackoverflow 上的大多数答案都是这样——但我无法弄清楚我在这里遇到的实际问题。
Spring Boot 2.0.0.M5 状态的 发行说明:
OAuth 2.0 支持
Spring Security OAuth 项目的功能正在迁移到核心 Spring Security。已添加 OAuth 2.0 客户端支持,并将在适当时候迁移其他功能。
如果您依赖尚未迁移的 Spring Security OAuth 功能,则需要添加
org.springframework.security.oauth:spring-security-oauth2
并手动配置。如果您只需要 OAuth 2.0 客户端支持,您可以使用 Spring Boot 2.0 提供的自动配置。我们还将继续支持 Spring Boot 1.5,以便旧应用程序可以继续使用它,直到提供升级路径。原文由 Stefan Falk 发布,翻译遵循 CC BY-SA 4.0 许可协议
解决了示例 pom 的问题后,我现在可以看到失败的完整堆栈跟踪:
问题的根本原因是
org.springframework.context.event.GenericApplicationListener
不在类路径中。此类是spring-context
模块的一部分,是 Spring Framework 4.2 中的新类。查看 pom,您没有使用 Boot 的依赖项管理(通过导入
spring-boot-dependencies
bom 或使用spring-boot-starter-parent
作为项目的父项。这意味着任何传递依赖项的版本没有被管理。这导致了这里的问题,因为您的项目正在使用spring-context
的 4.0.9.RELEASE 通过spring-security-oauth2
--- 传递。我强烈建议您使用 Spring Boot 的依赖管理。如果您不想这样做或由于某种未说明的原因不能这样做,则必须手动确保所有传递依赖项都具有受支持的版本。