跨域


跨域

概念简介

跨域: 指的是浏览器不能执行其他网站的脚本。它是由浏览器的同源策略(同源策略:是指协议,域名,端口都要相同,其中有一个不同都会产生跨域;) 造成的,是浏览器对javascript施加的安全限制。

在这里插入图片描述

跨域流程

详细解析
在这里插入图片描述

解决方法①:使用nginx部署为同一域

在这里插入图片描述

解决方法②:配置当次请求允许跨域

在这里插入图片描述

自己写一个配置类

@Configuration
public class GulimallCrosConf {

    @Bean
    public CorsWebFilter corsWebFilter(){
        //记得要选reactive包,不然会报错
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();

        CorsConfiguration corsConfiguration = new CorsConfiguration();
        //配置跨域
        corsConfiguration.addAllowedHeader("*");//允许任意请求头跨域
        corsConfiguration.addAllowedMethod("*");//允许任意请求方式(Get\post\..)跨域
        corsConfiguration.addAllowedOrigin("*");//允许任意请求来源跨域
        corsConfiguration.setAllowCredentials(true);//允许带cookie跨域



        source.registerCorsConfiguration("/**",corsConfiguration);
        return new CorsWebFilter(source);

    }
}

文章作者: fFee-ops
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 fFee-ops !
评论
  目录