同源策略:协议、域名、端口3个都相同就是同源Vue:http://localhost:8080Spring Boot: http://localhost:8181/listCORS: Cross Origin Resource SharingSpring Boot项目中解决跨域的3种方案1、在目标方法上添加@CrossOrigin注解@GetMapping("/list") @CrossOrigin public List<String> list(){ List<String> list = Arrays.asList("Java","C++","Go"); return list; } 2、添加CORS过滤器package com.fzg.springboottest.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 { @Bean public CorsFilter corsFilter(){ CorsConfiguration corsConfiguration = new CorsConfiguration(); corsConfiguration.addAllowedOrigin("*"); corsConfiguration.addAllowedHeader("*"); corsConfiguration.addAllowedMethod("*"); UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); source.registerCorsConfiguration("/**", corsConfiguration); return new CorsFilter(source); } } 3、实现WebMvcConfigure接口,重写addCorsMappings方法package com.fzg.springboottest.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.filter.CorsFilter; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class CorsConfiguration implements WebMvcConfigurer { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") .allowedOriginPatterns("*") .allowedMethods("GET","POST","PUT","DELETE","HEAD","OPTIONS") .allowCredentials(true) .maxAge(3600) .allowedHeaders("*"); } }