package com.chinatower.railway.biz.config;
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;
/**
* <p>Title: CorsConfig</p>
* <p>Description:
* 描述:解决前台调用跨域的问题
* </p>
*
* @author tarkuni
* @since 2018/11/13 13:57
*/
@Configuration
public class CorsConfig {
private CorsConfiguration buildConfig() {
CorsConfiguration corsConfiguration = new CorsConfiguration();
//测试环境:允许所有的客户端调用
corsConfiguration.addAllowedOrigin("*");
//生产环境:指定允许访问的客户端地址
corsConfiguration.addAllowedHeader("*");
corsConfiguration.addAllowedMethod("*");
return corsConfiguration;
}
@Bean
public CorsFilter corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", buildConfig());
return new CorsFilter(source);
}
}
java后台支持跨域
最新推荐文章于 2024-11-15 18:38:47 发布