测试跨域
This commit is contained in:
parent
75d13febeb
commit
f70a495bb7
@ -3,26 +3,44 @@ package com.example.caseData.core;
|
|||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.lang.NonNull;
|
import org.springframework.lang.NonNull;
|
||||||
|
import org.springframework.web.cors.CorsConfiguration;
|
||||||
|
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
|
||||||
|
import org.springframework.web.filter.CorsFilter;
|
||||||
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
||||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
public class CorsConfig {
|
public class CorsConfig {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public WebMvcConfigurer corsConfigurer() {
|
public CorsFilter corsFilter() {
|
||||||
return new WebMvcConfigurer() {
|
CorsConfiguration config = new CorsConfiguration();
|
||||||
@Override
|
|
||||||
public void addCorsMappings(@NonNull CorsRegistry registry) {
|
// 允许所有来源(与 Gin 中的动态 Origin 等价)
|
||||||
registry.addMapping("/**") // 允许所有路径
|
config.addAllowedOriginPattern("*");
|
||||||
.allowedOriginPatterns("*") // 允许所有来源(Spring 2.4+ 用 `allowedOriginPatterns`)
|
|
||||||
.allowedMethods("POST", "GET", "OPTIONS", "PUT", "DELETE", "UPDATE") // 允许的 HTTP 方法
|
// 允许的请求方法
|
||||||
.allowedHeaders("Origin", "X-Requested-With", "Content-Type", "Accept", "Authorization") // 允许的请求头
|
config.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE", "OPTIONS", "UPDATE"));
|
||||||
.exposedHeaders("Content-Length", "Access-Control-Allow-Origin",
|
|
||||||
"Access-Control-Allow-Headers", "Cache-Control",
|
// 允许的请求头
|
||||||
"Content-Language", "Content-Type") // 允许前端访问的响应头
|
config.setAllowedHeaders(Arrays.asList(
|
||||||
.allowCredentials(false); // 是否允许携带 Cookie
|
"Origin", "X-Requested-With", "Content-Type", "Accept", "Authorization"
|
||||||
}
|
));
|
||||||
};
|
|
||||||
|
// 暴露哪些响应头给前端
|
||||||
|
config.setExposedHeaders(Arrays.asList(
|
||||||
|
"Content-Length", "Access-Control-Allow-Origin", "Access-Control-Allow-Headers",
|
||||||
|
"Cache-Control", "Content-Language", "Content-Type"
|
||||||
|
));
|
||||||
|
|
||||||
|
// 是否允许携带 Cookie 等认证信息(Gin 里是 false)
|
||||||
|
config.setAllowCredentials(false);
|
||||||
|
|
||||||
|
// 注册跨域配置
|
||||||
|
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
|
||||||
|
source.registerCorsConfiguration("/**", config);
|
||||||
|
return new CorsFilter(source);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user