栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用CORS请求设置Cookie

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用CORS请求设置Cookie

并非立即显而易见的是,服务器设置的cookie(至少在CORS请求中,并且可能(可能)在所有请求中)都被限制在与服务器相同的域中。

index.js(Node.js服务器)

const http = require('http');const fs = require('fs');// Pretty colorsconst colors = {  purple: '33[95m',  orange: '33[93m',  blue: '33[97m',  underline: '33[4m',  bold: '33[1m',  reset: '33[0m'}const server = http.createServer(function (req, res) {  //Console logs to verify what's getting hit.  console.log(colors.purple + colors.underline + 'Hit it!' + colors.reset);  console.log(colors.orange + colors.bold + 'url:' + colors.reset, req.url);  if (//cookie/.test(req.url)) {    console.log(colors.blue + 'We need to cook(ie) Jessen' + colors.reset);    // Generate a random string in a rather convoluted way.    var randomStr = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER).toString(36) +     Math.floor(Math.random() * Number.MAX_SAFE_INTEGER).toString(36) +     Math.floor(Math.random() * Number.MAX_SAFE_INTEGER).toString(36);    randomStr = new Buffer(randomStr.toString(), 'binary').toString('base64');    // All .dev domains pointed to localhost via dnsmasq, though a hosts file    // Update should also do the trick.    res.writeHead(200, {      'Set-cookie': 'ajaxTestcookie=cookie' + randomStr + '; domain=.example.dev; HttpOnly',      'Access-Control-Allow-Origin': 'http://example.dev:3999',      'Access-Control-Allow-Credentials': 'true',      'Access-Control-Allow-Methods': 'GET, POST',      'Access-Control-Allow-Headers': 'Content-Type, Set-cookie, *'    });    return res.end('OK!');  }  console.log(colors.blue + 'We're having fun at the HTML!n' + colors.reset);  // Send out html file.   fs.readFile('./cookies.html', function (err, data) {    if (err) {      res.writeHead(500);      return res.end('Failure to launch!');    }    res.end(data.toString());  });});server.listen(3999); // api.example.dev:3999, for example

cookies.html

<html><head>  <title>cookie Test</title></head><body>  <button >Get cookies!</button>  <script>    (function() {      document.querySelector(".getcookie").addEventListener("click", function(e) {        console.log("test");        var req = new XMLHttpRequest();        // Request succeeds, but cookie will not be set!        // req.open("GET", "http://localhost:3999/cookie", true);                // This line, however, will work, assuming this page is on        // the same domain, or a subdomain of the same domain.         // (For example test.example.dev and api.example.dev)        // As long as the Access-Control-Allow-Origin Header is        // properly set to allow the domain.        req.open("GET", "http://api.example.dev:3999/cookie", true);        req.onload = function() {          console.log(req.responseText);        };        req.withCredentials = true;        req.send();      });    }());  </script></body>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/569040.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号