跨域配置

SpringBoot跨域配置

我们的后端使用Spring Boot。Spring Boot跨域非常简单,只需书写以下代码即可。

@Configuration
public class CustomCORSConfiguration {private CorsConfiguration buildConfig() {CorsConfiguration corsConfiguration = new CorsConfiguration();corsConfiguration.addAllowedOrigin("*");corsConfiguration.addAllowedHeader("*");corsConfiguration.addAllowedMethod("*");return corsConfiguration;}@Beanpublic CorsFilter corsFilter() {UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();source.registerCorsConfiguration("/**", buildConfig());return new CorsFilter(source);}
}

Nginx跨域配置

某天,我们将Spring Boot应用用Nginx反向代理。而前端跨域请求的需求不减,于是乎。

Nginx跨域也比较简单,只需添加以下配置即可。

location / {proxy_pass http://localhost:8080;if ($request_method = 'OPTIONS') {add_header 'Access-Control-Allow-Origin' '*';add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range,Token';add_header 'Access-Control-Max-Age' 1728000;add_header 'Content-Type' 'text/plain; charset=utf-8';add_header 'Content-Length' 0;return 204;}if ($request_method = 'POST') {add_header 'Access-Control-Allow-Origin' '*';add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range,Token';add_header 'Access-Control-Expose-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range,Token';}if ($request_method = 'GET') {add_header 'Access-Control-Allow-Origin' '*';add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range,Token';add_header 'Access-Control-Expose-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range,Token';}
}

其中: add_header 'Access-Control-Expose-Headers' 务必加上你请求时所带的header。例如本例中的“Token”,其实是前端传给后端过来的。如果记不得也没有关系,浏览器的调试器会有详细说明。

参考文档:https://enable-cors.org/server_nginx.html

B.T.W,阿里云中文档描述到Nginx也可通过crossdomain.xml配置文件跨域:https://helpcdn.aliyun.com/knowledge_detail/41123.html ,不过笔者并未采用这种方式。

CORS on Nginx

The following Nginx configuration enables CORS, with support for preflight requests.

