跨域是指浏览器发起的请求,其目标服务器与当前页面的来源(域名、协议、端口)不一致。例如:
http://example.com
http://example.com/api
http://localhost:3000
http://localhost:8080/api
跨域问题的根源在于 浏览器的同源策略(Same-Origin Policy),这是一种安全机制,防止恶意站点通过 JavaScript 等方式访问用户敏感数据。
同源策略限制:浏览器的同源策略只允许当前网页(前端)的域名、端口、和协议与后端一致。如果前后端运行在不同的端口、协议或域名下(如前端在 http://localhost:3000
,后端在 http://localhost:8080
),这就是跨域,浏览器会阻止请求。
跨域的目的:
http://localhost:3000
)访问,那就是为了让你的前端能调用后端。*
),则是为了对任何来源的请求都开放,可能包括来自其他网站或恶意来源。表现: 当浏览器发起跨域请求时,如果目标服务器没有正确配置跨域资源共享(CORS),会报类似以下错误:
Access to XMLHttpRequest at 'http://example.com/api' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
解决方案:
目标服务器通过 CORS 协议,明确告诉浏览器允许跨域请求。CORS 协议依赖以下 HTTP 头部字段:
Access-Control-Allow-Origin
:允许的请求来源(如 http://localhost:3000
或 *
)。Access-Control-Allow-Methods
:允许的请求方法(如 GET, POST
)。Access-Control-Allow-Headers
:允许的请求头(如 Content-Type, Authorization
)。Access-Control-Allow-Credentials
:是否允许发送 Cookies 和凭据。Spring Boot 提供了多种方式来配置 CORS,根据项目需求灵活选择。
@CrossOrigin
)使用 @CrossOrigin
注解,在控制器或具体方法上配置跨域规则,适合简单场景。
示例代码:
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyController {
@CrossOrigin(origins = "http://localhost:3000") // 允许来自 http://localhost:3000 的请求
@GetMapping("/api/data")
public String getData() {
return "Hello, CORS!";
}
}
优缺点:
通过实现 WebMvcConfigurer
接口,配置全局跨域规则,适用于基于 Spring MVC 的项目。
示例代码:
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**") // 匹配所有接口
.allowedOrigins("http://localhost:3000") // 允许的请求来源
.allowedMethods("GET", "POST", "PUT", "DELETE") // 允许的请求方法
.allowedHeaders("*") // 允许的请求头
.allowCredentials(true) // 允许携带 Cookies 或凭据
.maxAge(3600); // OPTIONS 预检请求的缓存时间
}
}
根据CORS规范,当
allowCredentials(true)
时,allowedOrigins
必须指定具体的源,不能是通配符 "*
"
- 当
allowCredentials
设置为true
时,意味着请求可以携带认证信息(如 Cookies)- 如果同时允许所有源(
*
),会存在严重的安全漏洞- 攻击者可以从任何域发送包含凭证的跨域请求
如果不遵守这个规定,就无法允许任何的跨域请求并报错403。(博主血的教训)
对于基于 Spring WebFlux 的响应式项目,需要使用 CorsWebFilter
处理跨域。
示例代码:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
@Configuration
public class CorsConfig {
@Bean
public CorsWebFilter corsWebFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration config = new CorsConfiguration();
config.addAllowedOrigin("*"); // 允许所有来源
config.addAllowedHeader("*"); // 允许所有请求头
config.addAllowedMethod("*"); // 允许所有方法
config.setAllowCredentials(true); // 允许凭据
source.registerCorsConfiguration("/**", config);
return new CorsWebFilter(source);
}
}
两种方式的区别
CorsRegistry:
WebMvcConfigurer
接口,通过 addCorsMappings
方法设置 CORS 规则。CorsWebFilter:
如果不通过上面说到的针对Controller层的全局跨域配置,也可以通过在Spring Security的SecurityFilterChain中使用 http.cors()来支持跨域,与之前的Controller层的全局跨域配置二者取其一就可以。
代码示例:
@Configuration
public class SecurityConfig {
@Autowired
private JwtAuthenticationFilter jwtAuthenticationFilter;
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.cors(cors -> cors.configurationSource(request -> {
CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedOriginPatterns(List.of("*")); // 允许所有源
configuration.setAllowedMethods(List.of("*")); // 允许所有方法
configuration.setAllowedHeaders(List.of("*")); // 允许所有请求头
configuration.setAllowCredentials(true); // 注意:使用 * 时必须设置为 false
return configuration;
}));
return http.build();
}
*
"的同时AllowCredentials可以设为true)setAllowedOriginPatterns
方法允许使用更灵活的匹配模式,可以在URL中使用通配符,从而匹配多个源。例如:"http://*.example.com"Spring应用中的请求处理遵循以下的过滤器链顺序:
CorsFilter
)。HandlerInterceptor
)。Spring Security的CORS配置:如果配置了 HttpSecurity.cors()
,Spring Security将自动注册一个 CorsFilter
,该过滤器位于Spring Security的过滤器链中。这意味着它会在Spring Security的认证和授权过滤器之后执行。
WebMvcConfigurer
的CORS配置:Spring MVC内部也有一个 CorsFilter
,它位于Spring MVC的处理链中,通常在Spring Security过滤器链之后执行。
当同时使用 WebMvcConfigurer
和 Spring Security 的 HttpSecurity.cors()
来配置CORS时:
Spring Security的CORS配置优先于 WebMvcConfigurer
的CORS配置:因为Spring Security的过滤器位于过滤器链的前端,先于Spring MVC的过滤器执行。因此,Spring Security的CORS配置会首先生效。
潜在的配置冲突:如果两者的CORS配置不一致,可能导致预期之外的行为。通常,建议选择其中一种方式进行CORS配置,以避免混淆和冲突。
在微服务架构中,Spring Cloud Gateway 通常作为 API 网关,代理不同微服务的请求。跨域请求可以在网关层统一处理,避免每个微服务都重复配置跨域。
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
@Configuration
public class GatewayCorsConfig {
@Bean
public CorsWebFilter corsWebFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration config = new CorsConfiguration();
// 不能使用 "*",需要指定具体域名
config.addAllowedOrigin("http://localhost:5173");
config.addAllowedMethod("*");
config.addAllowedHeader("*");
config.setAllowCredentials(true);
source.registerCorsConfiguration("/**", config);
return new CorsWebFilter(source);
}
}
application.yml
配置spring:
cloud:
gateway:
globalcors:
cors-configurations:
'[/**]':
allowedOrigins: "http://localhost:3000"
allowedMethods: "*"
allowedHeaders: "*"
allowCredentials: true
区别:
CorsWebFilter
作用于网关层,处理所有通过网关的跨域请求。CorsWebFilter
仅处理单个服务的跨域。根据CORS规范,当
allowCredentials(true)
时,allowedOrigins
必须指定具体的源,不能是通配符 "*
"
- 当
allowCredentials
设置为true
时,意味着请求可以携带认证信息(如 Cookies)- 如果同时允许所有源(
*
),会存在严重的安全漏洞- 攻击者可以从任何域发送包含凭证的跨域请求
如果不遵守这个规定,就无法允许任何的跨域请求并报错403。(博主血的教训)