美文网首页
springboot2.4 跨域问题

springboot2.4 跨域问题

作者: _Rondo | 来源:发表于2021-03-23 18:10 被阅读0次

    nginx + springboot2.4 跨域问题,添加header、更改springmvc 配置方式,百度搜了一下午,快吐血了,都不行。后来谷歌才改好的,配置方式变了,需要用新的方式,相当于踩坑了

        @Bean
        public CorsFilter corsFilter() {
            CorsConfiguration corsConfiguration = new CorsConfiguration();
            //1,允许任何来源
            corsConfiguration.setAllowedOriginPatterns(Collections.singletonList("*"));
            //2,允许任何请求头
            corsConfiguration.addAllowedHeader(CorsConfiguration.ALL);
            //3,允许任何方法
            corsConfiguration.addAllowedMethod(CorsConfiguration.ALL);
            //4,允许凭证
            corsConfiguration.setAllowCredentials(true);
            UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
            source.registerCorsConfiguration("/**", corsConfiguration);
            return new CorsFilter(source);
        }
    

    08.27更新 gateway上边不好使 这个可以

    @Configuration
    public class WebConfig implements WebMvcConfigurer {
        @Override
        public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/**")
                    .allowedOriginPatterns("*")
                    .allowedMethods("*")
                    .maxAge(3600)
                    .allowCredentials(true);
        }
    }
    

    -end-

    相关文章

      网友评论

          本文标题:springboot2.4 跨域问题

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