Spring Boot 2.3.x 升级至 2.4.x 遇到的那些事
随着Spring Boot 3.0需要Java 17 和Spring Framework 6作为最低版本。计划逐步升级系统的Srping Boot版本,以应对未来的趋势,当前系统Spring Boot 版本是 2.3.12,继续先升即到2.4.13,然后2.5.X,2.6.X ,2.7.X,最后3.0系列。
防止版本跳跃多大,改动点太多。
在2.3.12升级到2.4.13的过程中遇到如下几个问题,在此记录一下:
一、springboot 2.4以上版本兼容junit4和junit5
springboot2.4以上版本移除了默认的junit4引擎,需要自行引入
<!-- 集成SpringBoot测试框架 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- SpringBoot兼容JUnit4兼容引擎 -->
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
</exclusion>
</exclusions>
</dependency>
二、跨域配置
使用 corsConfiguration.setAllowedOriginPatterns(Arrays.asList(CorsConfiguration.ALL)) 替换
corsConfiguration.addAllowedOrigin("*");
否则会出现如下异常:
java.lang.IllegalArgumentException: When allowCredentials is true, allowedOrigins cannot contain the special value "*" since that cannot be set on the "Access-Control-Allow-Origin" response header. To allow credentials to a set of origins, list them explicitly or consider using "allowedOriginPatterns" instead.
at org.springframework.web.cors.CorsConfiguration.validateAllowCredentials(CorsConfiguration.java:473)
at org.springframework.web.cors.CorsConfiguration.checkOrigin(CorsConfiguration.java:577)
at org.springframework.web.cors.DefaultCorsProcessor.checkOrigin(DefaultCorsProcessor.java:174)
at org.springframework.web.cors.DefaultCorsProcessor.handleInternal(DefaultCorsProcessor.java:116)
● Spring Boot 2.3 时, 使用如下配置
@Configuration
public class CorsConfig {
private CorsConfiguration buildConfig() {
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.addAllowedHeader("*"); // 设置访问源请求头
corsConfiguration.addAllowedMethod("GET"); // 允许GET方法访问
corsConfiguration.addAllowedMethod("POST"); // 允许POST方法访问
corsConfiguration.addAllowedMethod("OPTIONS"); // 允许OPTIONS方法访问
corsConfiguration.setAllowCredentials(Boolean.TRUE);
corsConfiguration.addAllowedOrigin("*");
corsConfiguration.setMaxAge(3600L);
return corsConfiguration;
}
@Bean
public CorsFilter corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", buildConfig()); // 4 对接口配置跨域设置
return new CorsFilter(source);
}
}
● Spring Boot 2.4,使用如下配置
@Configuration
public class CorsConfig {
private CorsConfiguration buildConfig() {
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.addAllowedHeader("*"); // 设置访问源请求头
corsConfiguration.addAllowedMethod("GET"); // 允许GET方法访问
corsConfiguration.addAllowedMethod("POST"); // 允许POST方法访问
corsConfiguration.addAllowedMethod("OPTIONS"); // 允许OPTIONS方法访问
corsConfiguration.setAllowCredentials(Boolean.TRUE);
// 2.4以后这样设置 替换 addAllowedOrigin("*)
corsConfiguration.setAllowedOriginPatterns(Arrays.asList(CorsConfiguration.ALL));
corsConfiguration.setMaxAge(3600L);
return corsConfiguration;
}
@Bean
public CorsFilter corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", buildConfig()); // 4 对接口配置跨域设置
return new CorsFilter(source);
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现