无法导入EnableOAuth2Sso注解,求助Spring Security OAuth2集成问题
解决@EnableOAuth2Sso注解找不到的问题
我之前也踩过这个坑!问题出在你的依赖配置上——@EnableOAuth2Sso注解并不在你当前引入的spring-security-oauth2核心依赖里,它属于Spring Boot专门的OAuth2自动配置模块。
具体解决方案:
- 补充正确的依赖:你需要引入
spring-boot-starter-oauth2-client(这个starter已经封装了OAuth2客户端和SSO所需的所有依赖,包括自动配置模块),推荐用这个starter来避免版本兼容问题。 - 修改后的依赖配置:
dependencies { compile('org.springframework.boot:spring-boot-starter-web') testCompile('org.springframework.boot:spring-boot-starter-test') compile('org.springframework.boot:spring-boot-starter-security') // 替换原有的spring-security-oauth2依赖,或直接添加该starter compile('org.springframework.boot:spring-boot-starter-oauth2-client') compile('org.springframework.boot:spring-boot-starter-thymeleaf') compile('org.thymeleaf.extra...') // 保留你原本的thymeleaf扩展依赖 }
- 正确的注解导入路径:依赖添加完成后,就能导入正确的注解了:
import org.springframework.boot.autoconfigure.security.oauth2.client.EnableOAuth2Sso;
额外提醒:
确保你的Spring Boot版本和OAuth2相关依赖版本保持一致——使用starter的话,版本会自动和Spring Boot的版本对齐,不用手动指定,能有效避免版本冲突问题。
内容的提问来源于stack exchange,提问作者gstackoverflow
相关产品推荐
相关产品推荐

