【发布时间】:2016-05-26 23:49:06
【问题描述】:
我正在设置一个通用的 React 应用程序并使用 this project 作为基础。我成功地将请求(使用http-proxy)代理到我的 Laravel 后端。但是,我是 Nodejs 的新手,我不知道如何将 JWT 从代理服务器安全地存储到客户端的最佳方法。
我最初的想法是将令牌存储到 localStorage,但问题是快递服务器无法访问它。所以我的下一个猜测是将其存储为 cookie,但我不确定如何将其存储在客户端或将其作为所有传出请求的标头包含在内(此外,我可能需要某种 csrf 中间件)。
那么我将如何操纵来自我的 api 服务器的响应以将令牌放入客户端中设置的 cookie 中,然后将其用作所有 api 请求的不记名令牌?
// server.js
const targetUrl = 'http://' + config.apiHost + ':' + config.apiPort;
const app = new Express();
const server = new http.Server(app);
const proxy = httpProxy.createProxyServer({
target: targetUrl,
changeOrigin: true
});
// Proxy to Auth endpoint
app.use('/auth', (req, res) => {
// on a successful login, i want to store the token as a cookie
proxy.web(req, res, {target: targetUrl});
});
// Proxy to api endpoint
app.use('/api', (req, res) => {
// use the token in the cookie, and add it as a authorization header in the response
proxy.web(req, res, {target: targetUrl});
});
【问题讨论】:
标签: javascript node.js express cookies proxy