zoukankan      html  css  js  c++  java
  • spring boot2 坑

    @Configuration
    @EnableWebMvc
    public class MyWebMvcConfigurer implements WebMvcConfigurer {
    
        @Override
        public void addCorsMappings(CorsRegistry registry) {
            //设置允许跨域的路径
            registry.addMapping("/**")
                    //是否允许证书 不再默认开启
                    .allowCredentials(true)
                    .allowedOrigins("*")
                    .allowedMethods("*")
                    .allowedHeaders("*");
                    //跨域允许时间
    //                .maxAge(3600);
        }
    
        private CorsConfiguration addCorsConfig() {
            CorsConfiguration corsConfiguration = new CorsConfiguration();
            List<String> list = new ArrayList<>();
            list.add("*");
            corsConfiguration.setAllowedOrigins(list);
            //请求常用的三种配置,*代表允许所有,当时你也可以自定义属性(比如header只能带什么,只能是post方式等等)
            corsConfiguration.addAllowedOrigin("*");
            corsConfiguration.addAllowedHeader("*");
            corsConfiguration.addAllowedMethod("*");
            return corsConfiguration;
        }
    
        @Bean
        public CorsFilter corsFilter() {
            UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
            source.registerCorsConfiguration("/**", addCorsConfig());
            return new CorsFilter(source);
        }
    
    }
    

     spring boot1.5版本好用的代码,在升级新框架以后不能用了。

    解决方案为下面这部分代码

    private CorsConfiguration addCorsConfig() {
            CorsConfiguration corsConfiguration = new CorsConfiguration();
            List<String> list = new ArrayList<>();
            list.add("*");
            corsConfiguration.setAllowedOrigins(list);
            //请求常用的三种配置,*代表允许所有,当时你也可以自定义属性(比如header只能带什么,只能是post方式等等)
            corsConfiguration.addAllowedOrigin("*");
            corsConfiguration.addAllowedHeader("*");
            corsConfiguration.addAllowedMethod("*");
            return corsConfiguration;
        }
    
        @Bean
        public CorsFilter corsFilter() {
            UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
            source.registerCorsConfiguration("/**", addCorsConfig());
            return new CorsFilter(source);
        }
    
  • 相关阅读:
    Windows下使用Visual Studio Code搭建Go语言环境
    无缓冲和带缓冲channel的区别
    Asp.Net MVC如何返回401响应码
    从这里开始我的博客园
    java判定字符串中仅有数字和- 正则表达式匹配 *** 最爱那水货
    主席树
    Mybitis+springMVC 套路
    jeeplus ani 文档路径
    jquery easyui datagrid 多选只能获取一条数据
    python写入文件编码报错
  • 原文地址:https://www.cnblogs.com/song-wentao/p/9354456.html
Copyright © 2011-2022 走看看