Cors

CORS(跨域资源共享),这是由W3C提出的。

浏览器有同源策略, 源[origin]就是协议、域名和端口号,同时同源策略还对http方法有约束。

部分代码引自 https://spring.io/blog/2015/06/08/cors-support-in-spring-framework

1.控制器CORS配置: 你可以@RequestMapping所拦截的方法上添加@CrossOrigin,默认情况下允许所有源和方法。

@RestController
@RequestMapping("/account")
public class AccountController {

	@CrossOrigin
	@GetMapping("/{id}")
	public Account retrieve(@PathVariable Long id) {
		// ...
	}

	@DeleteMapping("/{id}")
	public void remove(@PathVariable Long id) {
		// ...
	}
}

也可以为整个Controller启用CORS

也可以组合配置

如果使用了Spring Security,就可以在Security级别启用CORS。 https://docs.spring.io/spring-security/site/docs/current/reference/html/cors.html

以上是细粒度的CORS,也可以启用全局的CORS。

2.JavaConfig

为整个应用程序启用CORS

如果是使用了SpringBoot,则可以这样使用

也可以定制配置

3.基于过滤器的CORS Spring Framework还提供了CorsFilter

Last updated