SpringBoot實(shí)現(xiàn)版本升級(jí)到2.7.18
前言
目前項(xiàng)目上掃描出一些 Java 依賴(lài)的代碼漏洞,需要對(duì)現(xiàn)有依賴(lài)版本升級(jí),記錄一下遇到的問(wè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>
升級(jí)到
<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 了。
啟動(dòng)報(bào)錯(cuò)記錄
1. Nacos 字樣報(bào)錯(cuò)信息
Add a spring.config.import=nacos: property to your configuration.
解決方法,增加依賴(lài)
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bootstrap</artifactId> </dependency>
2. spring-data-commons 相關(guān)類(lèi)找不到
org/springframework/data/repository/core/support/RepositoryMethodInvocationListener
解決方法,增加依賴(lài)
<dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-commons</artifactId> <!--默認(rèn)版本沒(méi)效果2.7.18 可能是依賴(lài)下載問(wèn)題--> <version>2.7.18</version> </dependency>
3. 不再提供默認(rèn)負(fù)載均衡
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. 默認(rèn)不支持循環(huán)依賴(lài)
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.
解決,開(kāi)啟循環(huán)依賴(lài)
spring: main: allow-circular-references: true
5. thymeleaf 相關(guān)類(lèi)找不到
java.lang.ClassNotFoundException: org.thymeleaf.util.VersionUtils
版本沖突導(dǎo)致,統(tǒng)一thymeleaf版本
6. swagger2 相關(guān)報(bào)錯(cuò)
Failed to start bean ‘documentationPluginsBootstrapper’; nested exception is java.lang.NullPointerException
Swagger2 bug導(dǎo)致
解決:增加配置
@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); } } }; }
配置過(guò)時(shí)
1. ResourceProperties
Spring Boot 2.4.0版本之后已作廢,2.6.0版本被移除
org.springframework.boot.autoconfigure.web.ResourceProperties
2. StringUtils
commons-lang 升級(jí)到 commons-lang3
3. 單元測(cè)試注解
@BeforeEach 代替 @Before
4. 數(shù)組轉(zhuǎn)集合
CollectionUtils.arrayToList(key)
替換為
Arrays.asList
5. Hystrix
Spring Cloud 2020 以后就不再支持 Hystrix
建議替換為 Sentinel。
仍要使用 Hystrix 的話(huà),相關(guān) yaml 配置和啟用注解有變化。
Spring Security OAuth2
目前使用的版本是2.2.5,也是最后一個(gè)版本
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-oauth2</artifactId> <version>2.2.5.RELEASE</version> </dependency>
Spring Boot 升級(jí)后,會(huì)有問(wèn)題,需要對(duì)相關(guān)依賴(lài)版本進(jìn)行降版本,降到5.3以下,
但之前 Spring Security 有個(gè)漏洞需要升級(jí)到 5.5.7 。
所以目前解決的方法是自己搭建認(rèn)證服務(wù),不使用 OAuth2
Spring Authorization Server 學(xué)習(xí)一下。Spring Security OAuth 已不再維護(hù),官網(wǎng)鏈接也已刪除
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java中notify()和notifyAll()的使用區(qū)別
本文主要介紹了Java中notify()和notifyAll()的使用區(qū)別,文中通過(guò)示例代碼介紹的非常詳細(xì),感興趣的小伙伴們可以參考一下2021-06-06Java中Lambda表達(dá)式之Lambda語(yǔ)法與作用域解析
這篇文章主要介紹了Java中Lambda表達(dá)式之Lambda語(yǔ)法與作用域解析重點(diǎn)介紹Lambda表達(dá)式基礎(chǔ)知識(shí),需要的朋友可以參考下2017-02-02log4j控制臺(tái)不打印日志故障的詳細(xì)解決方案
這篇文章主要給大家介紹了關(guān)于log4j控制臺(tái)不打印日志故障的詳細(xì)解決方案,log4j不提供默認(rèn)配置,因?yàn)樵谀承┉h(huán)境中可能禁止輸出到控制臺(tái)或文件系統(tǒng),需要的朋友可以參考下2023-08-08Spring注解中@Configuration和@Component到底有啥區(qū)別
之前一直搞不清@Component和@Configuration這兩個(gè)注解到底有啥區(qū)別,一直認(rèn)為被這兩修飾的類(lèi)可以被Spring實(shí)例化嘛,最近終于弄明白了,這篇文章主要給大家介紹了關(guān)于Spring注解中@Configuration和@Component到底有啥區(qū)別的相關(guān)資料,需要的朋友可以參考下2023-04-04JDK1.8中的ConcurrentHashMap使用及場(chǎng)景分析
這篇文章主要介紹了JDK1.8中的ConcurrentHashMap使用及場(chǎng)景分析,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01淺談Zookeeper開(kāi)源客戶(hù)端框架Curator
這篇文章主要介紹了淺談Zookeeper開(kāi)源客戶(hù)端框架Curator的相關(guān)內(nèi)容,具有一定參考價(jià)值,需要的朋友可以了解下。2017-10-10spring的同一定時(shí)任務(wù)上一次的任務(wù)未結(jié)束前不會(huì)啟動(dòng)這次任務(wù)問(wèn)題
這篇文章主要介紹了spring的同一定時(shí)任務(wù)上一次的任務(wù)未結(jié)束前不會(huì)啟動(dòng)這次任務(wù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12