网站建设合同用缴印花税吗,如何搭建微商城,wordpress 图片页面,网站菜单代码1. 跨域介绍 
首先解释什么是跨域#xff0c;跨域就是前端和后端的端口号不同#xff1b;会产生跨域问题#xff0c;这里浏览器的保护机制#xff08;同源策略#xff09;。 同源策略#xff1a;前端和后端的协议、域名、端口号三者都相同叫做同源。 我们看一下不同源跨域就是前端和后端的端口号不同会产生跨域问题这里浏览器的保护机制同源策略。 同源策略前端和后端的协议、域名、端口号三者都相同叫做同源。 我们看一下不同源 VUEhttp://localhost:8080 Spring: http://localhost:8081/list 当我们出现跨域问题前端就会报一个错篮框扩这那个  
2. 解决方法 
上方就是不同源两者的协议、域名相同但是端口号不同如何解决呢使用Spring Boot解决它提供三种方案 
直接在方法上方添加CrossOrigin注解即可解决问题 CrossOriginRequestMapping(/getuserbyid)public UserInfo getUserById(Integer id) {if(id  null ) return null;return userService.getUserById(id);}添加 CORS 过滤器 
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 {Beanpublic CorsFilter corsFilter() {CorsConfiguration corsConfiguration  new CorsConfiguration();corsConfiguration.setAllowCredentials(true); // 允许cookies跨域corsConfiguration.addAllowedHeader(*); // 请求头字段corsConfiguration.addAllowedMethod(*); // 方法corsConfiguration.addAllowedOrigin(*); // 允许向该服务器提交请求的URI*表示全部允许自定义可以添加多个在SpringMVC中如果设成*会自动转成当前请求头中的OriginUrlBasedCorsConfigurationSource source  new UrlBasedCorsConfigurationSource();source.registerCorsConfiguration(/**,corsConfiguration); // 添加映射路径以及参数return new CorsFilter(source);}
}重写 WebMvcConfigurer 接口中的 addCorsMappings 方法 
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;Configuration
public class WebConfig implements WebMvcConfigurer {Overridepublic void addCorsMappings(CorsRegistry registry) {// 先设置映射registry.addMapping(/**).allowedOriginPatterns(*) // 允许向该服务器提交请求的URI*表示全部允许自定义可以添加多个在SpringMVC中如果设成*会自动转成当前请求头中的Origin.allowCredentials(true) // 允许cookies跨域.allowedHeaders(*) // 请求头字段.allowedMethods(GET,POST) // 允许跨域的方法.maxAge(3600);// 预检请求的缓存时间秒即在这个时间段里对于相同的跨域请求不会再预检了}
}