Cors解决跨域问题

1.跨域问题:

跨域问题是浏览器对于ajax请求的一种安全限制:一个页面发起的ajax请求,只能是与当前页域名相同的路径,这能有效的阻止跨站攻击,当发起的请求域当前域名不同时,即产生跨域问题。
由下面两张图,可判定跨域:
在这里插入图片描述
在这里插入图片描述
解决方法:
在网关下配置跨域允许:
在这里插入图片描述

 * @auther Mr.wu
 * @date 2019/5/5 19:04
 *
 * 跨域配置
 */
@Configuration
public class LeyouCorsConfiguration {
    @Bean
    public CorsFilter corsFilter(){
        // 配置初始化对象
        CorsConfiguration configuration = new CorsConfiguration();
        // 允许跨域的域名,如果设置允许携带cookie为true,则不能写*,*代表所有的域名都可以跨域访问
        configuration.addAllowedOrigin("http://manage.leyou.com");
        configuration.addAllowedOrigin("http://www.leyou.com");
//        configuration.addAllowedOrigin("*");
        // 允许携带cookie
        configuration.setAllowCredentials(true);
        // 代表所有的请求方法
        configuration.addAllowedMethod("*");
        // 允许携带任何的头信息
        configuration.addAllowedHeader("*");

        // 初始化cors配置源对象
        UrlBasedCorsConfigurationSource configurationSource = new UrlBasedCorsConfigurationSource();
        configurationSource.registerCorsConfiguration("/**",configuration);

        // 返回corsFilter实例,参数:cors配置源对象
        return new CorsFilter(configurationSource);
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值