Spring Boot集成JWT时securityFilterChain Bean创建失败求助
问题排查方案
1. 确认OAuth2资源服务器依赖是否存在
BearerTokenResolver属于Spring Security OAuth2资源服务器模块,先检查你的POM文件是否引入了正确依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-oauth2-resource-server</artifactId> </dependency>
如果已经引入,确认Spring Boot版本和该依赖版本兼容——Spring Boot Starter会自动管理依赖版本,尽量避免手动指定版本导致冲突。
2. 核对Spring Boot版本匹配性
Dan Vega的教程大概率基于Spring Boot 3.x,该版本中BearerTokenResolver位于org.springframework.security.oauth2.server.resource.web包;如果你的项目用的是Spring Boot 2.x,这个类的包路径会不同(比如org.springframework.security.oauth2.server.resource.web.authentication),甚至部分旧版本中没有这个类。
- 若坚持用2.x:调整代码中该类的导入路径,改用对应版本的实现类(比如
DefaultBearerTokenResolver) - 若要匹配教程:升级到Spring Boot 3.x(注意3.x基于Jakarta EE,和2.x的javax包不兼容,需同步调整相关导入)
3. 检查SecurityConfig中的类引用
确认SecurityConfig里对BearerTokenResolver的导入路径和当前Spring Boot版本匹配,比如:
- Spring Boot 3.x导入:
import org.springframework.security.oauth2.server.resource.web.BearerTokenResolver;
- Spring Boot 2.x导入:
import org.springframework.security.oauth2.server.resource.web.authentication.DefaultBearerTokenResolver;
4. 清理项目缓存并重新构建
依赖缓存异常也可能导致类找不到,执行以下命令清理后重新构建:
mvn clean install
或者在IDE中执行缓存清理操作(比如IDEA的File -> Invalidate Caches...)后重启IDE。
5. 验证RsaKeyProperties与密钥配置
确保RsaKeyProperties类配置正确,比如:
import org.springframework.boot.context.properties.ConfigurationProperties; @ConfigurationProperties(prefix = "rsa") public record RsaKeyProperties(String publicKey, String privateKey) {}
同时在配置文件(application.properties/yaml)中正确指定密钥路径或内容:
rsa.public-key=classpath:public.key rsa.private-key=classpath:private.key
内容的提问来源于stack exchange,提问作者Cezar P
相关产品推荐
相关产品推荐

