2016-08-15 28 views
1

我正在使用由John Papa提供的作爲開發服務器的chimurai 的HTTP代理中間件的lite服務器。 問題出在我的會話cookie上,我無法堅持來自真實服務器的會話cookie。 我看到這個解決方案: https://github.com/chimurai/http-proxy-middleware/issues/78http-proxy-middleware,如何複製所有/ cookie標頭

,但我看不出有什麼相似之處我BS-config.js:

var proxy = require('http-proxy-middleware'); 

module.exports = { 
    port: 3003, 
    server: { 
     middleware: { 
      1: proxy('demo/webservice/jaxrs', { 
       target: 'https://localhost:8443', 
       secure: false, // disable SSL verification 
       changeOrigin: true // for vhosted sites, changes host header to match to target's host 
      }), 
      2: require('connect-history-api-fallback')({index: '/index.html', verbose: true}) 
     } 
    } 
}; 

是否有人知道如何合併這兩?

UPDATE:這是響應頭的一部分:

set-cookie:JSESSIONID=620083CD7AEB7A6CC5772AC800E673E3; Path=/appServer/webservice/jaxrs; Secure 
strict-transport-security:max-age=31622400; includeSubDomains 
Transfer-Encoding:chunked 

UPDATE2: 我想我的配置應該是這樣的:

var proxy = require('http-proxy-middleware'); 

function relayRequestHeaders(proxyReq, req) { 
    Object.keys(req.headers).forEach(function (key) { 
     proxyReq.setHeader(key, req.headers[key]); 
    }); 
}; 

function relayResponseHeaders(proxyRes, req, res) { 
    Object.keys(proxyRes.headers).forEach(function (key) { 
      res.append(key, proxyRes.headers[key]); 
     }); 
}; 

module.exports = { 
    port: 3003, 
    server: { 
     middleware: { 
      1: proxy('/skybox', { 
       target: 'https://localhost:8443', 
       secure: false, // disable SSL verification 
       changeOrigin: true, // for vhosted sites, changes host header to match to target's host 
       onProxyReq: relayRequestHeaders, 
       onProxyRes: relayResponseHeaders 
      }), 
      2: require('connect-history-api-fallback')({index: '/index.html', verbose: true}) 
     } 
    } 
}; 

但現在res.append未定義:(

+0

你能提供來自目標的RAW迴應?該RAW響應中的「set-cookie」值是多少? – chimurai

+0

我更新了我原來的帖子。這是會話cookie:set-cookie:JSESSIONID = 620083CD7AEB7A6CC5772AC800E673E3;路徑=/APPSERVER/web服務/ JAXRS;安全 – Avi

+0

你還可以提供正在被激發到端口3003上的服務器的RAW請求嗎? – chimurai

回答

1

不知道如何本地主機:3003配置;帶或不帶https: ...

假設您使用的是http://localhost:3000(不是https :); 目標中的Secure cookie屬性可能會導致瀏覽器忽略cookie。

4.1.2.5。安全屬性

安全屬性將Cookie的範圍限制爲「安全」通道(其中「安全」由用戶代理定義)。當
餅乾具有安全屬性,用戶代理將包括在僅當請求是通過
安全信道傳輸(通常HTTP傳輸層安全性的HTTP請求的
餅乾(TLS)

源:https://tools.ietf.org/html/rfc6265#section-4.1.2.5

瀏覽器可以省略基於算法的Cookie中描述:https://tools.ietf.org/html/rfc6265#section-5.4

嘗試取出Secure Attribute和看看是否有幫助

+0

當移動到沒有安全標誌的http時,我確實得到了會話。這是否意味着我無法使用https或安全cookie與瀏覽器同步? – Avi

+0

你還可以。只需在瀏覽器同步中啓用'https'選項:'browserSync({0}};''這樣安全cookie可能不會被拒絕。 – chimurai

0

嘗試:

var cookie; 
function relayRequestHeaders(proxyReq, req) { 
    if (cookie) { 
    proxyReq.setHeader('cookie', cookie); 
    } 
}; 

function relayResponseHeaders(proxyRes, req, res) { 
    var proxyCookie = proxyRes.headers["set-cookie"]; 
    if (proxyCookie) { 
    cookie = proxyCookie; 
    } 
}; 

它與精簡版服務器的工作