• SpringBoot中重写addCorsMapping解决跨域以及提示list them explicitly or consider using "allowedOriginPatterns" instead.


    场景

    SpringBoot中通过重写WebMvcConfigurer的addCorsMapping方法实现后台服务解决跨域问题:

    https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/111283955

    在SpringBoot中做静态资源映射时,通过编写配置类实现WebMvcConfigurer接口并重写addResourceHandlers方法进而

    实现静态资源映射,将服务器磁盘路径映射出一个网络url。

    但是当前端使用该静态url时提示跨域问题。

    之前在上面的博客中解决跨域问题时可以直接重写addCordMappings方法即可。但是在应用时发现不好使,并且后台提示

    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.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1014) ~[spring-webmvc-5.3.1.jar:5.3.1]
     at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) ~[spring-webmvc-5.3.1.jar:5.3.1]

    注:

    博客:
    https://blog.csdn.net/badao_liumang_qizhi
    关注公众号
    霸道的程序猿
    获取编程相关电子书、教程推送与免费下载。

    实现

    1、按照提示内容,将allowedOrigins修改为allowedOriginPatterns

        @Override
        public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/**")  // 拦截所有的请求
                    //.allowedOrigins("*")  // 可跨域的域名,可以为 *
                    .allowedOriginPatterns("*")
                    .allowCredentials(true)
                    .allowedMethods("*")   // 允许跨域的方法,可以单独配置
                    .allowedHeaders("*");  // 允许跨域的请求头,可以单独配置
        }

    2、完整添加的解决跨域的配置内容

    package com.chrisf.config;
    
    import com.chrisf.constant.Constants;
    import org.springframework.beans.factory.annotation.Autowired;
    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.InterceptorRegistry;
    import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    
    @Configuration
    @SuppressWarnings("SpringJavaAutowiredFieldsWarningInspection")
    public class ResourcesConfig implements WebMvcConfigurer {
    
    
        @Override
        public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/**")  // 拦截所有的请求
                    .allowedOriginPatterns("*")
                    .allowCredentials(true)
                    .allowedMethods("*")   // 允许跨域的方法,可以单独配置
                    .allowedHeaders("*");  // 允许跨域的请求头,可以单独配置
        }
    }

    3、完整带静态资源映射以及跨域配置

    package com.chrisf.config;
    
    import com.chrisf.constant.Constants;
    import org.springframework.beans.factory.annotation.Autowired;
    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.InterceptorRegistry;
    import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    
    
    @Configuration
    @SuppressWarnings("SpringJavaAutowiredFieldsWarningInspection")
    public class ResourcesConfig implements WebMvcConfigurer {
    
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            /** 本地文件上传路径 */
            registry.addResourceHandler(Constants.RESOURCE_PREFIX + "/**").addResourceLocations("file:" + RuoYiConfig.getProfile() + "/");
        }
    
        @Override
        public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/**")  // 拦截所有的请求
                    //.allowedOrigins("*")  // 可跨域的域名,可以为 *
                    .allowedOriginPatterns("*")
                    .allowCredentials(true)
                    .allowedMethods("*")   // 允许跨域的方法,可以单独配置
                    .allowedHeaders("*");  // 允许跨域的请求头,可以单独配置
        }
    }
  • 相关阅读:
    HDU
    HDU
    (4)数据--相似性与相异性
    (3)数据--操作
    (2)数据--基本概念
    五、按生命周期划分数据(二)
    五、常用数据类型(一)
    四、坏耦合的原因与解耦(三)
    四、强化耦合(二)
    四、初识耦合(一)
  • 原文地址:https://www.cnblogs.com/badaoliumangqizhi/p/16722855.html
Copyright © 2020-2023  润新知