package cn.test.common.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import java.util.ArrayList;
import java.util.List;
/**
* ===================================================
*
* @Title: RequestFilter.java
* @Package: cn.hsa.spp.tender.biz.config
* @Description:
* @Version: V1.0
* ===================================================
*/
@Configuration
public class RequestFilter extends CorsFilter {
public RequestFilter() {
super(configSource());
}
/**
* 跨域设置
*
* @return UrlBasedCorsConfigurationSource
*/
private static UrlBasedCorsConfigurationSource configSource() {
List<String> allowedHeaders = new ArrayList<>();
//增加自定义设置headers参数
List<String> allowedMethods = new ArrayList<>();
allowedMethods.add("OPTIONS");
allowedMethods.add("POST");
allowedMethods.add("GET");
CorsConfiguration config = new CorsConfiguration();
//默认不携带cookies
config.setAllowCredentials(false);
config.addAllowedOrigin(CorsConfiguration.ALL);
config.setAllowedHeaders(allowedHeaders);
config.setAllowedMethods(allowedMethods);
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", config);
return source;
}
}
java处理跨域代码
最新推荐文章于 2023-10-01 16:12:34 发布