SpringBoot解决跨域
原文链接: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 */ @SpringBootConfiguration public 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. 添加映射路径 UrlBasedCorsConfigurationSource 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("*");
相关文章
- SpringBoot 解决“不支持发行版本xx”的问题
- 最新JVM技术:基于Springboot 3 对GraalVM Native Image进行构建、部署测试
- Springboot: ApplicationRunner、CommandLineRunner的应用场景、区别及使用示例
- SpringBoot——加载配置文件
- SpringBoot集成kafka全面实战
- 前端请求SpringBoot接口出现Required request body is missing
- SpringBoot入门-解决跨域问题
- 《SpringBoot篇》01.Springboot超详细入门(基础篇)
- SpringBoot 框架整合
- springboot 跨域问题配置
- SpringBoot自定义注解,Springboot权限角色注解
- Springboot 整合 MyBatisPlus「详细过程」
- springboot学习之七(整合spring swagger)
- 解决SpringBoot跨域的三种方式
- Springboot跨域处理的几种方式
- 【springBoot】SpringBoot修改启动logo图案
- SpringBootApplication是如何启动Tomcat的? | 破解SpringBoot Tomcat启动之谜 !
- 真正实现前后端分离架构,通过SpringBoot整合Jpa做构建后端服务器,前端通过Vue结合Element-UI和axios进行数据请求,解决了跨域请求问题、用户登录验证、百度地图根据坐标标记
- SpringBoot (3)设置支持跨域请求
- SpringBoot的文件上传