使用Spring CROS解决项目中的跨域问题
CROS(Cross-Origin Resource Sharing) 用于解决浏览器中跨域请求的问题。简单的Get请求可以使用JSONP来解决,而对于其它复杂的请求则需要后端应用的支持CROS。Spring在4.2版本之后提供了@CrossOrigin 注解来实现对Cross的支持。
- 在Controller方法上配置
@CrossOrigin(origins = {"http://loaclhost:8088"})
@RequestMapping(value = "/crossTest",method = RequestMethod.GET)
public String greeting() {
return "corss test";
}
- 在Controller上配置,那么这个Controller中的所有方法都会支持CORS
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@CrossOrigin(origins = "http://localhost:8088",maxAge = 3600)
@Controller
@RequestMapping("/api")
public class AppController {
@RequestMapping(value = "/crossTest",method = RequestMethod.GET)
public String greeting() {
return "corss test";
}
}
- Java Config全局配置
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
@EnableWebMvc
public class SpringWebConfig extends WebMvcConfigurerAdapter {
/**
* {@inheritDoc}
* <p>This implementation is empty.
*
* @param registry
*/
@Override
public void addCorsMappings(CorsRegistry registry) {
super.addCorsMappings(registry);
// 对所有的URL配置
registry.addMapping("/**");
// 针对某些URL配置
registry.addMapping("/api/**").allowedOrigins("http:///localhost:8088")
.allowedMethods("PUT","DELETE")
.allowedHeaders("header1","header2","header3")
.exposedHeaders("header1","header2")
.allowCredentials(false).maxAge(3600);
}
}
- XML全局配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<mvc:cors>
<!--<mvc:mapping path=""/>-->
<mvc:mapping path="/api/**"
allowed-origins="http://localhost:8088,http://localhost:8888"
allowed-methods="GET,PUT"
allowed-headers="header1,header2"
exposed-headers="header1,header2"
allow-credentials="false"
max-age="3600" />
</mvc:cors>
</beans>
使用Spring CROS解决项目中的跨域问题的更多相关文章
- 关于vue项目中axios跨域的解决方法(开发环境)
1.在config文件中修改index.js proxyTable: { "/api":{ target: 'https://www.baidu.com/muc/',//你需要跨域 ...
- Vue+webpack项目中实现跨域的http请求
目前Vue项目中对json数据的请求一般使用两个插件vue-resource和axios, 但vue-resource已经不再维护, 而axios是官方推荐的且npm下载量已经170多万,github ...
- web项目中的跨域问题解决方法
一种是JSONP 一种是 CORS. 在客户端Javascript调用服务端接口的时候,如果需要支持跨域的话,需要服务端支持. JSONP的方式就是服务端对返回的值进行回调函数包装,他的优点是支持众多 ...
- 08 Django REST Framework 解决前后端分离项目中的跨域问题
01-安装模块 pip install django-cors-headers 02-添加到INSTALL_APPS中 INSTALLED_APPS = ( ... 'corsheaders', .. ...
- vue项目中的跨域源请求拦截问题CORS头缺少'Access-Control-Allow-Origin'
这里使用的是axios发请求出现的. 访问的api接口是: 在不同域之间访问是比较常见,在本地调试访问远程服务器....这就是有域问题. VUE解决通过proxyTable 解决办法: 1.检查请求方 ...
- django项目中关于跨域CORS
1.使用django-cors-headers扩展,但首先进行安装 2.在配置中添加应用 3.在中间层中设置:“corsheaders.middleware.CorsMiddleware” 4.添加C ...
- vue项目中设置跨域
config->index.js 'use strict' // Template version: 1.3.1 // see http://vuejs-templates.github.io/ ...
- Springboot中关于跨域问题的一种解决方法
前后端分离开发中,跨域问题是很常见的一种问题.本文主要是解决 springboot 项目跨域访问的一种方法,其他 javaweb 项目也可参考. 1.首先要了解什么是跨域 由于前后端分离开发中前端页面 ...
- 2019-03-26 SpringBoot项目部署遇到跨域问题,记录一下解决历程
近期SpringBoot项目部署遇到跨域问题,记录一下解决历程. 要严格限制,允许哪些域名访问,在application.properties文件里添加配置,配置名可以自己起: cors.allowe ...
随机推荐
- volatile 关键词
volatile 关键字指示一个字段可以由多个同时执行的线程修改. 出于性能原因,编译器,运行时系统甚至硬件都可能重新排列对存储器位置的读取和写入. 声明了 volatile 的字段不进行这些优化.这 ...
- BZOJ 4919: [Lydsy1706月赛]大根堆 set启发式合并
这个和 bzoj 5469 几乎是同一道题,但是这里给出另一种做法. 你发现你要求的是一个树上 LIS,而序列上的 LIS 有一个特别神奇的 $O(n\log n) $ 做法. 就是维护一个单调递增的 ...
- python基础知识总结大全(转载)
- UOJ46. 【清华集训2014】玄学 [线段树,二进制分组]
UOJ 思路 模拟赛出了这题,结果我没学过二进制分组--一波主席树然后空间就爆炸了-- 用线段树维护时间序列,每个节点维护\(a_i\to x_i\times a_i+b_i,i\in [1,n]\) ...
- Automatic Annotation of Airborne Images by Label Propagation Based on a Bayesian-CRF Model
贝叶斯+全连接条件场,无人机和航片数据,通过标注航片数据自动生成无人机标注数据,具体不懂
- 去参加了十四届D2前端大会~
朋友喊我去一起去d2,原来一直在加班,没有想去的动力,后来业务上线,幸运的入手了别人转的一张票(也不便宜啊)- 讲了五个挑战 端侧渲染体系的重塑,从PC时代到无线时代,再到未来的IOT时代,在渲染方面 ...
- (转载)ranger原理
文章目录 一.业务背景 现状&&需求 二.大数据安全组件介绍与对比 1.Kerberos 2.Apache Sentry 3.Apache Ranger 4.为什么我们选择Ranger ...
- dedecms 织梦二级菜单的调用
<ul class="nav navbar-nav navlist"> <li class='nav-item'> <a href="htt ...
- 剑指offer:链表中环的入口结点
题目描述: 给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null. 思路分析: 这道题首先需要判断链表是否存在环,很快就能想到用快慢指针来判断. 由于快慢指针的相遇位置并不一定为链 ...
- python 把带小数的浮点型字符串转换为整数的解决方案
以下内容在python中完全可以接受: 将整数的字符串表示形式传递给 int 将float的字符串表示形式传递给 float 但是,如果你将float型的字符串传递给int将会得到错误. >&g ...