一、同源策略的意义
同源策略是浏览器的一个安全功能,不同源的客户端脚本在没有明确授权的情况下,不能读写对方资源。只有同一个源的脚本赋予dom、读写cookie、session、ajax等操作的权限。
什么是源
源[origin]就是协议、域名和端口号。例如:http://www.baidu.com:80这个URL。
什么是同源
若地址里面的协议、域名和端口号均相同则属于同源。
以http://abc.com/为例

二、SpringBoot2.x配置Cors
SpringBoot2.x主要提供了两种方式来支持Cors,如下:

参考:https://www.cnblogs.com/anxminise/p/9808279.html
三、官方全局配置模板---WebMvcConfigurer
参考:https://blog.csdn.net/nainaiqiuwencun/article/details/83029770
官方文档:https://docs.spring.io/spring-boot/docs/2.0.5.RELEASE/reference/htmlsingle/#boot-features-cors
@Configuration
public class MyConfiguration {
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/api/**");
}
};
}
}
网友评论