1、什么是CORS
定义:跨域资源共享。
2、什么是跨域资源共享
允许浏览器可以从当前源服务器通过ajax访问另外一个源服务地址。
3、同源策略
是浏览器的一个安全功能,不同源的客户端脚本在没有明确的授权的情况下,不能读写对方资源。同源策略是浏览器安全的基石。
4、什么是源 ORIGIN
源 [ORIGIN] 就是协议、域名和端口号。
例如:http://www.baidu.com:80 这个URL。
协议:http
端口:80
测试URL:http://www.a.com/test/index.html 默认端口80
http://www.a.com/dir/page.html 同源
http://www.child.a.com/test/index.html 不同源
https://www.a.com/test/index.html 不同源
http://www.a.com:8080/test/index.html 不同源
5、哪些操作不会受到同源限制
<script>
<img>
<link>
<iframe>
6、哪些操作会受到同源限制
ajax
出现跨域:Access-Control-Allow-Origin
7、springboot中如何解决跨域问题?
一、局部解决跨域
@CrossOrigin:这个注解用在类上,代表该类中所有方法运行允许其他域中资源访问。
二、全局解决跨域
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
@Configuration
public class CorsConfig
{
@Bean
public CorsFilter corsFilter()
{
UrlBasedCorsConfigurationSource source = new
UrlBasedCorsConfigurationSource();
CorsConfiguration corsConfiguration = new CorsConfiguration();
//允许任何域名使用
corsConfiguration.addAllowedOrigin("*");
//允许任何头
corsConfiguration.addAllowedHeader("*");
//允许任何请求方式(post、get等)
corsConfiguration.addAllowedMethod("*");
//处理所有请求的跨域配置
source.registerCorsConfiguration("/**", corsConfiguration);
return new CorsFilter(source);
}
}
浙公网安备 33010602011771号