首页 > Java > java教程 > 正文

解决Spring Security阻止Stripe请求的CORS问题

碧海醫心
发布: 2025-10-17 10:41:26
原创
548人浏览过

解决spring security阻止stripe请求的cors问题

本文旨在解决Spring Boot应用集成Spring Security后,前端React应用调用Stripe支付接口时遇到的CORS(跨域资源共享)问题。通过分析Spring Security配置、CORS配置以及前端代码,提供了一套完整的解决方案,帮助开发者正确配置CORS,确保Stripe支付功能的正常运行。重点在于理解CORS机制,并结合Spring Security的特性进行配置,最终实现前后端安全可靠的交互。

理解CORS与Spring Security

CORS (Cross-Origin Resource Sharing) 是一种浏览器安全机制,用于限制从一个源(域、协议和端口)发起的HTTP请求访问另一个源的资源。 当浏览器检测到跨域请求时,会先发送一个"预检"(preflight)请求(OPTIONS方法)到服务器,以确认服务器是否允许该跨域请求。

Spring Security作为一个强大的安全框架,默认情况下会拦截所有请求,包括CORS预检请求。 因此,我们需要正确配置Spring Security,使其允许来自特定源的跨域请求。

解决方案

解决此问题的关键在于正确配置CORS,并确保Spring Security不会错误地阻止Stripe的请求。 以下步骤提供了一种全面的解决方案:

1. 移除前端代码中不必要的CORS Header设置

在前端代码 ProfilPayment.java 中,你手动设置了 Access-Control-Allow-Origin 等header。这些header应该由服务器端设置,而不是客户端。移除以下代码:

const headers = {
    'Content-Type': 'application/json',
    "Access-Control-Allow-Origin": "*", // 移除
    'Access-Control-Allow-Methods': "*", // 移除
    'Authorization': `Bearer ${localStorage.getItem("token")}`
}

//修改为:
const headers = {
    'Content-Type': 'application/json',
    'Authorization': `Bearer ${localStorage.getItem("token")}`
}
登录后复制

2. 确保CORS配置正确

检查 CorsConfiguration.java 中的CORS配置。 确保允许前端应用的源(例如 http://localhost:3000)以及Stripe的必要域名。

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 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("http://localhost:3000") // 替换为你的前端应用域名
                        .exposedHeaders("*")
                        .allowedHeaders("*")
                        .allowCredentials(true); // 允许携带cookie,如果需要的话
            }
        };
    }
}
登录后复制

注意事项:

  • allowedOrigins 应该设置为你的前端应用的实际域名。 在生产环境中,不要使用 *,因为它会允许所有域的跨域请求,这可能存在安全风险。
  • allowCredentials(true) 只有在你的请求需要携带cookie时才需要设置。

3. Spring Security配置调整

AI建筑知识问答
AI建筑知识问答

用人工智能ChatGPT帮你解答所有建筑问题

AI建筑知识问答 22
查看详情 AI建筑知识问答

确保Spring Security的配置允许CORS预检请求。 一种方法是在 SecurityConfig.java 中添加一个 CorsFilter bean。 这种方法通常比使用 WebMvcConfigurer 更可靠,尤其是在处理复杂的安全配置时。

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.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import java.util.Arrays;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    // 注入你的 JWT 过滤器
    private final JwtFilter jwtFilter;

    public SecurityConfig(JwtFilter jwtFilter) {
        this.jwtFilter = jwtFilter;
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        String[] staticResources = {
                "/api/clients/authentication/**",
                "/api/repas/**"
        };

        http.cors().and().csrf().disable()
                .exceptionHandling()
                .and()
                .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests()
                .antMatchers(staticResources).permitAll()
                .anyRequest().authenticated();

        http.addFilterBefore(jwtFilter, UsernamePasswordAuthenticationFilter.class); // 调整过滤器顺序
    }

    @Bean
    public CorsConfigurationSource corsConfigurationSource() {
        CorsConfiguration configuration = new CorsConfiguration();
        configuration.setAllowedOrigins(Arrays.asList("http://localhost:3000")); // 替换为你的前端应用域名
        configuration.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE", "OPTIONS"));
        configuration.setAllowedHeaders(Arrays.asList("authorization", "content-type", "x-auth-token"));
        configuration.setAllowCredentials(true);
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", configuration);
        return source;
    }

    @Bean
    public CorsFilter corsFilter() {
        return new CorsFilter(corsConfigurationSource());
    }

}
登录后复制

解释:

  • @EnableWebSecurity: 启用 Spring Security 的 Web 安全功能。
  • corsConfigurationSource(): 定义一个 CorsConfigurationSource bean,用于配置 CORS。
    • setAllowedOrigins(): 设置允许的源。
    • setAllowedMethods(): 设置允许的 HTTP 方法。
    • setAllowedHeaders(): 设置允许的请求头。
    • setAllowCredentials(): 设置是否允许携带 cookie。
  • CorsFilter: 创建一个 CorsFilter bean,并将 CorsConfigurationSource 应用于所有请求。

关键点:

  • 确保 CorsFilter 在 Spring Security 过滤器链中正确注册。 在上面的例子中,我们直接在 SecurityConfig 中创建了 CorsFilter bean,Spring会自动将其添加到过滤器链中。
  • 将 JwtFilter 调整到 UsernamePasswordAuthenticationFilter 之前,确保 CORS 过滤器先于 JWT 过滤器执行。 这允许预检请求通过而无需 JWT 验证。

4. 检查Stripe域名

确认你使用的Stripe API域名是否需要添加到CORS允许列表中。虽然 r.stripe.com 主要用于跟踪,但确保用于实际支付请求的域名(例如 api.stripe.com)也在你的CORS配置中。通常情况下,Stripe SDK会自动处理与Stripe API的交互,所以你可能不需要显式地添加api.stripe.com到allowedOrigins。

5. 测试与调试

完成上述配置后,重新启动你的Spring Boot应用和React应用。 使用浏览器的开发者工具检查网络请求,查看CORS相关的header是否正确设置。 确保没有CORS错误。

总结

解决Spring Security和Stripe集成中的CORS问题需要仔细配置CORS和Spring Security。 通过移除前端不必要的CORS header,正确配置Spring Security和CORS过滤器,并验证Stripe域名,可以确保你的应用能够安全地处理Stripe支付请求。记住,安全至关重要,始终使用最小权限原则,并避免在生产环境中使用通配符(*)作为允许的源。

以上就是解决Spring Security阻止Stripe请求的CORS问题的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号