测试的时候,前端在自己本地跑前端项目然后访问后台本地跑的项目,出现跨域问题.解决方法如下:springboot项目里增加一个配置类
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 *** * @ClassName CorsConfig * @Description 跨域处理 * @create *** * @desc *** **/ @Configuration public class CorsConfig { private CorsConfiguration buildConfig(){ CorsConfiguration corsConfiguration = new CorsConfiguration(); corsConfiguration.addAllowedOrigin("*"); //允许任何域名使用 corsConfiguration.addAllowedHeader("*"); //允许任何头 corsConfiguration.addAllowedMethod("*"); //允许任何请求方法(post、get等) return corsConfiguration; } @Bean public CorsFilter corsFilter(){ UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); source.registerCorsConfiguration("/**",buildConfig()); return new CorsFilter(source); } }
也可以参靠这篇:
https://www.jianshu.com/p/bf185ea791a7