首页 > 其他分享 >Spring Boot 2.3.x 升级至 2.4.x 遇到的那些事

Spring Boot 2.3.x 升级至 2.4.x 遇到的那些事

时间:2022-10-12 11:00:15浏览次数:49  
标签:corsConfiguration CorsConfiguration Spring Boot 2.3 org 2.4

随着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);
    }

}

标签:corsConfiguration,CorsConfiguration,Spring,Boot,2.3,org,2.4
From: https://www.cnblogs.com/wallacepang/p/16783759.html

相关文章

  • springbean的八种加载方式
    总结了一下八种bean的加载方式bold;">接口 xml+<bean/> xml:context+注解(@Component+4个@Bean) 配置类+扫描+注解(@Component+4个@Bean)@Bean定义FactoryBean接口......
  • SpringBoot_RestFul风格CURD
    一、什么是RestFulREST(英文:RepresentationalStateTransfer,简称REST,意思:表述性状态转换,描述了一个架构样式的网络系统,比如web应用),是一种软件架构风格不是标准哦!一种软......
  • SpringBoot笔记
    Idea环境创建项目:   ......
  • task2.3
    #include<stdio.h>#include<math.h>intmain(){doublex,ans;while(scanf("%lf",&x)!=EOF){ans=9.0/5.0*x+32.0;printf("摄氏度c=%.2lf;华氏度f=%.2lf\n",x,ans)......
  • 这些不知道,别说你熟悉 Spring
    大家好,这篇文章跟大家来聊下Spring中提供的常用扩展点、SpringSPI机制、以及SpringBoot自动装配原理,重点介绍下Spring基于这些扩展点怎么跟配置中心(Apollo、Nacos、......
  • SpringBoot+MyBatis Plus对Map中Date格式转换的处理
    在SpringBoot项目中,如何统一JSON格式化中的日期格式问题现在的关系型数据库例如PostgreSQL/MySQL,都已经对JSON类型提供相当丰富的功能,项目中对于不需要检索但是......
  • 【SpringCloud】(一)分布式理论
    分布式架构理论方法远程调用各个模块运行于不同的tomcat,模块之间通过网络进行调用。远程调用的技术演进1WebService解决应用程序之间的跨平台访问问题,基于SOAP/WSDL......
  • spring 的 IOC 和 AOP
    springjava企业开发框架spring全家桶web:springmvc、springflux持久层:redis、magnodb、springdata安全性:springsecurity脚手架:springboot微服务:springclo......
  • Spring Batch BATCH_JOB_INSTANCE 表不存在错误
    在运行SpringBatch项目的时候,提示上面的错误信息:java.sql.SQLSyntaxErrorException:(conn=12433)Table'usrealestate.BATCH_JOB_INSTANCE'doesn'texist  ......
  • springboot Druid后台监控功能和过滤
    @ControllerpublicclassDruidConfig{@ConfigurationProperties(prefix="spring.datasource")@BeanpublicDataSourcedruidDataSource(){return......