后端设置Cookie前端跨域获取丢失问题(基于springboot实现)
1.跨域问题说明:后端域名为A.abc,前端域名为B.abc。
2.后端设置⼀个cookie发送给前台,domain应该是setDomain(“abc”),⽽不是setDomain(“B.abc”)
3.另外,还要实现WebMvcConfigurerr配置加⼊Cors的跨域
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(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<String> sensitiveHeaders = new LinkedHashSet<>(
springcloud和springbootArrays.asList("Cookie", "Set-Cookie", "Authorization"));
⽽我,刚好就中奖了
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论