cookies丢失 同域名_后端设置Cookie前端跨域获取丢失问题(基于springboot实现)
1.跨域問題說明:后端域名為A.abc.com,前端域名為B.abc.com。
2.后端設(shè)置一個(gè)cookie發(fā)送給前臺(tái),domain應(yīng)該是setDomain(“abc.com”),而不是setDomain(“B.abc.com”)
3.另外,還要實(shí)現(xiàn)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--------------------------------
由于之前的項(xiàng)目要搬到springcloud上面,所有就有了zuul網(wǎng)關(guān)來管理所有的請求,之前cookie設(shè)置的請求頭Authoriaztion居然沒有被傳到前端。
涼涼……
設(shè)置網(wǎng)關(guān)層跨域問題都已經(jīng)全部允許任何請求頭(下圖),但是還是前端訪問還是沒有Authoriaztion,各種問題都排查了,都沒有問題。。。大寫的迷惘!!!
后來啊,干脆把Authoriaztion名字給改了,直接改為token。
艸,居然可以了,前端能拿到token;改回Authoriaztion,沒有。。。
后來查了資料,才發(fā)現(xiàn)哦,zuul會(huì)默認(rèn)過濾掉幾個(gè)敏感詞,沒錯(cuò),就是它:
/**
* 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"));
而我,剛好就中獎(jiǎng)了!!!
總結(jié)
以上是生活随笔為你收集整理的cookies丢失 同域名_后端设置Cookie前端跨域获取丢失问题(基于springboot实现)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 沉冤得雪是什么意思 沉冤得雪的解释
- 下一篇: 怎么取消校讯通 怎么取消校讯通方法一览