修复了一系列bug
This commit is contained in:
@ -16,15 +16,27 @@ public class CorsConfig {
|
||||
|
||||
@Bean
|
||||
public FilterRegistrationBean<CorsFilter> corsFilter() {
|
||||
// 1. 准备一个“规则源”,用于给不同路径配置 CORS 规则
|
||||
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
|
||||
// 2. 创建一个“跨域配置”对象,往里填各种允许/不允许的选项
|
||||
CorsConfiguration config = new CorsConfiguration();
|
||||
// 携带cookie
|
||||
// 【允许携带 Cookie 这类 “凭证”】
|
||||
config.setAllowCredentials(true);
|
||||
// 放行哪些域名(必须用 patterns,否则 * 会和 allowCredentials 冲突)
|
||||
// 【允许的来源域名】
|
||||
// 用通配符 "*",表示不管从哪个域来的,都允许。
|
||||
// 这里用的是 addAllowedOriginPattern,而不是 addAllowedOrigin,
|
||||
// 是为了和 allowCredentials(true) 一起使用时不报错。
|
||||
config.addAllowedOriginPattern("*");
|
||||
// 【允许的请求头】
|
||||
// 客户端可以带哪些自定义的 HTTP 头,比如 Content-Type、X-Token…
|
||||
config.addAllowedHeader("*");
|
||||
// 【允许的请求方法】
|
||||
// GET、POST、PUT、DELETE、OPTIONS……所有都放行
|
||||
config.addAllowedMethod("*");
|
||||
// 把上面这套“跨域规则”注册到所有接口路径(/**)
|
||||
source.registerCorsConfiguration("/**", config);
|
||||
// 3.用这套规则,创建一个 CorsFilter 过滤器,
|
||||
// 4.并把它交给 Spring 管理,优先级设为最高
|
||||
FilterRegistrationBean<CorsFilter> bean = new FilterRegistrationBean<>(new CorsFilter(source));
|
||||
bean.setOrder(Ordered.HIGHEST_PRECEDENCE);
|
||||
return bean;
|
||||
|
Reference in New Issue
Block a user