当前位置:网站首页>跨域和处理跨域
跨域和处理跨域
2022-07-17 05:09:00 【码赛客1024】
一、跨域的概念
在讨论跨域之前,我们先来说一下什么是:同源策略。
看下面这个URL地址:
该URL由:协议,IP,端口等部分组成,如果他的协议,IP和端口3者都一样我们就可以称之为是同源,有一个不一样就不是同源,即:跨域,也就是跨域访问,默认这是不被允许的。那为什么会有同源策略呢?主要是浏览器对JavaScript实施的一种安全限制,防止跨站执行JS脚本引发安全问题。
来看一组URL做个练习:
| URL1 | URL2 | 是否跨域 |
|---|---|---|
| http://localhost:8080 | https://localhost:8080 | 跨域(协议不一致) |
| http://176.198.100.1:8080 | http://176.198.100.2:8080 | 跨域(ip不一致) |
| http://localhost:8080 | http://localhost:80 | 跨域(端口不一致) |
| http://localhost:8080/index | http://localhost:8080/test | 同源 |
| http://www.jd.com | http://img.jd.com | 跨域(父域名相同,子域名不同) |
| http://www.baidu.com | http://14.215.177.38,假设百度ip就是14.215.177.38 | 跨域(域名访问ip也是跨域) |
| http://localhost:8080 | http://127.0.0.1:8080 | 跨域 |
二、处理跨域
知道了什么是跨域,那如何处理跨域呢? 毕竟咱们平时开发中确实会存在使用JS发送ajax进行跨域的请求。
1.jsonp处理跨域
比较老旧的技术,本质是利用script标签的src属性来时实现请求的发送,因为src属性是能够跨域访问的。如下是代码案例:
服务1(端口80):
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JSONP测试</title>
</head>
<body>
<h1>测试跨域</h1>
<script> function test(data){
console.log(data) } </script>
<script src="http://localhost:8090/jsonp?methodName=test"></script>
<!-- 该script标签请求后服务端返回的是: test(123),而script会把返回结果当成脚本来执行。 相当于:<script> test(123) </script> ,这里的test()称为回调函数 服务端还可以把想返回的数据传回来。 以此通过script标签实现跨域请求从而获取到服务端的数据。 -->
</body>
</html>
服务2(端口8090):
@RestController
public class JsonpController {
@RequestMapping("/jsonp")
public String testJsonp(String methodName) {
//data 为要模拟的返回数据
int data = 123;
return methodName+"("+data+")";
}
}
2.CORS处理跨域(推荐)
CORS 是跨域资源分享(Cross-Origin Resource Sharing)的缩写。它是 W3C 标准,属于跨源 AJAX 请求的根本解决方法。
CORS处理跨域代码很简单,只需服务器端设置Access-Control-Allow-Origin 即可。如果浏览器检测到相应的设置,就可以允许Ajax进行跨域的访问。
@Configuration
public class CorsConfig {
@Bean
public CorsFilter corsFilter() {
//1.添加CORS配置信息
CorsConfiguration config = new CorsConfiguration();
//1) 允许的域
//config.addAllowedOrigin("*"); //*表示所有,但是这样cookie就无法使用了
config.addAllowedOrigin("http://127.0.0.1:8090"); //允许http://127.0.0.1:8090来访问我
//2) 是否发送Cookie信息
config.setAllowCredentials(true);
//3) 允许的请求方式,*表示所有
//config.addAllowedMethod("*");
config.addAllowedMethod("OPTIONS");
config.addAllowedMethod("HEAD");
config.addAllowedMethod("GET");
config.addAllowedMethod("PUT");
config.addAllowedMethod("POST");
config.addAllowedMethod("DELETE");
config.addAllowedMethod("PATCH");
// 4)允许的头信息
config.addAllowedHeader("*");
//2.添加映射路径,我们拦截一切请求
UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();
configSource.registerCorsConfiguration("/**", config);
return new CorsFilter(configSource);
}
}
边栏推荐
- 分布式注册中心-etcd
- Two or three things to know about operation and maintenance safety
- Questions d'entrevue courantes du système d'exploitation
- 从20s优化到500ms,我用了这三招
- 线程池如何监控,才能帮助开发者快速定位线上错误?
- A comprehensive performance optimization, from 5 seconds to 1 second
- [first launch in the whole network] automatic analysis of JVM performance problems
- MySQL安装配置教程(超级详细)
- redis 源码分析3 间断遍历的实现
- List与Map
猜你喜欢

1.东软跨境电商数仓需求规格说明文档

Talk about 12 business scenarios of concurrent programming

7.数据仓库搭建之数据仓库环境准备

BUUCTF web WarmUp

Minor problems of GCC compiling C language in ubantu

聊聊写代码的20个反面教材

Data visualization
![[first launch in the whole network] one month later, we switched from MySQL dual master to master-slave](/img/2f/b894569c8d13c9c18fd201c12a5d07.png)
[first launch in the whole network] one month later, we switched from MySQL dual master to master-slave

Solve the problem of inconsistent prediction effect between text detection training model and information model based on paddleocr

Router loopback port experiment
随机推荐
Swagger configuration and use
在 CDP中使用Iceberg 为数据湖仓增压
【函数的效率】
Redis source code analysis - data structure and Implementation (Dictionary dict)
MySQL -- storage and cursor
mysql的事务
Redis source code analysis dynamic string implementation (SDS)
2020-11-10
mysql 缓存策略和解决方案
GoFrame 错误处理的常用方法&错误码的使用
C语言&位域
MySQL安装配置教程(超级详细)
特殊指针的一些应用
Online software testing training institutions lemon class and itest AI platform achieves strategic cooperation
Common (Consortium)
Some applications of special pointers
From 20s to 500ms, I used these three methods
【全网首发】主线程异常会导致 JVM 退出?
A comprehensive performance optimization, from 5 seconds to 1 second
Three methods for cesium to obtain the longitude and latitude at the mouse click