springMVC encapsulates cors related, add the following configuration information at the gateway, you can achieve cross-domain issues
package cn.twgfs.shop.cloud.zuul.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;
/**
* @author willian
* @date 2020/3/8 14:06
* Cors3解决跨域配置问题
*/
@Configuration
public class config {
@Bean
public CorsFilter corsFilter(){
//初始化cors配置对象
CorsConfiguration corsConfiguration = new CorsConfiguration();
//1.添加需要跨域的域名,如果要携带cookie就不能些*,*代表所有域名
corsConfiguration.addAllowedOrigin("需要跨域的域名。例如http://www.twgfs.online");
//2.设置是否允许携带cookie
corsConfiguration.setAllowCredentials(true);
//3.设置请求方式,列如put,get,post,delete.*代表所有
corsConfiguration.addAllowedMethod("*");
//4.设置请求头部信息,*表示允许携带任何头部信息
corsConfiguration.addAllowedHeader("*");
//初始化cors配置源对象
UrlBasedCorsConfigurationSource configurationSource = new UrlBasedCorsConfigurationSource();
configurationSource.registerCorsConfiguration("/**",corsConfiguration);
//返回cors的示例,参数:cors的配置源对象
return new CorsFilter(configurationSource);
}
}