开心一刻
2021,朋友某一次核酸检测,跟我聊天
朋友:今天我们小区做核算,队长死了
我:卧槽,过劳死吗
朋友:?????
我:啊?
朋友:队 长死了,队伍很长!
我:哈哈哈,我以为做核算的队长死了呢
背景介绍
项目基于 springboot2.5.2 实现的,用 springfox-swagger2 生成与前端对接的 API 文档;pom.xml 中依赖如下- <parent>
- <groupId>org.springframework.boot</groupId>
- spring-boot-starter-parent</artifactId>
- <version>2.5.2</version>
- </parent>
- <properties>
- <maven.compiler.source>8</maven.compiler.source>
- <maven.compiler.target>8</maven.compiler.target>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- </properties>
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- spring-boot-starter-web</artifactId>
- </dependency>
- <dependency>
- <groupId>io.springfox</groupId>
- springfox-boot-starter</artifactId>
- <version>3.0.0</version>
- </dependency>
- </dependencies>
复制代码 启动服务后,就可以访问 Swagger UI
http://localhost:8080/swagger-ui/index.html
前端同事就可以访问这个来对接接口了,后端省去了写接口文档的工作,一切都是那么美好!可突然有一天,安全部门发来报告,说服务存在很多安全漏洞
CVE-2023-20860、CVE-2022-45143、CVE-2023-46589、...
让我们根据报告中的建议进行修复,然后就开始了我的踩坑之旅!
springboot 与 springfox 兼容问题
粗略看了一眼,将 spring-boot 升级,可以解决很多漏洞,既然要升,那就升到可升的最高版本;因为是基于 JDK8,所以 spring-boot 最高能升级到 2.7.8。那就升嘛,不要怂就是干!- <parent>
- <groupId>org.springframework.boot</groupId>
- spring-boot-starter-parent</artifactId>
- <version>2.7.18</version>
- </parent>
复制代码 启动服务,第一个坑来了:NullPointerException- 2025-05-30 21:13:42.264|ERROR|main|818|o.s.boot.SpringApplication :Application run failed
- org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException
- at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:182)
- at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:54)
- at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:357)
- at java.lang.Iterable.forEach(Iterable.java:75)
- at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:156)
- at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:124)
- at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:946)
- at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:594)
- at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147)
- at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:732)
- at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:409)
- at org.springframework.boot.SpringApplication.run(SpringApplication.java:308)
- at org.springframework.boot.SpringApplication.run(SpringApplication.java:1300)
- at org.springframework.boot.SpringApplication.run(SpringApplication.java:1289)
- at com.qsl.Application.main(Application.java:16)
- Caused by: java.lang.NullPointerException: null
- at springfox.documentation.spring.web.WebMvcPatternsRequestConditionWrapper.getPatterns(WebMvcPatternsRequestConditionWrapper.java:56)
- at springfox.documentation.RequestHandler.sortedPaths(RequestHandler.java:113)
- at springfox.documentation.spi.service.contexts.Orderings.lambda$byPatternsCondition$3(Orderings.java:89)
- at java.util.Comparator.lambda$comparing$77a9974f$1(Comparator.java:469)
- at java.util.TimSort.countRunAndMakeAscending(TimSort.java:355)
- at java.util.TimSort.sort(TimSort.java:220)
- at java.util.Arrays.sort(Arrays.java:1512)
- at java.util.ArrayList.sort(ArrayList.java:1454)
- at java.util.stream.SortedOps$RefSortingSink.end(SortedOps.java:387)
- at java.util.stream.Sink$ChainedReference.end(Sink.java:258)
- at java.util.stream.Sink$ChainedReference.end(Sink.java:258)
- at java.util.stream.Sink$ChainedReference.end(Sink.java:258)
- at java.util.stream.Sink$ChainedReference.end(Sink.java:258)
- at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:482)
- at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
- at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
- at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
- at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
- at springfox.documentation.spring.web.plugins.WebMvcRequestHandlerProvider.requestHandlers(WebMvcRequestHandlerProvider.java:81)
- at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
- at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1374)
- at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
- at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
- at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
- at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
- at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
- at springfox.documentation.spring.web.plugins.AbstractDocumentationPluginsBootstrapper.withDefaults(AbstractDocumentationPluginsBootstrapper.java:107)
- at springfox.documentation.spring.web.plugins.AbstractDocumentationPluginsBootstrapper.buildContext(AbstractDocumentationPluginsBootstrapper.java:91)
- at springfox.documentation.spring.web.plugins.AbstractDocumentationPluginsBootstrapper.bootstrapDocumentationPlugins(AbstractDocumentationPluginsBootstrapper.java:82)
- at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.start(DocumentationPluginsBootstrapper.java:100)
- at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:179)
- ... 14 common frames omitted
复制代码 遇到问题不要怕
查询问题并解决问题嘛;从哪查,我想大家已经达成了统一的共识:大数据模型。deepseek 就是热门之一,我们直接将堆栈信息扔给他,让他提供解决方案。他一针见血分析出了原因
这个错误通常是由于 Springfox Swagger(springfox-swagger2)与 Spring Boot 版本不兼容 或 Spring MVC 路径匹配策略冲突 导致的。以下是几种解决方案
- 降级 Spring Boot 版本
Spring Boot 2.6+ 开始与 springfox 不兼容,但 Springfox Swagger 2.x 已经停止维护了,所以说通过升级 Springfox Swagger 来适配 Spring Boot 2.6+ 是不行了。
我们的目的是升级 Spring Boot,那么降级 Spring Boot 这个方案肯定是行不通的。
- 升级到 SpringDoc OpenAPI
SpringDoc 是 Swagger 的替代方案,支持 OpenAPI 3.0,兼容 Spring Boot 2.6+ 和 3.x
考虑到注解变动大,需要调整的地方太多,这个方案不到万不得已不采用
- 修改路径匹配策略
如果不想降级 Spring Boot,可以调整路径匹配策略- spring:
- mvc:
- pathmatch:
- matching-strategy: ant_path_matcher
复制代码这个调整简单,感觉可行,试试发现雀氏可以,采用这种方案
- Swagger 配置是否正确
确保 @EnableSwagger2 和 Docket 配置正确- @Configuration
- @EnableSwagger2
- public class SwaggerConfig {
- @Bean
- public Docket api() {
- return new Docket(DocumentationType.SWAGGER_2)
- .select()
- .apis(RequestHandlerSelectors.basePackage("com.your.package")) // 替换成你的 Controller 包名
- .paths(PathSelectors.any())
- .build();
- }
- }
复制代码 这个确定是配置正确的,不是这个问题
deepseek 还给了其他方案,大家可以结合自己的实际情况,看看方案是否适用。如果你们以为坑就这么填平了,那只能说你们还是太年轻啦
我再加个依赖- <dependency>
- <groupId>org.springframework.boot</groupId>
- spring-boot-starter-actuator</artifactId>
- </dependency>
复制代码 启动服务,同样的问题又来了- org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException
- at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:182)
- at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:54)
- at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:357)
- at java.lang.Iterable.forEach(Iterable.java:75)
- at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:156)
- at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:124)
- at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:946)
- at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:594)
- at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147)
- at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:732)
- at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:409)
- at org.springframework.boot.SpringApplication.run(SpringApplication.java:308)
- at org.springframework.boot.SpringApplication.run(SpringApplication.java:1300)
- at org.springframework.boot.SpringApplication.run(SpringApplication.java:1289)
- at com.qsl.Application.main(Application.java:16)
- Caused by: java.lang.NullPointerException: null
- at springfox.documentation.spring.web.WebMvcPatternsRequestConditionWrapper.getPatterns(WebMvcPatternsRequestConditionWrapper.java:56)
- at springfox.documentation.RequestHandler.sortedPaths(RequestHandler.java:113)
- at springfox.documentation.spi.service.contexts.Orderings.lambda$byPatternsCondition$3(Orderings.java:89)
- at java.util.Comparator.lambda$comparing$77a9974f$1(Comparator.java:469)
- at java.util.TimSort.countRunAndMakeAscending(TimSort.java:355)
- at java.util.TimSort.sort(TimSort.java:220)
- at java.util.Arrays.sort(Arrays.java:1512)
- at java.util.ArrayList.sort(ArrayList.java:1454)
- at java.util.stream.SortedOps$RefSortingSink.end(SortedOps.java:387)
- at java.util.stream.Sink$ChainedReference.end(Sink.java:258)
- at java.util.stream.Sink$ChainedReference.end(Sink.java:258)
- at java.util.stream.Sink$ChainedReference.end(Sink.java:258)
- at java.util.stream.Sink$ChainedReference.end(Sink.java:258)
- at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:482)
- at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
- at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
- at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
- at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
- at springfox.documentation.spring.web.plugins.WebMvcRequestHandlerProvider.requestHandlers(WebMvcRequestHandlerProvider.java:81)
- at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
- at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1374)
- at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
- at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
- at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
- at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
- at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
- at springfox.documentation.spring.web.plugins.AbstractDocumentationPluginsBootstrapper.withDefaults(AbstractDocumentationPluginsBootstrapper.java:107)
- at springfox.documentation.spring.web.plugins.AbstractDocumentationPluginsBootstrapper.buildContext(AbstractDocumentationPluginsBootstrapper.java:91)
- at springfox.documentation.spring.web.plugins.AbstractDocumentationPluginsBootstrapper.bootstrapDocumentationPlugins(AbstractDocumentationPluginsBootstrapper.java:82)
- at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.start(DocumentationPluginsBootstrapper.java:100)
- at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:179)
- ... 14 common frames omitted
复制代码 同个问题出现一次也就算了,换个方式出现第二次,有点欺负人了!
此时我们再去问 deepseek,给出的解决方案,尝试了都不对,好在在网上找到了解决方案,增加如下配置
[code]@Configuration@EnableSwagger2public class SwaggerConfig { /** * springboot 2.7.x不支持swagger2,注册bean进行兼容 * 该方法在Spring Boot 2.7.x中手动注册WebMvcEndpointHandlerMapping,用于解决Swagger无法直接访问Actuator端点的问题。下面详细说明其作用: * 1. 收集所有可暴露的端点 * 使用以下组件获取不同类型的端点: * webEndpointsSupplier.getEndpoints():获取所有基于Web的端点(如/actuator/health)。 * servletEndpointsSupplier.getEndpoints():获取Servlet类型的端点(如/actuator/servlet)。 * controllerEndpointsSupplier.getEndpoints():获取Controller类型的端点。 * 将这些端点统一添加到allEndpoints列表中,以便后续处理。 * 2. 设置端点的基础路径 * 从WebEndpointProperties中获取配置的端点基础路径(basePath),默认值通常是/actuator。 * 创建一个EndpointMapping对象,并传入基础路径,用于定义端点的URL映射规则。 * 3. 判断是否需要注册端点链接映射 * 判断条件如下: * 如果启用了端点发现(webEndpointProperties.getDiscovery().isEnabled())。 * 并且设置了有效的基础路径(StringUtils.hasText(basePath)),或者管理端口与应用端口不同(ManagementPortType.DIFFERENT)。 * 如果满足条件,则创建端点链接映射,用于生成包含所有可用端点的首页链接(例如/actuator)。 * 4. 构建并返回WebMvcEndpointHandlerMapping * 创建WebMvcEndpointHandlerMapping实例时传入以下参数: * endpointMapping:定义端点的基础路径。 * webEndpoints:需要注册的Web类型端点集合。 * endpointMediaTypes:定义端点支持的媒体类型(如JSON、YAML等)。 * corsConfiguration:跨域资源共享(CORS)配置。 * new EndpointLinksResolver(allEndpoints, basePath):用于生成端点链接的解析器。 * shouldRegisterLinksMapping:是否注册端点链接映射。 * null:通常用于指定自定义的请求谓词,此处为默认值。 * 返回的WebMvcEndpointHandlerMapping使Swagger能够正常访问和展示Actuator端点的信息。 * 5. 兼容性适配 * Spring Boot 2.7.x之后,Swagger 2不再直接支持访问Actuator端点,此方法通过手动注册WebMvcEndpointHandlerMapping来恢复兼容性,确保Swagger UI可以正确显示和调用这些监控和管理接口。 * 总结 * 此方法的核心目的是手动注册端点处理器映射,以确保Swagger能够正确访问Spring Boot Actuator提供的各种监控和管理端点。通过整合多种端点类型、设置基础路径、启用链接映射等方式,使得开发者能够在Swagger UI中方便地测试和使用这些端点。 */ @Bean public WebMvcEndpointHandlerMapping webEndpointServletHandlerMapping(WebEndpointsSupplier webEndpointsSupplier, ServletEndpointsSupplier servletEndpointsSupplier, ControllerEndpointsSupplier controllerEndpointsSupplier, EndpointMediaTypes endpointMediaTypes, CorsEndpointProperties corsProperties, WebEndpointProperties webEndpointProperties, Environment environment) { List |