跨域处理两种方式

方式一 前端nginx配置

 location /bph_sp {
			proxy_pass http://bph_sp.com; #与服务器集群名称一致
			client_max_body_size    200m;  #上传最大值
			proxy_set_header Host $host:$server_port;
			#websocket支持
			proxy_http_version 1.1;
			proxy_set_header Upgrade $http_upgrade;
			proxy_set_header Connection "upgrade";			
			proxy_connect_timeout 74s; #配置点1 长连接ajax请求超时时间
			proxy_read_timeout 3000s; #配置点2,该指令设置与代理服务器的读超时时间。它决定了nginx会等待多长时间来获得请求的响应。这个时间不是获得整个response的时间,而是两次reading操作的时间。
			proxy_send_timeout 3000s; #配置点3
			#跨域处理,服务器和nginx选一个就行
			add_header Access-Control-Allow-Origin *;
			add_header Access-Control-Allow-Methods 'GET,POST';
			add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';		
			 
			
			proxy_redirect default; 
				root   html;
				index  index.html index.jsp;#tomcat的访问地址:index.jsp
        }

方式二 后端

   /**
     * 跨域配置
     */
    @Bean
    public CorsFilter corsFilter()
    {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true);
        // 设置访问源地址
        config.addAllowedOrigin("*");
        // 设置访问源请求头
        config.addAllowedHeader("*");
        // 设置访问源请求方法
        config.addAllowedMethod("*");
        // 对接口配置跨域设置
        source.registerCorsConfiguration("/**", config);
        return new CorsFilter(source);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值