出于安全原因,浏览器禁止AJAX调用当前域之外的域的资源。跨源资源共享(CORS)是由大多数浏览器实现的W3C规范,允许您指定哪些类型的跨域请求是被授权的,而不是基于IFRAME或JSONP的不安全且功能较差的工作区。
Spring MVC HandlerMapping提供了对CORS的内置支持。在成功地将请求映射到处理程序之后,HandlerMapping将检查给定请求和处理程序的CORS配置,并采取进一步的操作。
为了启用跨源请求(例如,源头是存在的,并且与请求的主机不同),您需要有一些显式声明的CORS配置。如果没有找到匹配的CORS配置,则拒绝请求前请求。在简单和实际的CORS请求的响应中没有添加CORS头,因此浏览器拒绝它们。
每个HandlerMapping都可以通过基于URL模式的Cors映射单独配置。在大多数情况下,应用程序将使用MVC Java config或XML名称空间来声明这样的映射。
在HandlerMapping级别上的全局CORS配置可以与更细粒度的、handler级别的CORS配置相结合。例如,带注解的控制器可以使用类或方法级的@CrossOrigin注解。
@CrossOrigin注解的使用
| @RestController @RequestMapping("/account") public class AccountController { @CrossOrigin @GetMapping("/{id}") public Account retrieve(@PathVariable Long id) { // ... } @DeleteMapping("/{id}") public void remove(@PathVariable Long id) { // ... } } |
默认情况下@CrossOrigin允许:
- 所有的域。
- 所有的headers
- 将控制器方法映射到的所有HTTP方法。
- maxAge设置为30分钟。
类级别支持某一特定的域
| @CrossOrigin(origins = "http://domain2.com", maxAge = 3600) @RestController @RequestMapping("/account") public class AccountController { @GetMapping("/{id}") public Account retrieve(@PathVariable Long id) { // ... } @DeleteMapping("/{id}") public void remove(@PathVariable Long id) { // ... } } |
类和方法上都使用
|
@CrossOrigin(maxAge = 3600) @RestController @RequestMapping("/account") public class AccountController { @CrossOrigin("http://domain2.com") @GetMapping("/{id}") public Account retrieve(@PathVariable Long id) { // ... } @DeleteMapping("/{id}") public void remove(@PathVariable Long id) { // ... } } |
Spring JAVA Configuration中 全局配置
| @Configuration @EnableWebMvc public class WebConfig implements WebMvcConfigurer { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/api/**") .allowedOrigins("http://domain2.com") .allowedMethods("PUT", "DELETE") .allowedHeaders("header1", "header2", "header3") .exposedHeaders("header1", "header2") .allowCredentials(true).maxAge(3600); // Add more mappings... } } |
在SpringBoot中的应用
| package org.niugang.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; /** * * @Description:跨域访问配置 * @Project:boot-sis * @File:CORSMyConfiguration.java * @Package:org.niugang.config * @Date:2018年7月12日下午10:22:10 * @author:niugang * @Copyright (c) 2018, [email protected] All Rights Reserved. * */ @Configuration public class CORSConfiguration { @Bean public WebMvcConfigurer corsConfigurer() { return new WebMvcConfigurerAdapter() { @Override public void addCorsMappings(CorsRegistry registry) { //addMapping 跨域所能访问的路径 //allowedOrigins:那些域可以访问,默认为任何域都可以访问 registry.addMapping("/api/**").allowedOrigins("*"); } }; } } |
在SpringSecurity中的应用
| @EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http // by default uses a Bean by the name of corsConfigurationSource //默认配置一个Bean Name为corsConfigurationSource .cors().and() ... } //配置那些域可以访问的我的资源 @Bean CorsConfigurationSource corsConfigurationSource() { CorsConfiguration configuration = new CorsConfiguration(); configuration.setAllowedOrigins(Arrays.asList("https://example.com")); configuration.setAllowedMethods(Arrays.asList("GET","POST")); UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); source.registerCorsConfiguration("/**", configuration); return source; } } |
- 配置跨域 代码和上面一样
| package org.niugang.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; /** * * @Description:跨域访问配置 * @Project:boot-sis * @File:CORSMyConfiguration.java * @Package:org.niugang.config * @Date:2018年7月12日下午10:22:10 * @author:niugang * @Copyright (c) 2018, [email protected] All Rights Reserved. * */ @Configuration public class CORSConfiguration { @Bean public WebMvcConfigurer corsConfigurer() { return new WebMvcConfigurerAdapter() { @Override public void addCorsMappings(CorsRegistry registry) { //addMapping 跨域所能访问的路径 //allowedOrigins:那些域可以访问,默认为任何域都可以访问 registry.addMapping("/api/**").allowedOrigins("*"); } }; } } |
- 配置Spring Security