当前位置: 首页 > news >正文

nginx优雅如何优雅的接管【跨域配置】

跨域问题太常见了,这里不做详细赘述。文章主要想说一下,如何统一管理和更好的来管理  跨域配置

跨域的常见配置有两种 后台代码设置网关设置

1、后台代码设置  以springboot为例代码如下(水一下文章长度...)


@Configuration
public class WebMvcConfiguration extends WebMvcConfigurationSupport {private static final Logger logger = LoggerFactory.getLogger(WebMvcConfiguration.class);@Overridepublic void configurePathMatch(PathMatchConfigurer configurer) {//setUseSuffixPatternMatch 后缀模式匹配configurer.setUseSuffixPatternMatch(true);//setUseTrailingSlashMatch 自动后缀路径模式匹配configurer.setUseTrailingSlashMatch(true);}@Beanpublic CorsFilter corsFilter() {logger.info("---------------corsFilter--------------------");final UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();final CorsConfiguration corsConfiguration = new CorsConfiguration();corsConfiguration.setAllowCredentials(true);corsConfiguration.addAllowedOrigin("*");corsConfiguration.addAllowedHeader("*");corsConfiguration.addAllowedMethod("*");urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);return new CorsFilter(urlBasedCorsConfigurationSource);}/*** 设置跨域访问** @param registry*/@Overridepublic void addCorsMappings(CorsRegistry registry) {logger.info("---------------WebMvcConfigurer--------------------");registry.addMapping("/**").allowedHeaders("*").allowedOrigins("*").allowedMethods("*").allowCredentials(true);}}

2、网关设置-以nginx配置为例

add_header 'Access-Control-Allow-Origin' "*";
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS';
add_header 'Access-Control-Allow-Headers' '*';

栗子举完了,这里说下如有优雅起来 ~

以上代码现有问题:

1、跨域处理全部都开放的话,会有资源安全问题,很有可能会被盗用

2、管理项目比较多的话,如果代码设置了跨域处理。nginx又add_header一下会有报错的

The ‘Access-Control-Allow-Origin’ header contains multiple values ‘http://xxx.xxx.xx, *’, but only one is allowed.

已被CORS策略阻止:对预请求的响应未通过访问控制检查:“access control Allow Origin”标头包含多个值’‘http://xxx.xxx.xx,,*',但只允许使用一个。(这个属性不能有多个、也不能为空)

现在说一下目前认为比较合理的方案,用nginx全面接管跨域设置,覆盖代码跨域设定。添加origin域名校验

覆盖跨域处理,这里推荐用 “proxy_hide_header ”,也就是先隐藏 再添加,这样就不会出现多条header属性的问题了(也可以用more_set_headers模块来处理,但是完全没必要引入新module...)

nginx跨域代码如下

     #隐藏后端服务响应中的 Access-Control-Allow-Origin 标头proxy_hide_header 'Access-Control-Allow-Origin';proxy_hide_header 'Access-Control-Allow-Credentials';proxy_hide_header 'Access-Control-Allow-Methods';proxy_hide_header 'Access-Control-Allow-Headers';#通过$http_origin来判断域名是否允许访问set $cors "";if ($http_origin ~* '(test.com|test.cn)'){set $cors "true";}if ($cors = 'true'){add_header 'Access-Control-Allow-Origin' "$http_origin";add_header 'Access-Control-Allow-Credentials' 'true';add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS';add_header 'Access-Control-Allow-Headers' '*';}

这个思路应该是比较适用大多数的处理,那就按这个思路 让配置更优雅一些 ~

将上述代码放入到cors.txt中

nginx配置中,哪里需要跨域处理就 include一下就可以。这样也可以统一管理安全域名

  location /test {#引入跨域设置include /usr/local/nginx/conf/vhost/cors.txt;proxy_pass http://172.16.x.xx:8080;}

以上就是基本优化思路,简单实用 ~

http://www.lryc.cn/news/254448.html

相关文章:

  • 远离危险的购买手机的渠道
  • 外包干了2个多月,技术明显有退步了。。。。。
  • 【Java项目管理工具】Maven
  • solidity案例详解(六)服务评价合约
  • 使用kubeadm搭建高可用的K8s集群
  • C#图像处理OpenCV开发指南(CVStar,07)——通用滤波(Filter2D)的实例代码
  • c++函数模板STL详解
  • Java利用UDP实现简单群聊
  • fastapi.templating与HTMLResponse
  • 当初为什么选择计算机这类的行业?
  • tif文件转png、Excel
  • 【PyTorch】训练过程可视化
  • 深入理解Go语言GC机制
  • qt-C++笔记之组件-分组框QGroupBox
  • qt 定时器用法
  • 用23种设计模式打造一个cocos creator的游戏框架----(九)访问者模式
  • 根文件系统初步测试
  • 【精选】设计模式——策略设计模式-两种举例说明,具体代码实现
  • 外包干了3个月,技术倒退2年。。。
  • 微信小程序:chooseimage从本地相册选择图片或使用相机拍照
  • 「Swift」取消UITableView起始位置在状态栏下方开始
  • android高版本适配使用Tools.java
  • 面试官:说说webpack中常见的Loader?解决了什么问题?
  • 【蓝桥杯省赛真题50】Scratch智能计价器 蓝桥杯scratch图形化编程 中小学生蓝桥杯省赛真题讲解
  • 折半查找(数据结构实训)
  • AR助推制造业智能转型:实时远程协作与可视化引领生产创新
  • 【用unity实现100个游戏之18】从零开始制作一个类CSGO/CS2、CF第一人称FPS射击游戏——基础篇3(附项目源码)
  • sed 流式编辑器
  • Linux shell编程学习笔记33:type 命令
  • 【数据结构】—红黑树(C++实现)