Spring Boot集成OAuth库遇类无法解析问题求助
问题
在Spring Boot中集成OAuth库时,出现以下类无法解析的报错:
ProtectedResourceDetails cannot be resolved to a type
OAuthConsumerSupport cannot be resolved to a type
BaseProtectedResourceDetails cannot be resolved to a type
当前pom.xml依赖配置如下:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.security.oauth</groupId> <artifactId>spring-security-oauth2</artifactId> <version>2.5.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-oauth2-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-oauth2-resource-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-test</artifactId> <scope>test</scope> </dependency>
需要引入正确依赖以使用以下类:
OAuthConsumerSupport oAuthConsumerSupport = new CoreOAuthConsumerSupport(); ProtectedResourceDetails is24ClientKeyDetails = createIs24ClientKeyDetails(); BaseProtectedResourceDetails protectedResourceDetails = new BaseProtectedResourceDetails();
解决方案
类无法解析的核心原因是混用了新旧两套Spring OAuth体系,依赖配置存在冲突:
明确类归属:
ProtectedResourceDetails、OAuthConsumerSupport、BaseProtectedResourceDetails都属于旧版Spring Security OAuth项目(org.springframework.security.oauth组),和Spring Boot官方提供的新版OAuth2 Starter(spring-boot-starter-oauth2-client/resource-server)不属于同一体系,二者不兼容。调整依赖配置:
- 移除冲突的新版OAuth Starter依赖:
spring-boot-starter-oauth2-client和spring-boot-starter-oauth2-resource-server。 - 建议通过Spring Boot的依赖管理自动控制
spring-security-oauth2版本,避免手动指定版本号引发的版本不匹配问题。
- 移除冲突的新版OAuth Starter依赖:
调整后的pom.xml依赖示例:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <!-- 仅保留旧版Spring Security OAuth2依赖 --> <dependency> <groupId>org.springframework.security.oauth</groupId> <artifactId>spring-security-oauth2</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-test</artifactId> <scope>test</scope> </dependency>
- 额外提示:旧版Spring Security OAuth项目已停止维护,若为新项目建议迁移至Spring官方推荐的新版OAuth2体系,不过这意味着需要重构代码,用
RegisteredClient、OAuth2AuthorizedClient等新版API替代旧类。
内容的提问来源于stack exchange,提问作者Captai-N
相关产品推荐
相关产品推荐

