
本文旨在解决Spring Boot应用集成Spring Security后,前端React应用调用Stripe支付接口时遇到的CORS错误问题。通过分析配置、代码示例和错误信息,提供详细的解决方案,帮助开发者正确配置CORS,允许跨域请求,确保Stripe支付功能正常运行。本文重点在于理解CORS配置的关键点,以及如何在Spring Security中正确应用CORS策略。
CORS(Cross-Origin Resource Sharing,跨域资源共享)是一种浏览器安全机制,用于限制网页从不同源(域、协议或端口)请求资源。当Spring Security配置不当,或者CORS配置缺失时,可能会导致跨域请求被阻止,出现类似“Cross-Origin Request Blocked”的错误。本文将针对Spring Boot应用集成Stripe支付时遇到的CORS问题,提供详细的排查和解决方案。
CORS的配置主要涉及到服务器端(Spring Boot)的响应头设置,允许特定的源进行跨域请求。常见的CORS配置包括:
Spring Boot提供了多种方式来配置CORS,包括:
确认CORS配置生效:
首先,确保你的CORS配置已经生效。检查CorsConfiguration.java中的配置是否正确加载。如果使用了@CrossOrigin注解,确保注解的位置正确。
@Configuration
public class CorsConfiguration {
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedMethods("HEAD", "GET", "PUT", "POST", "DELETE", "PATCH","OPTIONS")
.allowedOrigins("https://m.stripe.com/6","https://r.stripe.com/0","http://localhost:3000")
.exposedHeaders("*")
.allowedHeaders("*")
.allowCredentials(true); // 允许发送cookie
}
};
}
}注意: allowCredentials(true) 需要与前端的 withCredentials: true 相对应,否则CORS请求会被拒绝。
检查Spring Security配置:
Spring Security的配置可能会覆盖或影响CORS配置。确保在Spring Security的配置中允许CORS请求。
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.cors().and().csrf().disable()
.authorizeRequests()
.antMatchers("/api/clients/authentication/**", "/api/repas/**").permitAll()
.anyRequest().authenticated()
.and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
http.addFilterBefore(jwtFilter, UsernamePasswordAuthenticationFilter.class);
}
@Bean
CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedOrigins(Arrays.asList("http://localhost:3000", "https://m.stripe.com/6","https://r.stripe.com/0")); // 允许的源
configuration.setAllowedMethods(Arrays.asList("GET","POST", "PUT", "DELETE", "OPTIONS")); // 允许的方法
configuration.setAllowedHeaders(Arrays.asList("Authorization", "Cache-Control", "Content-Type")); // 允许的头
configuration.setAllowCredentials(true); // 允许发送cookie
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration);
return source;
}
}关键点:
前端代码检查:
前端代码也需要进行相应的配置,确保跨域请求能够正确发送。
axios配置:
import axios from 'axios';
const instance = axios.create({
baseURL: 'http://localhost:8080', // 你的API地址
withCredentials: true, // 允许发送cookie
headers: {
'Content-Type': 'application/json',
}
});
export default instance;注意: withCredentials: true 必须与后端 allowCredentials(true) 相对应。
Stripe请求:
确保Stripe请求的发送方式正确。通常,Stripe的SDK会处理CORS问题,但如果手动发送请求,需要确保配置正确。
忽略 r.stripe.com 错误
根据Stripe官方的说法,r.stripe.com 域名主要用于追踪指标,与支付功能本身无关。因此,如果仅仅是 r.stripe.com 出现 CORS 错误,可以忽略它,不会影响支付流程。
解决Spring Security阻止Stripe请求导致的CORS问题,需要从服务器端和客户端两个方面入手。
通过以上步骤,可以解决大部分Stripe请求的CORS问题,确保支付功能正常运行。如果问题仍然存在,需要进一步检查网络配置和浏览器设置,确保没有其他因素干扰跨域请求。
以上就是解决Spring Security阻止Stripe请求导致的CORS问题的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号