2016-05-16 75 views
0

假设我在两个不同的域上有两个应用程序。一个应用程序是一个在线商店,另一个应用程序是附属管理面板。如何从其他域加载Cookie

任务是:如果用户是会员,给他看一个特殊的标题。

问题:如果我的用户在那里登录,我该如何去询问我的会员管理员面板?我不能只做一个异步调用,因为这些cookie不会以这种方式返回。我必须使用iframe还是有另一种方式?

+0

http://www.opentracker.net/article/third-party-cookies-vs-first-party-cookies,https:// en .wikipedia.org/wiki/HTTP_cookie#Privacy_and_third-party_cookies – Rob

+0

您不能通过javascript向管理域发送请求。管理域上的cookie需要有正确的许可思想 –

回答

1

配置管理面板服务器上的特殊终端(运行Nginx的):

location /hack_cookie { 
    gzip_static on; 
    gzip on; 
    gzip_proxied any; 
    if ($http_referer ~* (your_app_domain)) { 
     add_header 'Access-Control-Allow-Origin' $http_origin; 
     add_header 'Access-Control-Allow-Credentials' 'true'; 
     add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; 
     add_header 'Access-Control-Allow-Headers' 'your_cookie,DNT,X-CustomHeader,Keep-Alive,User- Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type'; 
     add_header your_cookie $cookie_your_cookie; 
     add_header 'Access-Control-Max-Age' 1728000; 
     add_header 'Content-Type' 'text/plain charset=UTF-8'; 
     return 200 'Ok'; 
    } 
} 

然后在您的应用程序:

var req = new XMLHttpRequest(); 
req.open('GET', 'https://www.admin_panel_domian.com/hack_cookie', false); 
req.withCredentials = true; 
req.send(); 
req.onload = function(){ 
    var headers = req.getAllResponseHeaders(); 
} 

现在只需解析headers字符串,它看起来就像这样:

date: Tue, 17 May 2016 06:46:52 GMT 
server: nginx/1.10.0 
access-control-max-age: 1728000 
access-control-allow-methods: GET, POST, OPTIONS 
content-type: text/plain charset=UTF-8 
status: 200 
access-control-allow-credentials: true 
your_cookie: fancyjohn 
access-control-allow-headers: your_cookie,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type 
content-length: 2 
相关问题