Spring Boot Maven通用配置库无法使用的问题求助
问题解决方案
问题1:Spring OAuth类ClassNotFoundException
原因
Jar库中依赖的Spring OAuth相关类(如ResourceServerConfigurerAdapter)未被加载到业务项目的classpath中。通常是因为库中这类依赖的Maven scope被设置为provided,或者业务项目未通过依赖传递获取到对应包。
解决步骤
- 检查库的
pom.xml,确保Spring OAuth相关依赖的scope为compile(默认即为compile,无需手动设置provided):<dependency> <groupId>org.springframework.security.oauth</groupId> <artifactId>spring-security-oauth2</artifactId> <version>对应版本号</version> </dependency> - 若业务项目是Spring Boot应用,优先使用Spring Boot Starter管理OAuth依赖,避免版本冲突:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-oauth2-resource-server</artifactId> </dependency> - 执行
mvn dependency:tree检查业务项目的依赖树,确认OAuth相关包已被正确引入。
问题2:缺少ServletWebServerFactory Bean
原因
ReferenceSecurityConfig中的配置依赖Web环境(ResourceServerConfigurerAdapter是Web应用专属的安全配置类),但业务项目可能是非Web类型的Spring Boot应用(如批处理、纯后台服务),此时Spring Boot不会自动创建ServletWebServerFactory Bean。
解决步骤
- 给
ReferenceSecurityConfig添加条件注解,限制其仅在Web环境下生效:@Configuration @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET) public class ReferenceSecurityConfig extends ResourceServerConfigurerAdapter { // 你的配置代码 } - 若业务项目确实需要Web环境,在其
pom.xml中添加Spring Boot Web Starter依赖:<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> - 调整自定义注解
@EnableCustomSecurityConfig,同步添加条件判断:@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Import(ReferenceSecurityConfig.class) @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET) public @interface EnableCustomSecurityConfig { }
内容的提问来源于stack exchange,提问作者Satish Patro
相关产品推荐
相关产品推荐

