当前位置: 首页 > news >正文

SpringBoot3中跨域问题解决

问题

SpringBoot3 中处理跨域请求

异常

浏览器在 localhost:3000 地址请求后端 http://127.0.0.1:8080 时, 报错提示 CORS 问题.
默认使用 Get 请求正常, 其他会提示.
使用 SpringBoot 3.4.2 版本配合 SpringSecurity 配置

Access to fetch at 'http://127.0.0.1:8080/todo-task/list' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

解决

需要在 Spring Security 配置以下内容…


/**
 * Spring Security 配置
 *
 * @author Jion
 */
@Configuration
@EnableWebSecurity
@AllArgsConstructor
public class SecurityConfig {

    private final JwtAuthenticationFilter jwtAuthenticationFilter;

    private final CustomAuthenticationEntryPoint unauthorizedHandler;


    /**
     * 配置过滤器链
     *
     * @param http 请求
     * @return 过滤器链
     * @throws Exception 抛出异常
     */
    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
		// 一些其他配置等...
		
        // 跨域请求
        http.cors(corsConfigurer -> corsConfigurer.configurationSource(corsConfigurationSource()));
        return http.build();
    }


    /**
     * 跨域配置, 仅在开发环境有必要.
     * 如果是发布之后, 桌面应用不会产生跨域问题.
     */
    @Bean
    public UrlBasedCorsConfigurationSource corsConfigurationSource() {
        CorsConfiguration configuration = new CorsConfiguration();
        // 允许本地域名访问
        configuration.setAllowedOrigins(Arrays.asList("http://localhost:3000", "http://127.0.0.1:3000"));
        // 允许被本地域名访问
        configuration.setAllowedOriginPatterns(Arrays.asList("http://localhost:3000/**", "http://127.0.0.1:3000/**"));
        // 允许所有请求方法
        configuration.setAllowedMethods(List.of("*"));
        // 允许所有请求头
        configuration.setAllowedHeaders(List.of("*"));
        // 允许所有响应头
        configuration.setExposedHeaders(List.of("*"));
        // 允许携带凭证
        configuration.setAllowCredentials(true);
        // 跨域请求配置
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        // 匹配所有请求路径
        source.registerCorsConfiguration("/**", configuration);
        return source;
    }
}

相关文章:

  • 上帝之眼——nmap
  • AI 人工智能 概念
  • 请解释 Vue 中的生命周期钩子,不同阶段触发的钩子函数及其用途是什么?
  • NetLogon 权限提升漏洞
  • 2025年微店平台商品详情接口调用指南(Python代码示例)
  • Redis简介、常用命令及优化
  • es6中Relect的详细用法
  • 【MySQL】索引与事务
  • 深度学习-123-综述之AI人工智能与DL深度学习简史1956到2024
  • 一文讲解Redis中的常用命令
  • 蓝桥杯备赛-基础训练(二)链表 day13
  • 【含开题报告+文档+PPT+源码】基于SpringBoot+Vue的社区团购配送系统
  • 虚拟机网络ssh连接失败,没有网络
  • 在 Vue 3 中使用 ECharts 制作多 Y 轴折线图时,若希望 **Y 轴颜色自动匹配折线颜色**且无需手动干预,可以通过以下步骤实现:
  • 鸿蒙-验证码输入框的几种实现方式-上
  • 软件集成测试的技术要求
  • TCP和Http协议
  • import requests Pycharm 报错
  • 什么是向量化?ElasticSearch如何存储向量化?
  • fedora 安装 ffmpeg 过程记录
  • 上海北外滩,未来五年将如何“长个子”“壮筋骨”?
  • 外国游客“在华扫货”热:“带空箱子到中国!”
  • 外企聊营商|波音速度:创新审批促“起飞”
  • 体坛联播|安切洛蒂执掌巴西男足,字母哥尝试离开雄鹿
  • 教育部:启动实施县中头雁教师岗位计划,支撑县中全面振兴
  • 上海与世界|环城生态公园带是上海绿色发展新名片