SpringBoot實現(xiàn)版本升級到2.7.18
前言
目前項目上掃描出一些 Java 依賴的代碼漏洞,需要對現(xiàn)有依賴版本升級,記錄一下遇到的問題。
<spring-boot.version>2.3.2.RELEASE</spring-boot.version> <spring-cloud.version>Hoxton.SR9</spring-cloud.version> <spring-cloud-alibaba.version>2.2.6.RELEASE</spring-cloud-alibaba.version>
升級到
<spring-boot.version>2.7.18</spring-boot.version> <spring-cloud.version>2021.0.8</spring-cloud.version> <spring-cloud-alibaba.version>2021.0.5.0</spring-cloud-alibaba.version>
2.7.18 版本的 Spring Boot 支持 JDK 8 ,再往后需要 JDK 17 了。

啟動報錯記錄
1. Nacos 字樣報錯信息
Add a spring.config.import=nacos: property to your configuration.

解決方法,增加依賴
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bootstrap</artifactId> </dependency>
2. spring-data-commons 相關類找不到
org/springframework/data/repository/core/support/RepositoryMethodInvocationListener

解決方法,增加依賴
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<!--默認版本沒效果2.7.18 可能是依賴下載問題-->
<version>2.7.18</version>
</dependency>3. 不再提供默認負載均衡
nested exception is java.lang.IllegalStateException: No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalancer?
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-loadbalancer</artifactId> </dependency>
4. 默認不支持循環(huán)依賴
Relying upon circular references is discouraged and they are prohibited by default. Update your application to remove the dependency cycle between beans. As a last resort, it may be possible to break the cycle automatically by setting spring.main.allow-circular-references to true.
解決,開啟循環(huán)依賴
spring:
main:
allow-circular-references: true5. thymeleaf 相關類找不到
java.lang.ClassNotFoundException: org.thymeleaf.util.VersionUtils
版本沖突導致,統(tǒng)一thymeleaf版本
6. swagger2 相關報錯
Failed to start bean ‘documentationPluginsBootstrapper’; nested exception is java.lang.NullPointerException
Swagger2 bug導致
解決:增加配置
@Bean
public static BeanPostProcessor springfoxHandlerProviderBeanPostProcessor() {
return new BeanPostProcessor() {
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof WebMvcRequestHandlerProvider) {
customizeSpringfoxHandlerMappings(getHandlerMappings(bean));
}
return bean;
}
private <T extends RequestMappingInfoHandlerMapping> void customizeSpringfoxHandlerMappings(List<T> mappings) {
List<T> copy = mappings.stream()
.filter(mapping -> mapping.getPatternParser() == null)
.collect(Collectors.toList());
mappings.clear();
mappings.addAll(copy);
}
@SuppressWarnings("unchecked")
private List<RequestMappingInfoHandlerMapping> getHandlerMappings(Object bean) {
try {
Field field = ReflectionUtils.findField(bean.getClass(), "handlerMappings");
field.setAccessible(true);
return (List<RequestMappingInfoHandlerMapping>) field.get(bean);
} catch (IllegalArgumentException | IllegalAccessException e) {
throw new IllegalStateException(e);
}
}
};
}配置過時
1. ResourceProperties
Spring Boot 2.4.0版本之后已作廢,2.6.0版本被移除
org.springframework.boot.autoconfigure.web.ResourceProperties
2. StringUtils
commons-lang 升級到 commons-lang3
3. 單元測試注解
@BeforeEach 代替 @Before
4. 數(shù)組轉集合
CollectionUtils.arrayToList(key)
替換為
Arrays.asList
5. Hystrix
Spring Cloud 2020 以后就不再支持 Hystrix
建議替換為 Sentinel。
仍要使用 Hystrix 的話,相關 yaml 配置和啟用注解有變化。
Spring Security OAuth2
目前使用的版本是2.2.5,也是最后一個版本
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-oauth2</artifactId> <version>2.2.5.RELEASE</version> </dependency>
Spring Boot 升級后,會有問題,需要對相關依賴版本進行降版本,降到5.3以下,
但之前 Spring Security 有個漏洞需要升級到 5.5.7 。
所以目前解決的方法是自己搭建認證服務,不使用 OAuth2
Spring Authorization Server 學習一下。Spring Security OAuth 已不再維護,官網(wǎng)鏈接也已刪除

總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
SpringBoot實現(xiàn)多文件上傳的詳細示例代碼
文件上傳中并沒有什么太多的知識點,下面這篇文章主要給大家介紹了關于SpringBoot實現(xiàn)多文件上傳的詳細示例,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2023-03-03
Spring之借助Redis設計一個簡單訪問計數(shù)器的示例
本篇文章主要介紹了Spring之借助Redis設計一個簡單訪問計數(shù)器的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06

