SpringBoot解决跨域

本文介绍了SpringBoot处理跨域问题的五种方式,包括配置CorsFilter、重写WebMvcConfigurer、使用@CrossOrigin注解以及手动设置响应头。这些方法主要目的是修改响应头以实现跨域。全局配置适用于所有接口,局部配置可以进行更细粒度的控制。在高版本SpringBoot中需要注意,当allowCredentials为true时,allowedOrigins不能设置为*,需要改为allowedOriginPatterns(*)。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文链接:https://www.cnblogs.com/antLaddie/p/14751540.html

SpringBoot解决跨域问题

复制代码
五种解决方式:①:返回新的CorsFilter②:重写WebMvcConfigurer③:使用注解@CrossOrigin④:手动设置响应头(HttpServletResponse)参考第一章第四节
注意:CorFilter / WebMvConfigurer / @CrossOrigin 需要 SpringMVC 4.2以上版本才支持,对应springBoot 1.3版本以上上面前两种方式属于全局 CORS 配置,后两种属于局部 CORS配置。如果使用了局部跨域是会覆盖全局跨域的规则,
所以可以通过 @CrossOrigin 注解来进行细粒度更高的跨域资源控制。其实无论哪种方案,最终目的都是修改响应头,向响应头中添加浏览器所要求的数据,进而实现跨域
复制代码

1:配置CorsFilter(全局跨域)

复制代码
import org.springframework.boot.SpringBootConfiguration;import org.springframework.context.annotation.Bean;import org.springframework.web.cors.CorsConfiguration;import org.springframework.web.cors.UrlBasedCorsConfigurationSource;import org.springframework.web.filter.CorsFilter;/** * @Author AnHui_XiaoYang * @Email 939209948@qq.com * @Date 2021/5/10 17:07 * @Description  */@SpringBootConfigurationpublic class WebGlobalConfig {    @Bean    public CorsFilter corsFilter() {        //创建CorsConfiguration对象后添加配置        CorsConfiguration config = new CorsConfiguration();        //设置放行哪些原始域        config.addAllowedOrigin("*");        //放行哪些原始请求头部信息        config.addAllowedHeader("*");        //暴露哪些头部信息        config.addExposedHeader("*");        //放行哪些请求方式        config.addAllowedMethod("GET");     //get        config.addAllowedMethod("PUT");     //put        config.addAllowedMethod("POST");    //post        config.addAllowedMethod("DELETE");  //delete        //corsConfig.addAllowedMethod("*");     //放行全部请求        //是否发送Cookie        config.setAllowCredentials(true);        //2. 添加映射路径        UrlBasedCorsCon 江西干部培训学校 www.ctbuganxun.com figurationSource corsConfigurationSource =                new UrlBasedCorsConfigurationSource();        corsConfigurationSource.registerCorsConfiguration("/**", config);        //返回CorsFilter        return new CorsFilter(corsConfigurationSource);    }}
复制代码

如果你使用的是高版本SpringBoot2.4.4则需要改动一下,否则后台报错

java.lang.IllegalArgumentException: When allowCredentials is true, allowedOrigins cannot contain the special value "*" since that cannot be set on the "Access-Control-Allow-Origin" response header. To allow credentials to a set of origins, list them explicitly or consider using "allowedOriginPatterns" instead.
at org.springframework.web.cors.CorsConfiguration.validateAllowCredentials(CorsConfiguration.java:453) ~[spring-web-5.3.6.jar:5.3.6]

当allowCredentials为true时,alloedOrigins不能包含特殊值“*”,因为该值不能在“Access-Control-Allow-Origin”响应头部中设置。要允许凭据访问一组来源,请显式列出它们或考虑改用“AllowedOriginPatterns”。

解决:把 config.addAllowedOrigin("*"); 替换成 config.addAllowedOriginPattern("*");

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值