spring mvc 跨域请求处理——spring 4.2 以上
Controller method CORS configuration
You can add to your @RequestMapping annotated handler method a @CrossOrigin annotation in order to enable CORS on it (by default @CrossOrigin allows all origins and the HTTP methods specified in the @RequestMapping annotation):
@RestController
@RequestMapping("/account")
public class AccountController {
	@CrossOrigin
	@RequestMapping("/{id}")
	public Account retrieve(@PathVariable Long id) {
		// ...
	}
	@RequestMapping(method = RequestMethod.DELETE, value = "/{id}")
	public void remove(@PathVariable Long id) {
		// ...
	}
}It is also possible to enable CORS for the whole controller:
@CrossOrigin(origins = "http://domain2.com", maxAge = 3600)
@RestController
@RequestMapping("/account")
public class AccountController {
	@RequestMapping("/{id}")
	public Account retrieve(@PathVariable Long id) {
		// ...
	}
	@RequestMapping(method = RequestMethod.DELETE, value = "/{id}")
	public void remove(@PathVariable Long id) {
		// ...
	}
}In this example CORS support is enabled for both retrieve() and remove() handler methods, and you can also see how you can customize the CORS configuration using@CrossOrigin attributes.
You can even use both controller and method level CORS configurations, Spring will then combine both annotation attributes to create a merged CORS configuration.
@CrossOrigin(maxAge = 3600)
@RestController
@RequestMapping("/account")
public class AccountController {
	@CrossOrigin(origins = "http://domain2.com")
	@RequestMapping("/{id}")
	public Account retrieve(@PathVariable Long id) {
		// ...
	}
	@RequestMapping(method = RequestMethod.DELETE, value = "/{id}")
	public void remove(@PathVariable Long id) {
		// ...
	}
}Global CORS configuration
In addition to fine-grained, annotation-based configuration you’ll probably want to define some global CORS configuration as well. This is similar to using filters but can be declared withing Spring MVC and combined with fine-grained @CrossOrigin configuration. By default all origins and GET, HEAD and POST methods are allowed.
JavaConfig
Enabling CORS for the whole application is as simple as:
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
	@Override
	public void addCorsMappings(CorsRegistry registry) {
		registry.addMapping("/**");
	}
}You can easily change any properties, as well as only apply this CORS configuration to a specific path pattern:
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
	@Override
	public void addCorsMappings(CorsRegistry registry) {
		registry.addMapping("/api/**")
			.allowedOrigins("http://domain2.com")
			.allowedMethods("PUT", "DELETE")
			.allowedHeaders("header1", "header2", "header3")
			.exposedHeaders("header1", "header2")
			.allowCredentials(false).maxAge(3600);
	}
}XML namespace
It is also possible to configure CORS with the mvc XML namespace.
This minimal XML configuration enable CORS on /** path pattern with the same default properties than the JavaConfig one:
<mvc:cors>
	<mvc:mapping path="/**" />
</mvc:cors>It is also possible to declare several CORS mappings with customized properties:
<mvc:cors>
	<mvc:mapping path="/api/**"
		allowed-origins="http://domain1.com, http://domain2.com"
		allowed-methods="GET, PUT"
		allowed-headers="header1, header2, header3"
		exposed-headers="header1, header2" allow-credentials="false"
		max-age="123" />
	<mvc:mapping path="/resources/**"
		allowed-origins="http://domain1.com" />
</mvc:cors>How does it work?
CORS requests (including preflight ones with an OPTIONS method) are automatically dispatched to the various HandlerMappings registered. They handle CORS preflight requests and intercept CORS simple and actual requests thanks to a CorsProcessor implementation (DefaultCorsProcessor by default) in order to add the relevant CORS response headers (likeAccess-Control-Allow-Origin). CorsConfiguration allows you to specify how the CORS requests should be processed: allowed origins, headers, methods, etc. It can be provided in various ways:
- AbstractHandlerMapping#setCorsConfiguration()allows to specify a- Mapwith severalCorsConfiguration mapped on path patterns like- /api/**
- Subclasses can provide their own CorsConfigurationby overridingAbstractHandlerMapping#getCorsConfiguration(Object, HttpServletRequest)method
- Handlers can implement CorsConfigurationSourceinterface (likeResourceHttpRequestHandlernow does) in order to provide a CorsConfiguration for each request.
spring mvc 跨域请求处理——spring 4.2 以上的更多相关文章
- 关于Spring MVC跨域
		1.Sping MVC 3.X跨域 关于跨域问题,主要用的比较多的是cros跨域. 详细介绍请看https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Ac ... 
- spring mvc 跨域问题。。。解决
		官方推荐方式: http://spring.io/blog/2015/06/08/cors-support-in-spring-framework 方式1: $.ajax({ //前台:常规写法.注意 ... 
- spring mvc跨域设置(全局)
		//--------------第一步//spring 5版本全局配置方式 @Configuration @EnableWebMvc public class SpringMvcBeans imple ... 
- spring mvc跨域(ajax post json)--filter方案
		@RequestMapping(value = "/login.do",method = RequestMethod.POST) public Message login(Http ... 
- spring mvc跨域(post)--filter方案
		import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.Http ... 
- spring boot跨域请求访问配置以及spring security中配置失效的原理解析
		一.同源策略 同源策略[same origin policy]是浏览器的一个安全功能,不同源的客户端脚本在没有明确授权的情况下,不能读写对方资源. 同源策略是浏览器安全的基石. 什么是源 源[orig ... 
- spring boot 跨域请求
		场景 网站localhost:56338要访问网站localhost:3001的服务 在网站localhost:3001中增加CORS相关Java Config @Configuration @Ord ... 
- SpringBoot 优雅配置跨域多种方式及Spring Security跨域访问配置的坑
		前言 最近在做项目的时候,基于前后端分离的权限管理系统,后台使用 Spring Security 作为权限控制管理, 然后在前端接口访问时候涉及到跨域,但我怎么配置跨域也没有生效,这里有一个坑,在使用 ... 
- Spring MVC 3.0.5+Spring 3.0.5+MyBatis3.0.4全注解实例详解(二)
		在上一篇文章中我详细的介绍了如何搭建maven环境以及生成一个maven骨架的web项目,那么这章中我将讲述Spring MVC的流程结构,Spring MVC与Struts2的区别,以及例子中的一些 ... 
随机推荐
- Git -- 分支管理简介
			分支就是科幻电影里面的平行宇宙,当你正在电脑前努力学习Git的时候,另一个你正在另一个平行宇宙里努力学习SVN. 如果两个平行宇宙互不干扰,那对现在的你也没啥影响.不过,在某个时间点,两个平行宇宙合并 ... 
- 我的openwrt开发相关文章
			openwrt学习笔记: 在openwrt的学习过程中,走了非常多的弯路.一直以来有个期盼.希望能够出个简易教程,希望openwrt的同仁们能够更加高速的入手. . openwrt学习笔记(三十二): ... 
- T4生成实体,单张表和多张表
			# 使用说明 . 把T4目录放在项目的根目录 . 配置连接字符串,在tt文件的最底部 . generateAllEntity.tt生成全部的实体类 . generateSingleEntityByNa ... 
- u3d一个GameObject绑定两个AudioSource
			u3d 一个GameObject绑定两个AudioSource ,使他们分别播放,并控制 using UnityEngine; using System.Collections; public cl ... 
- uva 548 Tree(通过后序,先序重建树+dfs)
			难点就是重建树,指针參数的传递今天又看了看.应该是曾经没全然弄懂.昨天真没效率,还是不太专心啊.以后一定得慢慢看.不能急躁,保持寻常心,. 分析: 通过兴许序列和中序序列重建树,用到了结构体指针.以及 ... 
- LoadRunner做性能测试 从设计到分析执行
			项目简介:像百度知道系统类似的系统性能测试,是公司的自己产品. 对最近这个系统的性能测试进行总结下: 系统功能介绍: 前台用户可以根据自己的需要对不同的区域提问,提问包括匿名和登陆用户提问 后台不同区 ... 
- Java编程思想学习笔记——类型信息
			前言 运行时类型信息(RTTI:Runtime Type Information)使得我们可以在程序运行时发现和使用类型信息. Java在运行时识别对象和类的信息的方式: (1)一种是RTTI,它假定 ... 
- vue如何加入百度联盟广告
			在百度联盟代码位管理中创建好对应的代码位之后,点击获取代码,会看到这样一段js 直接复制粘贴到自己网页中便可显示对应广告. 在vue中由于都是vue组件,不支持直接在组件中加入这样一段一段的js代码, ... 
- Visual Studio快捷键大全
			快捷键的使用可以简化大家的操作,在一定程度上提高工作的效率,下文中将为大家介绍一些VS中经常用到的快捷键,希望对大家有用. 方法/步骤 关于解决方案和项目 用于快速跳转 用于代码的文本编辑 ... 
- CorelDRAW中如何分布对象
			分布对象功能主要用来控制选择对象之间的距离,可以满足用户对均匀间距的要求,通常用于选择三个或三个以上的物体,将他们之间的距离平均分布.本教程将详解CorelDRAW中关于分布对象的操作. CorelD ... 
