springboot配置跨域问题

             近期自己搭建项目时,遇到一个跨域问题。我们以前项目解决跨域是在controller上加一个跨域注解@CrossOrigin(allowCredentials = "true"),很方便。但是在我自己搭建的项目中,启动时竟然报错了,错误如下:

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.

这就很奇怪了,为什么直接拿过来不能用了呢?

错误大概意思是:

当allowCredentials为真时,allowedorigin不能包含特殊值"*",因为不能在"访问-控制-起源“响应头中设置该值。要允许凭证到一组起源,显示地列出它们,或者考虑使用"allowedOriginPatterns”代替。

原来是以前的项目springboot是2.1,而我自己搭建的项目采用springboot2.7版本。根据错误提示,可以改成如下写法就可以了。

@CrossOrigin(allowCredentials = "true",originPatterns = "/**")

原因是在springboot2.4版本之后,不允许origins含有特殊值星号

从注解@CrossOrigin源码可以看出,origins默认是*号

通过查看源码,我认为这样写也是OK的,如:

@CrossOrigin(allowCredentials = "true",origins = "http://localhost:8010/")

到此为止跨域报错问题解决了。

其实解决跨域有两种方式,一种是利用注解,一种写个配置类。各有利弊吧,跨域注解需要在对应的controoler上加注解,配置类写一次就可以了。

第一种方式:利用注解

@CrossOrigin(allowCredentials = "true",origins = "http://localhost:8010/")
//@CrossOrigin(allowCredentials = "true",originPatterns = "/**")

这2种注解都可以

第二种方式:配置类

package com.lsl.exam.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 跨域配置WebMvcConfig
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    /**
     * 配置跨域
     * allowedOriginPatterns 和 allowedOrigins都是配置跨域请求来源的,
     * 前者支持通配符,后者不支持通配符,只能字符串
     * 取其一种即可
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry){
        registry.addMapping("/**")//配置当前项目哪些路径支持跨域。这是所有路径都支持跨域
//                .allowedOriginPatterns("*")//配置跨域请求来源。通配符配置方式。这是所有请求来源都可以
                .allowedOrigins("http://localhost:8010","http://www.exam.com:8010")//配置跨域请求来源,字符串配置,配置这有这2个请求来源的跨域请求访问
                .allowedMethods("GET","POST")//设置允许所有请求方式
                .allowedHeaders("*")//设置允许的header
                .allowCredentials(true)//设置是否发送cookie信息
                .maxAge(3600);
    }
}

 allowedOriginPatterns 和 allowedOrigins的区别?

     allowedOriginPatterns 和 allowedOrigins 都是用来设置允许跨域请求的来源,其中 allowedOriginPatterns 是在 Spring 5.3 版本引入的新方法,而 allowedOrigins 是旧版本中的方法。

        它们的主要区别在于使用方式和匹配规则。allowedOrigins 方法使用的是字符串匹配,即只能指定具体的跨域来源,而不能使用通配符;而 allowedOriginPatterns 方法使用的是 Ant 风格的路径匹配规则,可以使用通配符来匹配多个来源

举个例子,如果你的前端应用需要从 http://localhost:8080 和 https://www.exam.com 两个不同的来源获取数据,allowedOrigins你可以这样配置

registry.addMapping("/**")
        .allowedOrigins("http://localhost:8080", "https://www.exam.com")
        .allowedMethods("GET", "POST")
        .allowCredentials(true)
        .maxAge(3600);

        这段代码表示,允许来自 http://localhost:8080 和 https://www.exam.com 这两个来源的跨域请求访问任何请求路径,允许的 HTTP 方法包括 GET 和 POST,启用允许发送凭据,预检请求的缓存时间为 1 小时。

使用 allowedOriginPatterns 方法可以这样配置:

        

registry.addMapping("/**")
        .allowedOriginPatterns("http://localhost:*", "https://*.exam.com")
        .allowedMethods("GET", "POST")
        .allowCredentials(true)
        .maxAge(3600);

        这段代码表示,允许来自以 http://localhost: 开头的任意端口和以 https:// 开头的以 .exam.com 结尾的任意二级域名的跨域请求访问任何请求路径,允许的 HTTP 方法包括 GET 和 POST,启用允许发送凭据,预检请求的缓存时间为 1 小时。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一路奔跑1314

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值