#
# Wide-open CORS config for nginx
#
location / {if ($request_method = 'OPTIONS') {add_header 'Access-Control-Allow-Origin' '*';add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';## Custom headers and headers various browsers *should* be OK with but aren't#add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';## Tell client that this pre-flight info is valid for 20 days#add_header 'Access-Control-Max-Age' 1728000;add_header 'Content-Type' 'text/plain; charset=utf-8';add_header 'Content-Length' 0;return 204;}if ($request_method = 'POST') {add_header 'Access-Control-Allow-Origin' '*';add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';add_header 'Access-Control-Expose-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';}if ($request_method = 'GET') {add_header 'Access-Control-Allow-Origin' '*';add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';add_header 'Access-Control-Expose-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';}
}

浏览器设置跨域

Chrome、Firefox本身是可以通过配置支持跨域请求的。

  • Chrome跨域:参考文档:http://www.cnblogs.com/laden666666/p/5544572.html
  • Firefox跨域:参考文档:https://segmentfault.com/q/1010000002532581/a-1020000002533699

转载于:https://www.cnblogs.com/alterem/p/11227200.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/392326.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

fromEvent

fromEvent(selector,Event) 实际效果图 这个功能和cad 3dmax里面的鼠标定位功能一致吧,是不是有点小成就? 转载于:https://www.cnblogs.com/xiongwei2017/p/7074180.html

java开发第一天上班_从第一天开始,如何成为一名优秀的团队合作伙伴,成为初级开发人员

java开发第一天上班One of the many things you might be asking yourself when starting your software development career is:在开始软件开发职业时,您可能会问自己很多事情之一: “How do I REALLY contribute to my new team?”“我如何真正为我的…

java虚拟机编译文件,理解Java虚拟机(1)之一个.java文件编译成.class文件发生了什么...

理解Java虚拟机(1)之一个.java文件编译成.class文件发生了什么最近在看《深入理解Java虚拟机》弄明白了很多java的底层知识,决定分几部分总结下,从.java文件编译,到类加载机制,内存分配垃圾回收机制,线程并发&#xff…

leetcode 684. 冗余连接()

在本问题中, 树指的是一个连通且无环的无向图。 输入一个图,该图由一个有着N个节点 (节点值不重复1, 2, …, N) 的树及一条附加的边构成。附加的边的两个顶点包含在1到N中间,这条附加的边不属于树中已存在的边。 结果图是一个以边组成的二维数组。每一…

Go-如何读取yaml,json,ini等配置文件

1. json使用 JSON 应该比较熟悉,它是一种轻量级的数据交换格式。层次结构简洁清晰 ,易于阅读和编写,同时也易于机器解析和生成。 创建 conf.json:{"enabled": true,"path": "/usr/local" }新建conf…

SQL转化为MapReduce的过程

转载:http://www.cnblogs.com/yaojingang/p/5446310.html 在了解了MapReduce实现SQL基本操作之后,我们来看看Hive是如何将SQL转化为MapReduce任务的,整个编译过程分为六个阶段: Antlr定义SQL的语法规则,完成SQL词法&am…

使用集合映射和关联关系映射_使用R进行基因ID映射

使用集合映射和关联关系映射Inter-conversion of gene ID’s is the most important aspect enabling genomic and proteomic data analysis. There are multiple tools available each with its own drawbacks. While performing enrichment analysis on Mass Spectrometry da…

leetcode 1018. 可被 5 整除的二进制前缀

给定由若干 0 和 1 组成的数组 A。我们定义 N_i:从 A[0] 到 A[i] 的第 i 个子数组被解释为一个二进制数(从最高有效位到最低有效位)。 返回布尔值列表 answer,只有当 N_i 可以被 5 整除时,答案 answer[i] 为 true&…

纯java应用搭建,16、BoneCp纯java项目使用

2、代码实现 package com.study;import com.jolbox.bonecp.BoneCP;import com.jolbox.bonecp.BoneCPConfig;import com.jolbox.bonecp.BoneCPDataSource;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import java.sql.*;/*** Boncp 纯java处理* CreateTime 2018/3/…

数据结构与算法深入学习_我最喜欢的免费课程,用于深入学习数据结构和算法...

数据结构与算法深入学习by javinpaul由javinpaul Data structures and algorithms are some of the most essential topics for programmers, both to get a job and to do well on a job. Good knowledge of data structures and algorithms is the foundation of writing go…

RabbitMQ学习系列(一): 介绍

1、介绍 RabbitMQ是一个由erlang开发的基于AMQP(Advanced Message Queue )协议的开源实现。用于在分布式系统中存储转发消息,在易用性、扩展性、高可用性等方面都非常的优秀。是当前最主流的消息中间件之一。 RabbitMQ的官网:http…

详尽kmp_详尽的分步指南,用于数据准备

详尽kmp表中的内容 (Table of Content) Introduction 介绍 What is Data Preparation 什么是数据准备 Exploratory Data Analysis (EDA) 探索性数据分析(EDA) Data Preprocessing 数据预处理 Data Splitting 数据分割 介绍 (Introduction) Before we get into this, I want to …

leetcode 947. 移除最多的同行或同列石头(dfs)

n 块石头放置在二维平面中的一些整数坐标点上。每个坐标点上最多只能有一块石头。 如果一块石头的 同行或者同列 上有其他石头存在,那么就可以移除这块石头。 给你一个长度为 n 的数组 stones ,其中 stones[i] [xi, yi] 表示第 i 块石头的位置&#x…

matlab距离保护程序,基于MATLAB的距离保护仿真.doc

基于MATLAB的距离保护仿真摘要:本文阐述了如何利用Matlab中的Simulink及SPS工具箱建立线路的距离保护仿真模型,并用S函数编制相间距离保护和接地距离保护算法程序,构建相应的保护模块,实现了三段式距离保护。仿真结果表明&#xf…

ZOJ3385 - Hanami Party (贪心)

题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode3385 题目大意: 妖梦要准备一个party,所以需要许多食物,初始化妖梦的烹饪技能为L,每天妖梦有两种选择,一是选择当天做L个食物&am…

sklearn.fit_两个小时后仍在运行吗? 如何控制您的sklearn.fit。

sklearn.fitby Nathan Toubiana内森图比亚纳(Nathan Toubiana) 两个小时后仍在运行吗? 如何控制您的sklearn.fit (Two hours later and still running? How to keep your sklearn.fit under control) Written by Gabriel Lerner and Nathan Toubiana加布里埃尔勒纳…

RabbitMQ学习系列(二): RabbitMQ安装与配置

1.安装 Rabbit MQ 是建立在强大的Erlang OTP平台上,因此安装RabbitMQ之前要先安装Erlang。 erlang:http://www.erlang.org/download.html rabbitmq:http://www.rabbitmq.com/download.html 注意: 1.现在先别装最新的 3…

帝国CMS浅浅滴谈一下——博客园老牛大讲堂

封笔多月之后,工作中遇到了很多很多的问题,也解决了一些问题,下面我把一些得出的经验,分享给大家! 会帝国cms的请离开,这篇文章对你没什么用 1、什么是帝国CMS?---博客园老牛大讲堂 多月之前&am…

matlab cdf,Matlab 简单计算PDF和CDF | 学步园

通信的魅力就是在于随机性中蕴含的确定性,这也就是为什么你随便拿出一本通信方面的教材,前面几章都会大篇幅的讲解随机过程,随机过程也是研究生必须深入了解的一门课,特别是对于信号处理以及通信专业的学生。在实际工作中&#xf…

leetcode 1232. 缀点成线

在一个 XY 坐标系中有一些点,我们用数组 coordinates 来分别记录它们的坐标,其中 coordinates[i] [x, y] 表示横坐标为 x、纵坐标为 y 的点。 请你来判断,这些点是否在该坐标系中属于同一条直线上,是则返回 true,否则…