在前后端分离的开发模式中,前端调用后端接口时,经常会遇到 跨域资源共享(cors) 的问题。spring boot 作为常用的后端框架,提供了多种方式来优雅地解决这个问题。本文将全面介绍 spring boot 中处理 cors 的常见方法、原理分析及注意事项,帮助开发者高效排查和解决跨域问题。
一、什么是 cors
cors(cross-origin resource sharing,跨域资源共享)是浏览器的一种安全策略,用于防止页面被恶意地从一个域加载资源到另一个域。简单来说,只要前端请求的协议、域名、端口与后端接口不完全一致,就属于跨域请求。
例如:
- 前端地址:http://localhost:3000
- 后端接口:http://localhost:8080/api/data
这就构成了跨域请求,浏览器会默认拦截这类请求,除非服务器端明确允许跨域访问。
二、spring boot 中解决 cors 的几种方法
spring boot 提供了多种方式来配置和处理跨域问题,以下是最常用的三种方法:
方法一:使用 @crossorigin 注解(推荐用于小型项目或测试)
这是 spring boot 提供的快捷方式,可直接作用于控制器类或方法上:
@restcontroller @requestmapping("/api") @crossorigin(origins = "http://localhost:3000") public class mycontroller { @getmapping("/data") public string getdata() { return "hello, cors!"; } }
参数说明:
origins:允许的域名(可为 * 表示所有)。
methods:允许的方法,如 get, post 等。
maxage:预检请求的有效时间(单位秒)。
allowedheaders:允许携带的头信息。
缺点:对于中大型项目来说,控制器众多,维护成本高。
方法二:全局 cors 配置(推荐用于中大型项目)
通过实现 webmvcconfigurer 接口,全局统一配置跨域规则,适用于大多数项目场景。
@configuration public class corsconfig implements webmvcconfigurer { @override public void addcorsmappings(corsregistry registry) { registry.addmapping("/**") // 所有接口 .allowedorigins("http://localhost:3000") // 允许的前端地址 .allowedmethods("get", "post", "put", "delete") // 允许的方法 .allowedheaders("*") // 允许的请求头 .allowcredentials(true) // 允许携带 cookie .maxage(3600); // 预检请求缓存时间(秒) } }
优点:配置集中,维护简单,推荐使用。
方法三:通过 filter 手动设置响应头(不推荐,除非特殊需求)
手动注册一个过滤器,向响应中添加 cors 相关头信息:
@component public class corsfilter implements filter { @override public void dofilter(servletrequest request, servletresponse response, filterchain chain) throws ioexception, servletexception { httpservletresponse res = (httpservletresponse) response; httpservletrequest req = (httpservletrequest) request; res.setheader("access-control-allow-origin", "http://localhost:3000"); res.setheader("access-control-allow-methods", "get,post,put,delete,options"); res.setheader("access-control-allow-headers", "*"); res.setheader("access-control-allow-credentials", "true"); // options 预检请求直接返回 if ("options".equalsignorecase(req.getmethod())) { res.setstatus(httpservletresponse.sc_ok); } else { chain.dofilter(request, response); } } }
缺点:易出错,不易维护,建议使用 webmvcconfigurer 方式代替。
三、常见问题排查
options 请求返回 403 或没有响应?
确保后端允许 options 方法。
确保没有被 spring security 拦截。
携带 cookie 不生效?
后端必须设置:.allowcredentials(true)
前端必须设置:axios.defaults.withcredentials = true
spring security 拦截 cors 请求?
需要额外配置 cors 策略,示例如下:
@enablewebsecurity public class securityconfig { @bean public securityfilterchain filterchain(httpsecurity http) throws exception { http .cors() // 启用 cors .and() .csrf().disable() .authorizehttprequests() .anyrequest().permitall(); return http.build(); } @bean public corsconfigurationsource corsconfigurationsource() { corsconfiguration config = new corsconfiguration(); config.setallowedorigins(list.of("http://localhost:3000")); config.setallowedmethods(list.of("get", "post", "put", "delete", "options")); config.setallowedheaders(list.of("*")); config.setallowcredentials(true); urlbasedcorsconfigurationsource source = new urlbasedcorsconfigurationsource(); source.registercorsconfiguration("/**", config); return source; } }
四、总结
方法 | 场景 | 推荐度 |
---|---|---|
@crossorigin 注解 | 控制器少,接口固定 | ⭐⭐ |
实现 webmvcconfigurer | 中大型项目,统一配置 | ⭐⭐⭐⭐⭐ |
自定义 filter | 特殊需求(如动态域) | ⭐ |
cors 配置看似简单,但与前端请求设置、spring security 配合使用时需格外注意。建议在项目初始化阶段就进行统一的跨域策略设计,避免后续频繁调整。
到此这篇关于浅析springboot如何解决cors问题的文章就介绍到这了,更多相关springboot解决cors内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论