美文网首页
跨域问题及解决方案

跨域问题及解决方案

作者: BySjm | 来源:发表于2019-12-24 18:17 被阅读0次

    原因

    1.域名不一样

    2.Ajax异步请求

    解决:CORS 跨域资源共享

    在网关配置SpringMVC的CorsFilter

    @Configuration
    public class GlobalCORSConfig {
        @Bean
        public CorsFilter corsFilter() {
            //1.添加CORS配置信息
            CorsConfiguration config = new CorsConfiguration();
            //1) 允许的域,不要写*,否则cookie就无法使用了
            config.addAllowedOrigin("http://***.***.com");
            //2) 是否发送Cookie信息
            config.setAllowCredentials(true);
            //3) 允许的请求方式
            config.addAllowedMethod("OPTIONS");
            config.addAllowedMethod("HEAD");
            config.addAllowedMethod("GET");
            config.addAllowedMethod("PUT");
            config.addAllowedMethod("POST");
            config.addAllowedMethod("DELETE");
            // 4)允许的头信息
            config.addAllowedHeader("*");
            // 5)有效期 单位秒
            config.setMaxAge(3600L);
            //2.添加映射路径,我们拦截一切请求
            UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();
            configSource.registerCorsConfiguration("/**", config);
            //3.返回新的CORSFilter
            return new CorsFilter(configSource);
        }
    }
    

    相关文章

      网友评论

          本文标题:跨域问题及解决方案

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