解决跨域问题后端跨域配置代码:
/*** @作者 hua* @描述 跨域配置*/
@Configuration
public class WebConfiguration implements WebMvcConfigurer {/*** 跨域配置对象* @return CorsConfiguration对象*/private CorsConfiguration corsConfig() {CorsConfiguration corsConfiguration = new CorsConfiguration();List<String> list = new ArrayList<>();// 允许所有来源list.add("*");// 设置允许的来源列表corsConfiguration.setAllowedOrigins(list);// 允许所有HeadercorsConfiguration.addAllowedHeader("*");// 允许所有方法(GET、POST等)corsConfiguration.addAllowedMethod("*");return corsConfiguration;}/*** 注册CORS过滤器* @return CorsFilter对象*/@Beanpublic CorsFilter corsFilter() {UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();// 对所有路径应用上面定义的CORS配置source.registerCorsConfiguration("/**", corsConfig());return new CorsFilter(source);}
}