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 {
/**
* 配置跨域请求
* @return
*/
@Bean
public CorsFilter corsFilter() {
//初始化cors配置对象
CorsConfiguration corsConfiguration = new CorsConfiguration();
//允许跨域的域名
corsConfiguration.addAllowedOrigin("*");
//允许cookie
corsConfiguration.setAllowCredentials(true);
//可跨域的请求方式 如get put,post....
corsConfiguration.addAllowedMethod("*");
//允许携带任何头信息
corsConfiguration.addAllowedHeader("*");
//初始化cors配置源对象
UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);
return new CorsFilter(urlBasedCorsConfigurationSource);
}
}