我用spring-boot
作Rest
服务,Vue
作前端框架,用了element-admin-ui
这个框架作后台管理。在调试的过程当中遇到了以下错误:前端
Preflight response is not successful
这个问题是典型的CORS
跨域问题。web
所谓跨域:spring
跨域,指的是浏览器不能执行其余网站的脚本。它是由浏览器的同源策略形成的,是浏览器对JavaScript施加的安全限制。
在项目中添加类CustomCORSConfiguration
代码以下:跨域
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; /** * @author spartajet * @description * @create 2018-05-15 下午5:00 * @email spartajet.guo@gmail.com */ @Configuration public class CustomCORSConfiguration { private CorsConfiguration buildConfig() { CorsConfiguration corsConfiguration = new CorsConfiguration(); corsConfiguration.addAllowedOrigin("*"); corsConfiguration.addAllowedHeader("*"); corsConfiguration.addAllowedMethod("*"); corsConfiguration.setAllowCredentials(true); return corsConfiguration; } @Bean public CorsFilter corsFilter() { UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); source.registerCorsConfiguration("/**", buildConfig()); return new CorsFilter(source); } }