须要解决前端pc跟服务端(java),跨域后都能获取到同一个cookie。
使用二级域名共享cookie有一个限制条件,就是两个域名的二级域名必须相同前端
前端pc访问域名:a.b.com
后端接口域名:a-gateway.b.com
这两个域名同属一个二级域名:b.comvue
服务器nginx增长如下配置,便可解决跨域访问的问题。也能够在程序中经过代码解决跨域访问。java
location / {
#是否容许跨域发送Cookie
add_header Access-Control-Allow-Credentials true;
add_header Access-Control-Allow-Origin 'http://a.b.com';
add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
if ($request_method = 'OPTIONS') {
return 204;
}
}
复制代码
若是须要容许跨域携带发送cookie的话,nignx则须要如下参数配置ios
import axios from 'axios';
axios.defaults.withCredentials=true //容许携带cookie
复制代码
public static void addCookie(HttpServletResponse response,String cookieName,String cookieValue,int maxAge){
Cookie cookie =new Cookie(cookieName,cookieValue);
cookie.setDomain("b.com");//指定域名
cookie.setPath("/");//设置cookie的生命周期
cookie.setHttpOnly(false);
if(maxAge>0){
cookie.setMaxAge(maxAge);
}
response.addCookie(cookie);
}
复制代码