cookies丢失 同域名_后端设置Cookie前端跨域获取丢失问题(基于springboot实现)

本文讲述了在SpringBoot后端设置Cookie时遇到的跨域问题,强调了domain应设为顶级域名,并配置CORS以允许跨域。同时,当使用Zuul作为网关时,由于Zuul默认过滤了敏感头部如Authorization,导致前端无法获取,解决方案是更改Header名称或配置Zuul以允许传递Authorization。
摘要由CSDN通过智能技术生成

1.跨域问题说明:后端域名为A.abc.com,前端域名为B.abc.com。

2.后端设置一个cookie发送给前台,domain应该是setDomain(“abc.com”),而不是setDomain(“B.abc.com”)

3.另外,还要实现WebMvcConfigurerr配置加入Cors的跨域

@Configurationpublic classWebConfig implements WebMvcConfigurer {

@Overridepublic voidaddCorsMappings(CorsRegistry registry) {

registry.addMapping("/**").allowedOrigins("*").allowedMethods("GET", "POST", "OPTIONS", "PUT")

.allowedHeaders("Content-Type", "X-Requested-With", "accept", "Origin", "Access-Control-Request-Method","Access-Control-Request-Headers")

.exposedHeaders("Access-Control-Allow-Origin", "Access-Control-Allow-Credentials")

.allowCredentials(true).maxAge(3600);

}

}

--------------------------------------------分割线2018-9-16--------------------------------

由于之前的项目要搬到springcloud上面,所有就有了zuul网关来管理所有的请求,之前cookie设置的请求头Authoriaztion居然没有被传到前端。

凉凉……

设置网关层跨域问题都已经全部允许任何请求头(下图),但是还是前端访问还是没有Authoriaztion,各种问题都排查了,都没有问题。。。大写的迷惘!!!

后来啊,干脆把Authoriaztion名字给改了,直接改为token。

艹,居然可以了,前端能拿到token;改回Authoriaztion,没有。。。

后来查了资料,才发现哦,zuul会默认过滤掉几个敏感词,没错,就是它:

/**

* List of sensitive headers that are not passed to downstream requests. Defaults to a

* "safe" set of headers that commonly contain user credentials. It's OK to remove

* those from the list if the downstream service is part of the same system as the

* proxy, so they are sharing authentication data. If using a physical URL outside

* your own domain, then generally it would be a bad idea to leak user credentials.

*/

private Set sensitiveHeaders = new LinkedHashSet<>(

Arrays.asList("Cookie", "Set-Cookie", "Authorization"));

而我,刚好就中奖了!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值