解决跨域访问,添加一个配置类,如下所示:
package com.fanfengping.config.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 { private CorsConfiguration configuration() { CorsConfiguration corsConfig = new CorsConfiguration(); corsConfig.setAllowCredentials(true); corsConfig.addAllowedOrigin("*"); // 1允许任何域名使用 corsConfig.addAllowedHeader("*"); // 2允许任何头 corsConfig.addAllowedMethod("*"); // 3允许任何方法(post、get等) return corsConfig; } @Bean public CorsFilter corsFilter() { UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); source.registerCorsConfiguration("/**", configuration()); // 4 return new CorsFilter(source); } }
重新打包部署即可。