美文网首页
spring boot 2.3.x升级到2.5.x遇到的问题

spring boot 2.3.x升级到2.5.x遇到的问题

作者: pilisiyang | 来源:发表于2023-07-30 16:28 被阅读0次
    1. 由于spring security的漏洞,需要将5.5.x 版本升级至5.5.7及其以上,相应的spring boot 版本和 spring cloud版本也需要同步升级,下面是版本对照信息
    Release Train Release Train
    2022.0.x aka Kilburn 3.0.x, 3.1.x (Starting with 2022.0.3)
    2021.0.x aka Jubilee 2.6.x, 2.7.x (Starting with 2021.0.3)
    2020.0.x aka Ilford 2.4.x, 2.5.x (Starting with 2020.0.3)
    Hoxton 2.2.x, 2.3.x (Starting with SR5)
    Greenwich 2.1.x
    Finchley 2.0.x
    Edgware 1.5.x
    Dalston 1.5.x

    spring boot 升级到 2.5.15 版本
    spring cloud 升级到 2020.0.6 版本

    1. 更换 security 依赖,spring security已经从 spring cloud 中独立出来,所以需要替换原先的依赖
           <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-security</artifactId>
            </dependency>
    

    调整为:

           <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-security</artifactId>
            </dependency>
    
    1. 由于spring cloud 更换了负载均衡配置,弃用了ribbon,改用了 loadbalancer

    nacos 中需要剔除 ribbon依赖

            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-loadbalancer</artifactId>
            </dependency>
    
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
                <!--不使用Ribbon 进行客户端负载均衡-->
                <exclusions>
                    <exclusion>
                        <groupId>org.springframework.cloud</groupId>
                        <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
    
    1. 跨域配置项变更,CorsRegistry 方法中的 allowedOrigins,需要改写为 allowedOriginPatterns,写法上也有所改变
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.cors.CorsConfiguration;
    import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
    import org.springframework.web.filter.CorsFilter;
    import org.springframework.web.servlet.config.annotation.CorsRegistry;
    import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
    
    import java.util.ArrayList;
    import java.util.List;
    
    @Configuration
    public class CorsConfig extends WebMvcConfigurationSupport {
        private CorsConfiguration corsConfig() {
            CorsConfiguration corsConfiguration = new CorsConfiguration();
            corsConfiguration.addAllowedOrigin("*");
            corsConfiguration.addAllowedHeader("*");
            corsConfiguration.addAllowedMethod("*");
            corsConfiguration.setAllowCredentials(true);
            corsConfiguration.setMaxAge(3600L);
            return corsConfiguration;
        }
    
        @Override
        public void addCorsMappings(CorsRegistry registry) {
    
            registry.addMapping("/**")
                    .allowedOriginPatterns("*")
                    .allowCredentials(true)
                    .allowedMethods("GET", "POST", "DELETE", "PUT")
                    .maxAge(3600);
        }
    
        @Bean
        public CorsFilter corsFilter() {
            UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
            source.registerCorsConfiguration("/**", corsConfig());
    
            CorsConfiguration config = new CorsConfiguration();
            // 是否允许请求带有验证信息
            config.setAllowCredentials(true);
    
            // 允许访问的客户端域名
            // (springboot2.4以上的加入这一段可解决 allowedOrigins cannot contain the special value "*"问题)
            List<String> allowedOriginPatterns = new ArrayList<>();
            allowedOriginPatterns.add("*");
            config.setAllowedOriginPatterns(allowedOriginPatterns);
    
            // 设置访问源地址 config.addAllowedOrigin("*"); 设置访问源请求头
            config.addAllowedHeader("*");
            // 设置访问源请求方法
            config.addAllowedMethod("*");
            // 对接口配置跨域设置
            source.registerCorsConfiguration("/**", config);
            return new CorsFilter(source);
        }
    
        /**
         * 发现如果继承了WebMvcConfigurationSupport,则在yml中配置的相关内容会失效。 需要重新指定静态资源
         *
         * @param registry
         */
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
            registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
            registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
            super.addResourceHandlers(registry);
        }
    }
    
    1. application.yml更换本地开发负载均衡调用方式,原先的ribbon写法弃用,改用 loadbalancer 写法
    eureka:
      client:
        enabled: false
    
    admin:
      ribbon:
        listOfServers: http://localhost:8081
        NIWSServerListClassName: com.netflix.loadbalancer.ConfigurationBasedServerList
    

    更换为:

    spring:
      cloud:
        #    spring升级后,本地开发feign调用方式
        discovery:
          client:
            simple:
              instances:
                admin:
                  - instanceId: admin
                    host: localhost
                    port: 8081
    
    

    相关文章

      网友评论

          本文标题:spring boot 2.3.x升级到2.5.x遇到的问题

          本文链接:https://www.haomeiwen.com/subject/exsspdtx.html