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

Spring Boot 解决跨域问题

在 Spring Boot 中解决跨域问题(CORS)主要有三种常用方式,下面详细说明每种实现方法:

方案一:全局配置(推荐)

在配置类中实现 WebMvcConfigurer 接口,统一配置所有接口的跨域规则:

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 CorsConfig implements WebMvcConfigurer {@Overridepublic void addCorsMappings(CorsRegistry registry) {registry.addMapping("/**")  // 所有接口// Spring Boot 2.4+ 使用 allowedOriginPatterns.allowedOriginPatterns("*") // 允许的请求方法.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") // 允许的请求头.allowedHeaders("*") // 是否允许发送Cookie.allowCredentials(true) // 预检请求缓存时间(秒).maxAge(3600); }
}

方案二:控制器注解配置

在单个控制器或方法上使用 @CrossOrigin 注解:

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
// 类级别配置(整个控制器生效)
@CrossOrigin(origins = "http://localhost:8080", allowCredentials = "true")
public class UserController {// 方法级别配置(覆盖类配置)@GetMapping("/users")@CrossOrigin(maxAge = 1800)public List<User> getUsers() {return userService.getAllUsers();}
}

方案三:过滤器配置(适合WebFlux或特殊需求)

创建自定义CORS过滤器:

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;@Configuration
public class GlobalCorsConfig {@Beanpublic CorsFilter corsFilter() {CorsConfiguration config = new CorsConfiguration();// 允许所有域名(生产环境建议指定具体域名)config.addAllowedOriginPattern("*");// 允许发送Cookieconfig.setAllowCredentials(true);// 允许所有请求头config.addAllowedHeader("*");// 允许所有方法config.addAllowedMethod("*");// 预检请求缓存时间config.setMaxAge(3600L);UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();source.registerCorsConfiguration("/**", config);return new CorsFilter(source);}
}

常见问题解决方案

  1. Credentials 与 Origin 冲突问题

    • 现象:前端携带 Cookie 时报错
    • 解决:
      // 正确配置
      .allowCredentials(true)
      // 使用 allowedOriginPatterns 替代 allowedOrigins
      .allowedOriginPatterns("http://trusted-domain.com") 
      
    • 前端配合:
      fetch(url, {credentials: 'include' // 必须
      })
      
  2. Spring Security 集成问题
    在 Security 配置中启用 CORS:

    @Override
    protected void configure(HttpSecurity http) throws Exception {http.cors() // 启用 CORS.and()// ... 其他安全配置
    }
    
  3. OPTIONS 请求被拦截

    • 确保未手动拦截 OPTIONS 方法
    • 在 Security 配置中添加:
      .antMatchers(HttpMethod.OPTIONS).permitAll()
      
  4. 多配置冲突

    • 优先级:过滤器 > 全局配置 > @CrossOrigin
    • 建议整个项目统一使用一种配置方式

最佳实践建议

  1. 开发环境
    使用全局配置 + allowedOriginPatterns("*") 快速开发

  2. 生产环境

    .allowedOriginPatterns("https://your-domain.com","https://api.your-domain.com"
    )
    
  3. 安全加固

    // 限制允许的请求头
    .allowedHeaders("Content-Type", "X-Requested-With", "Authorization")
    // 暴露特定响应头
    .exposedHeaders("Custom-Header")
    

重要提示

  • Spring Boot 2.4.x+ 必须使用 allowedOriginPatterns 替代旧版 allowedOrigins
  • 开启 allowCredentials(true) 时,禁止使用 allowedOriginPatterns("*")(浏览器安全限制),应指定具体域名
http://www.dtcms.com/a/281877.html

相关文章:

  • Spring Boot - Spring Boot 集成 MyBatis 分页实现 手写 SQL 分页
  • 日语学习-日语知识点小记-构建基础-JLPT-N3阶段(5):语法+单词
  • Buffer Pool
  • css 如何实现大屏4个占位 中屏2个 小屏幕1个
  • Samba服务器
  • Git版本控制完全指南:从入门到精通
  • 网络编程/Java面试/TCPUDP区别
  • 基于spring boot养老院老人健康监护平台设计与实现
  • SFT:大型语言模型专业化定制的核心技术体系——原理、创新与应用全景
  • docker run elasticsearch 报错
  • JAVA面试宝典 -《分布式ID生成器:Snowflake优化变种》
  • 详解SPFA算法-单源最短路径求解
  • C++ - 仿 RabbitMQ 实现消息队列--sqlite与gtest快速上手
  • 基于springboot+vue的酒店管理系统设计与实现
  • 一叶障目不见森林
  • 身份证号码姓名认证解决方案-身份证三要素API接口
  • Apache IoTDB(1):时序数据库介绍与单机版安装部署指南
  • 更灵活方便的初始化、清除方法——fixture【pytest】
  • QT跨平台应用程序开发框架(9)—— 容器类控件
  • 城市守护者的蓝色印记
  • Qt小组件 - 5 图片懒加载样例
  • 【MAC】nacos 2.5.1容器docker安装
  • Python面向对象编程(OOP)详解:通俗易懂的全面指南
  • 高性能架构模式——高性能缓存架构
  • python的慈善捐赠平台管理信息系统
  • 【前端】在Vue3中绘制多系列柱状图与曲线图
  • rocky8 --Elasticsearch+Logstash+Filebeat+Kibana部署【7.1.1版本】
  • 阿奇霉素:长效广谱抗菌背后的药理特性与研发历程
  • 利用Java自定义格式,循环导出数据、图片到excel
  • excel分组展示业绩及增长率