欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 社会 > 详细分析 Spring CORS 配置 (附Demo)

详细分析 Spring CORS 配置 (附Demo)

2025/9/1 8:01:32 来源:https://blog.csdn.net/weixin_47872288/article/details/142798778  浏览:    关键词:详细分析 Spring CORS 配置 (附Demo)

目录

  • 前言
  • 1. 基本知识
  • 2. Demo

前言

基本的Java知识推荐阅读:

  1. java框架 零基础从入门到精通的学习路线 附开源项目面经等(超全)
  2. 【Java项目】实战CRUD的功能整理(持续更新)

原先写过一篇跨域的基本知识:Springboot处理跨域的方式(附Demo)

1. 基本知识

CorsRegistry 是 Spring 框架中用于配置跨源资源共享(CORS)支持的一个类
CORS 允许服务器指示哪些来源(域)可以访问其资源,以防止潜在的跨站点请求伪造(CSRF)攻击

基本的源码如下:

public class CorsRegistry {// 存储所有的 CORS 注册信息private final List<CorsRegistration> registrations = new ArrayList<>();// 构造函数public CorsRegistry() {}// 添加 CORS 映射路径public CorsRegistration addMapping(String pathPattern) {// 创建一个新的 CorsRegistration 对象CorsRegistration registration = new CorsRegistration(pathPattern);// 将其添加到 registrations 列表中this.registrations.add(registration);// 返回创建的注册对象,以便进一步配置return registration;}// 获取所有的 CORS 配置protected Map<String, CorsConfiguration> getCorsConfigurations() {// 创建一个新的有序 Map,用于存储 CORS 配置Map<String, CorsConfiguration> configs = CollectionUtils.newLinkedHashMap(this.registrations.size());// 遍历所有的注册信息Iterator<CorsRegistration> var2 = this.registrations.iterator();while (var2.hasNext()) {CorsRegistration registration = var2.next();// 将路径模式和对应的 CORS 配置放入 Map 中configs.put(registration.getPathPattern(), registration.getCorsConfiguration());}return configs;}
}

上述的源码已经有注释

此处在单独分析下方法

  • CorsRegistry(): 构造函数,初始化 CorsRegistry 对象,创建一个空的 registrations 列表

  • addMapping(String pathPattern)
    接收一个路径模式作为参数,表示要应用 CORS 的路径
    创建一个新的 CorsRegistration 对象并将其添加到 registrations 列表中
    返回该 CorsRegistration 对象,以便进行进一步的 CORS 配置(例如设置允许的来源、方法等)

  • getCorsConfigurations()
    返回一个包含所有 CORS 配置的 Map
    遍历 registrations 列表,将每个 CorsRegistration 的路径模式和其对应的 CORS 配置放入 configs Map 中
    返回最终的 CORS 配置 Map

总的来说:

  1. CorsRegistry 类提供了一个简单而有效的方式来配置应用程序的 CORS 设置
  2. 通过 addMapping 方法,开发人员可以指定需要 CORS 支持的路径,并通过返回的 CorsRegistration 对象进一步配置具体的 CORS 规则
  3. getCorsConfigurations 方法则收集所有注册的信息,方便在应用启动时进行 CORS 配置的应用

2. Demo

这个Demo涉及一个拦截器,基本知识点可以了解:详细分析SpringMvc中HandlerInterceptor拦截器的基本知识(附Demo)

附上一个实战中的Demo:

@Configuration
public class CustomCorsConfiguration implements WebMvcConfigurer {// 添加 CORS 映射@Overridepublic void addCorsMappings(CorsRegistry registry) {registry.addMapping("/**") // 允许所有接口的跨域请求.allowCredentials(true) // 允许发送 Cookie.allowedOriginPatterns("*") // 允许所有来源.allowedMethods(new String[]{"GET", "POST", "PUT", "DELETE"}) // 允许的请求方法.allowedHeaders("*") // 允许的请求头.exposedHeaders("*"); // 暴露给客户端的响应头}
}

常用的方法如下:

函数描述
addMapping(String pathPattern)添加 CORS 映射路径
allowCredentials(boolean allowCredentials)设置是否允许发送 Cookie
allowedOriginPatterns(String… allowedOriginPatterns)设置允许的来源模式
allowedMethods(String… allowedMethods)设置允许的请求方法
allowedHeaders(String… allowedHeaders)设置允许的请求头
exposedHeaders(String… exposedHeaders)设置可以暴露给客户端的响应头

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词