Spring Boot OAuth2报错:找不到GlobalAuthenticationConfigurerAdapter类求助
嘿,我来帮你搞定这个FileNotFoundException的问题!你遇到的找不到GlobalAuthenticationConfigurerAdapter.class的情况,大概率是依赖版本不兼容或者核心依赖缺失导致的,下面给你一步步的解决方案:
1. 升级OAuth2依赖版本
你用的spring-security-oauth2:2.0.2.RELEASE版本过于老旧,和Spring Boot默认集成的Spring Security版本很容易出现兼容性冲突。建议升级到同分支的最新稳定版2.0.18.RELEASE,这个版本和Spring Boot 1.x系列适配得更顺畅。
2. 补全核心依赖
GlobalAuthenticationConfigurerAdapter类属于spring-security-config模块,虽然OAuth2依赖会间接引用它,但有时候传递依赖会出现异常。最稳妥的方式是直接引入spring-boot-starter-security,它会自动配齐Spring Security的核心模块(包括spring-security-config)。
修改后的Gradle依赖配置如下:
compile('org.springframework.boot:spring-boot-starter-web') compile('org.springframework.boot:spring-boot-starter-security') // 新增该starter,补全核心依赖 compile group: 'org.springframework.security.oauth', name: 'spring-security-oauth2', version: '2.0.18.RELEASE' testCompile('org.springframework.boot:spring-boot-starter-test')
3. 清理缓存并重新构建项目
调整依赖后,记得清理本地构建工具的缓存,避免旧依赖干扰:
- 若使用Maven,执行命令:
mvn clean install -U - 若使用Gradle,执行命令:
gradle clean build --refresh-dependencies
之后重启IDE并重新导入项目,确保新依赖被正确识别加载。
4. 验证类的引用路径
最后确认代码中的import语句是否正确,这个类的完整包路径为:
import org.springframework.security.config.annotation.authentication.configurers.GlobalAuthenticationConfigurerAdapter;
如果做完以上步骤仍未解决问题,请检查你的Spring Boot版本:如果是Spring Boot 2.x,spring-security-oauth2的2.0.x版本完全不兼容,需要改用Spring Security 5.x自带的OAuth2支持,或者引入spring-security-oauth2-autoconfigure模块进行适配。
内容的提问来源于stack exchange,提问作者gstackoverflow

