0

웹 응용 프로그램을 개발 중이며 Google 크롬 60.0.3112.113을 사용하여 테스트하고 있습니다.HTTP 응답이 쿠키를 설정하지 못했습니다.

개발 프로세스를 간소화하기 위해 http-proxy-middleware과 함께 node.js 개발 웹 서버를 사용하여 내 API 요청을 백엔드에 프록시합니다.

직접 응답
HTTP/1.1 200 OK 
Content-Length: 122 
Content-Type: application/json 
Set-Cookie: sessionid={4621f755-37da-41da-bdbd-9a6ce0ee02b7}; Max-Age=31536000; Version=1 
: 내 백엔드에서 세션을 생성하는 API 엔드 포인트 중 하나에 axios를 사용하여 HTTP POST 요청을 보낼 때

는 지금, 나는 (DevTools로 복사) 다시 약간 변경된 응답 헤더를 얻을 프록시 사용 응답은

HTTP/1.1 200 OK 
X-Powered-By: Express 
connection: close 
content-length: 122 
content-type: application/json 
set-cookie: sessionid={4621f755-37da-41da-bdbd-9a6ce0ee02b7}; Max-Age=31536000; Version=1 
Date: Thu, 07 Sep 2017 11:06:43 GMT 

문제 크롬 그러나 직접적인 반응은 예상대로 쿠키를 설정하고, (DevTools-> 응용 프로그램 -> 스토리지 기반> 쿠키가 비어 유지) 프록시 응답에 지정된 쿠키를 설정하지 않습니다.

쿠키는 DevTools-> Network->내 요청 -> 쿠키에 올바르게 표시됩니다. (직접 및 프록시) 두 버전 모두 http://localhost:[8080/3000]

를 통해 액세스되는 소문자 set-cookie 헤더는 크롬에서 무시 될 수 있습니까? 다른 헤더가 쿠키 설정을 방해 할 수 있습니까?

는 Btw는 : Safari에서 10.1.2을 잘 작동 (12603.3.8)

답변

0
// proxy middleware options 
var options = { 
    target: 'http://localhost:8081', // target host 
    changeOrigin: true,    // needed for virtual hosted sites 
    ws: true,       // proxy websockets 
    logLevel: "debug", 
    pathRewrite: { 
     '^/src/api/' : '/api/' 
    }, 
    onProxyRes: function (proxyRes, req, res) { 
     if (proxyRes.headers['set-cookie'] != undefined) { 
      req.session['cookie'] = proxyRes.headers['set-cookie']; // must be or you will get new session for each call 
      req.session['proxy-cookie'] = proxyRes.headers['set-cookie']; // add to other key because cookie will be lost 
     } 
     console.log("response: " + req.session.id); 
     console.log(req.session); 
    }, 
    onProxyReq: function (proxyReq, req, res) { 
     // check for whether the session be freshed 
     if (req.session.view) 
      req.session.view ++; 
     else 
      req.session.view = 1; 

     // use ower key to restore cookie 
     if (req.session['proxy-cookie'] != undefined) 
      proxyReq.setHeader('cookie', req.session['proxy-cookie'][0]); 

     console.log("request: " + req.session.id); 
     console.log(req.session); 
    } 
}; 
+0

는 일반적으로, 답변이 왜이 코드가 수행하는 역할에 대한 설명을 포함하면 훨씬 더 유용하고, 해결할 수있는 문제가 다른 사람을 소개하지 않고 문제. –

+0

주석은 코드에 포함되어 있습니다. –