在集成Stripe支付功能时,很多开发者会遇到Spring Security拦截Stripe跨域请求的问题,导致支付回调、前端发起的支付请求无法正常处理。这类问题本质是跨域资源共享规则和安全框架配置的冲突,下面我们一步步分析解决思路。

问题产生的原因
Spring Security默认会启用一系列安全过滤器,其中自带的CORS过滤器如果优先级设置不当,或者没有正确配置允许的来源,就会把Stripe的跨域请求拦截。Stripe的请求通常来自特定的域名,比如https://checkout.stripe.com,如果后端没有把这个域名加入CORS允许的源列表,请求就会被拒绝。
解决步骤
1. 配置全局CORS规则
首先需要定义明确的CORS配置,指定允许的源、方法和请求头,这里要把Stripe相关的域名加入允许列表。
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;
import java.util.Arrays;
@Configuration
public class CorsConfig {
@Bean
public CorsFilter corsFilter() {
CorsConfiguration config = new CorsConfiguration();
// 允许Stripe的域名,生产环境可替换为实际的Stripe回调域名
config.setAllowedOrigins(Arrays.asList(
"https://checkout.stripe.com",
"https://api.stripe.com"
));
// 允许的请求方法
config.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE", "OPTIONS"));
// 允许的请求头
config.setAllowedHeaders(Arrays.asList("*"));
// 允许携带凭证
config.setAllowCredentials(true);
// 设置CORS规则生效的路径
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", config);
return new CorsFilter(source);
}
}2. 调整Spring Security配置
Spring Security中需要关闭默认的CORS禁用规则,同时把CORS过滤器放在安全过滤器链的前面,避免安全规则先拦截请求。
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
// 开启CORS支持,使用自定义的CORS配置
.cors().and()
// 关闭CSRF,Stripe的回调请求通常不携带CSRF令牌
.csrf().disable()
// 配置请求授权规则
.authorizeRequests()
// Stripe相关的接口允许匿名访问,根据实际接口路径调整
.antMatchers("/stripe/**").permitAll()
.anyRequest().authenticated()
.and()
// 设置为无状态会话,适合接口服务
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}
}3. 验证Stripe接口配置
如果是处理Stripe的Webhook回调,需要确保回调接口的响应符合Stripe的要求,同时返回正确的CORS头。可以在接口中添加简单的日志,确认请求是否到达。
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class StripeWebhookController {
@PostMapping("/stripe/webhook")
public String handleStripeWebhook(@RequestBody String payload) {
// 处理Stripe回调逻辑
System.out.println("收到Stripe回调请求");
return "success";
}
}注意事项
- 生产环境中不要使用
setAllowedOrigins(Arrays.asList("*")),要明确指定Stripe的官方域名,避免安全风险。 - 如果Stripe的请求包含自定义的请求头,需要在
setAllowedHeaders中显式添加,否则还是会被拦截。 - 可以通过浏览器的开发者工具查看网络请求的响应头,确认
Access-Control-Allow-Origin等CORS头是否正确返回。
按照以上步骤配置后,Spring Security就不会再拦截Stripe的跨域请求,支付相关的功能可以正常运行。如果还有问题,可以检查过滤器的加载顺序,确保CORS过滤器在Spring Security的过滤器之前执行。
Spring_SecurityCORSStripe跨域请求修改时间:2026-05-30 23:49:13