同源策略是浏览器的一个安全功能,不同源的客户端脚本在没有明确授权的情况下,不能读写对方资源。 同源策略是浏览器安全的基石。
源就是协议、域名和端口号(三个只要有一个不同,就会出现跨域问题)。
通过配置Cors解决跨域问题
package com.college.collegesystem.config;
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 {
private CorsConfiguration buildConfig(){
CorsConfiguration corsConfiguration = new CorsConfiguration();
//允许任何域名使用
corsConfiguration.addAllowedOrigin("*");
//允许任何请求头
corsConfiguration.addAllowedHeader("*");
//允许任何请求方法
corsConfiguration.addAllowedMethod("*");
return cors
同源策略限制了不同源的浏览器脚本访问资源,而SpringBoot可以通过配置CORS解决跨域问题,允许特定来源的请求访问服务器资源。
最低0.47元/天 解锁文章

1906

被折叠的 条评论
为什么被折叠?



