星空网 > 软件开发 > Java

Spring Cloud 前后端分离后引起的跨域访问解决方案

背景

Spring Cloud 微服务试点改造,目前在尝试前后端分离。

前台A应用(本机8080端口),通过网管(本机8769端口)调用后台应用B(本机8082端口)、应用C发布的http服务。。

Spring Cloud 前后端分离后引起的跨域访问解决方案

A的js代码如下:

$.ajax({   type: "POST",   async: "true",   url: "http://127.0.0.1:8769/service-B/getResInfo",   data:{resTypeId:201}   dataType:"json",   error: function (+ "," + errorThrown);   },   beforeSend: function (function (data) {    ...   }    });

运行后报错:

Origin 'http://localhost:8080' is therefore not allowed access.

问题原因

A的前台访问B应用,导致了跨域。

跨域访问违反了同源策略,同源策略规定:浏览器的ajax只能访问跟它的前台页面同源(相同域名或IP)的资源。

也就是说,如果A的前台访问A的后台,则不会跨域。。

解决方案

尝试了两种解决办法,最终采用了方案二。。

方案一:

在被调用的类或方法上增加@CrossOrigin注解来声明自己支持跨域访问

origins=*表示允许所有来源都支持,也可以定义特定的来源,比 表示response里会增加标示Access-Control-Allow-Credentials=true
@RequestMapping(value="/getResInfo",method = {RequestMethod.POST})@CrossOrigin(allowCredentials="true", allowedHeaders="*", methods={RequestMethod.POST}, origins="*") public List<Map<String,Object>> getResInfo(@RequestParam(name = "resTypeId", required = false) String resTypeId){。。。}

如果只是针对某个服务需要被跨域访问,用此方案可行。。

但由于我们进行了前后端分离,前台调用的都是跨域的服务,此方案需要对几乎所有的B、C应用的服务对应的方法或者类上增加注解,不太合适。

而且,如果B、C服务都开放了跨域访问,则可能存在安全隐患,因为其他未知应用也可以访问这些服务。。

方案二:

在网管zuul里增加CorsFilter过滤器,比如下图直接在启动类里增加红色部分代码

@SpringBootApplication@EnableZuulProxypublic class DemoZuulApplication { public static void main(String[] args) {  SpringApplication.run(DemoZuulApplication.class, args); }   @Bean public CorsFilter corsFilter() {  final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();  final CorsConfiguration config = new CorsConfiguration();  config.setAllowCredentials(true); // 允许cookies跨域  config.addAllowedOrigin("*");// #允许向该服务器提交请求的URI,*表示全部允许  config.addAllowedHeader("*");// #允许访问的头信息,*表示全部  config.setMaxAge(18000L);// 预检请求的缓存时间(秒),即在这个时间段里,对于相同的跨域请求不会再预检了  config.addAllowedMethod("*");// 允许提交请求的方法,*表示全部允许,也可以单独设置GET、PUT等 /* config.addAllowedMethod("HEAD");  config.addAllowedMethod("GET");// 允许Get的请求方法  config.addAllowedMethod("PUT");  config.addAllowedMethod("POST");  config.addAllowedMethod("DELETE");  config.addAllowedMethod("PATCH");*/  source.registerCorsConfiguration("/**", config);  return new CorsFilter(source); }}

由于此方案是增加到网管上的,对B、C应用的代码都无任何改造。。

且因为B、C未直接开放跨域访问,所以其他应用无法跨越访问B、C服务,比如A不经过网关直接访问B、C应用会访问失败。。

后续会对网管应用装配上SSO进行单点登录校验,来更好的保障服务安全。。

 

本文参考

原标题:Spring Cloud 前后端分离后引起的跨域访问解决方案

关键词:Spring

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

Eagllwin:https://www.goluckyvip.com/tag/17704.html
EarData:https://www.goluckyvip.com/tag/17705.html
Eargo:https://www.goluckyvip.com/tag/17706.html
Early:https://www.goluckyvip.com/tag/17707.html
Early Reviewer计划:https://www.goluckyvip.com/tag/17708.html
earth blues:https://www.goluckyvip.com/tag/17709.html
北京丰台区水上乐园哪家好玩?:https://www.vstour.cn/a/366177.html
23点聊电商:“潮起钱塘·境遇未来”2024年跨境电商全平台发展大会圆满完成 :https://www.kjdsnews.com/a/1836652.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流