Spring提供了三种方式跨域

1、CorsFilter 过滤器

2、<mvc:cors> Bean(全局,推荐使用)

3、@CrossOrigin注解

以上三种方式本质都是用来配置CorsConfiguration

1、CorsFilter 过滤器

首先创建一个自己的过滤器

这里我创建一个类,名字为MyCorsFilter

import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter; import java.util.Collections; public class MyCorsFilter extends CorsFilter {
public MyCorsFilter() {
super(configurationSource());
}
private static UrlBasedCorsConfigurationSource configurationSource(){ UrlBasedCorsConfigurationSource source=new UrlBasedCorsConfigurationSource();
CorsConfiguration corsConfiguration=new CorsConfiguration();
/*
*允许跨域的地址
*http://127.0.0.1:8848*/
corsConfiguration.setAllowedOrigins(Collections.singletonList("http://127.0.0.1:8848"));
corsConfiguration.setAllowCredentials(true);
//允许跨域的路径
source.registerCorsConfiguration("/tagc",corsConfiguration);
return source;
}
}

创建好后,进行在web.xml文件进行配置

<filter>
<filter-name>cors</filter-name>
<!--过滤器地址-->
<filter-class>com.wbg.maven1128.controller.MyCorsFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>cors</filter-name>
<!--拦截所有-->
<url-pattern>/*</url-pattern>
</filter-mapping>

2、<mvc:cors> Bean(全局,推荐使用)

在spring-web里面进行配置

    <mvc:cors>
<mvc:mapping path="/taga"
allowed-origins="http://127.0.0.1:8020"
allowed-methods="POST,GET, OPTIONS,DELETE,PUT"
allowed-headers="Content-Type,ContentType,Access-Control-Allow-Headers, Authorization, X-Requested-With"
allow-credentials="true"/>
</mvc:cors>

3、@CrossOrigin注解

定义在类上,所有方法有效,@CrossOrigin不带参数就是默认

定义在方法上,该方法有效

测试页面

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<script src="http://code.jquery.com/jquery.js"></script>
<body>
<button onclick="aa() ">开始</button>
<script> function aa(){
if(window.confirm("是否发送?")){ fetch("http://localhost:8080/taga",{
method:"get"
}).then(function(res){
resp.text().then(function(data){
console.log(data);
});
})
}
else {
fetch("http://localhost:8080/tagb",{
method:"get"
}).then(function(res){
res.text().then(function(data){
console.log(data);
}) })
}
}
</script>
</body>
</html>

跨域

spring 跨域 CORS (Cross Origin Resources Share) 跨域的更多相关文章

  1. CORS (Cross Origin Resources Share) 跨域

    CORS 跨域 1 什么是跨域问题 基于安全考虑,浏览器会限制使用脚本发起任何跨域请求. 所谓的跨域请求,就是与当前页面的 http/ip/port 不一样的请求. 但在实际运用中,跨域获取数据的需求 ...

  2. Ajax本地跨域问题 Cross origin requests are only supported for HTTP

    问题:打开本地html文件时,报错如下 Cross origin requests are only supported for protocol schemes: http, data,chrome ...

  3. 本地数据访问时出现跨域问题Cross origin requests are only supported for protocol schemes: ……

    从桌面找到Chrome图标,右键属性,快捷方式,起始位置(安装路径) 注:在cmd中访问Program Files文件的方法 %ProgramFiles%=C:\Program Files %Prog ...

  4. Node.js 【CORS(cross origin resource sharing) on ExpressJS之笔记】

    app.use(function(req, res, next) { res.header("Access-Control-Allow-Origin", "*" ...

  5. 跨域问题:Cross origin requests are only supported for protocol schemes: http...

    跨域:Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extensi ...

  6. Spring MVC配置CORS(解决跨域请求)

    1. CORS 简介 同源策略(same origin policy)是浏览器安全的基石.在同源策略的限制下,非同源的网站之间不能发送 ajax 请求的. 为了解决这个问题,w3c 提出了跨源资源共享 ...

  7. Spring MVC学习总结(10)——Spring MVC使用Cors跨域

    跨站 HTTP 请求(Cross-site HTTP request)是指发起请求的资源所在域不同于该请求所指向资源所在的域的 HTTP 请求.比如说,域名A(http://domaina.examp ...

  8. Spring Boot 通过CORS实现跨域

    同源策略 很多人对跨域有一种误解,以为这是前端的事,和后端没关系,其实不是这样的,说到跨域,就不得不说说浏览器的同源策略. 同源策略是由 Netscape 提出的一个著名的安全策略,它是浏览器最核心也 ...

  9. spring boot:解决cors跨域问题的两种方法(spring boot 2.3.2)

    一,什么是CORS? 1,CORS(跨域资源共享)(CORS,Cross-origin resource sharing), 它是一个 W3C 标准中浏览器技术的规范, 它允许浏览器向非同一个域的服务 ...

随机推荐

  1. Java Swing笔记

    想到了解一下GUI主要是想用来做点小工具,记录一些笔记. 文本框自动换行和滚动条 private static JTextArea addJTextArea(JPanel panel, int x, ...

  2. 【Linux】修改ubuntu默认字符集

    今天把以前的项目移植到linux上了,我装的是ubuntu,web服务器是tomcat,发现用freemark模板生成的静态页面全 乱码了,在windows都是正常的,猜想可能是linux字符集的问题 ...

  3. nginx配置多域名

    http{ # 第一个虚拟主机 server { listen 80; server_name aaa.domain.com; #access_log logs/host.access.log mai ...

  4. 用R处理不平衡的数据

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文来自云+社区翻译社,作者ArrayZoneYour 在分类问题当中,数据不平衡是指样本中某一类的样本数远大于其他的类别样本数.相比于多分 ...

  5. ContextCapture水面约束(水面破洞修复)

      [问题描述] 对于水面而言,由于特征点较少,软件在计算时很难匹配正确,导致输出模型的水面通常是支离破碎的.软件针对这种情况提供了一个约束工具,用户手动的为水面添加平面约束后,输出的水面模型就会非常 ...

  6. Windows与Unix思想

    Unix与Windows的思想 Unix中的哲学是"一切皆文件",这里的一切皆文件是一个广泛的概念,有一些特殊的设备文件,在/dev目录下 物理设备在Unix中就对应一个特殊的设备 ...

  7. HDU 5318——The Goddess Of The Moon——————【矩阵快速幂】

    The Goddess Of The Moon Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/ ...

  8. 设计模式之职责链模式(JAVA实现)

    学习netty框架时,看到有人说netty用到了设计模式的职责链模式,学习一下职责链模式,主要参考大话设计模式. 主要场景: 小菜想要加薪,向经理提出加薪请求,经理没有权限,经理交由总监处理,总监也没 ...

  9. scss-@mixin

    @mixin指令用于定义混入,它包括任选的变量和参数中的mixin名称后. scss简单示例: @mixin style { .cont{ color: #77C1EF; } } @include s ...

  10. Objective C 中的BOOL, bool, Boolean理解

    一. 1.类型不同 BOOL为int型 bool为布尔型 2.长度不同 bool只有一个字节 BOOL长度视实际环境来定,一般可认为是4个字节 3.取值不同 bool取值false和true,是0和1 ...