使用Express http-proxy存储来自API的令牌
Storing Token from API w/ Express http-proxy
我正在设置一个通用的 React 应用程序并使用这个项目作为基础。我成功地将请求(使用 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});
});
鉴于 laravel 中来自身份验证端点的响应如下所示:
{
"token" : "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ"
}
此代码将执行您想要的操作:
// server.js
const targetUrl = 'http://' + config.apiHost + ':' + config.apiPort;
const Express = require('express');
const http = require('http');
const httpProxy = require('http-proxy');
const app = new Express();
const server = new http.Server(app);
const Cookies = require( "cookies" )
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
// this is done in the proxyRes
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
var cookies = new Cookies( req, res )
req.headers.authorization = "JWT " + cookies.get('jwt-token');
proxy.web(req, res, {target: targetUrl});
});
proxy.on('proxyRes', function(proxyRes, req, res) {
if (req.originalUrl === '/auth') {
var cookies = new Cookies( req, res )
var body = '';
var _write = res.write;
var _end = res.end;
var _writeHead = res.writeHead;
var sendHeader = false;
res.writeHead = function () {
if (sendHeader) {
_writeHead.apply( this, arguments );
}
}
res.write = function (data) {
body += data;
}
res.end = function () {
sendHeader = true;
var parsed = JSON.parse(body);
cookies.set('jwt-token', parsed.token);
_write.apply(this, [ body ]);
_end.apply(this, arguments);
}
}
});
相关文章:
- 如何使用Google撤销Oauth令牌'的Javascript API
- javascript api,用于在第三方域上存储身份验证令牌
- 使用Soundcloud API中的令牌保持登录状态
- 使用用户名/API令牌javascript的Jenkins POST请求
- Stripe API:没有ID为X的令牌(Stripe.cocharges.create)
- 如何使用访问令牌与Facebook Graph API
- Sencha 2.0 和 Codeigniter RESTful API 生成未捕获的语法错误:意外令牌:
- 授权客户端JS API调用Google'的具有现有访问令牌的gap库
- 使用Twilio JS客户端API时出现JWT令牌过期问题
- 如何添加api令牌作为我向api发出的每个HTTP请求的用户名
- 如何通过AngularJS$http从ASP.Net Web API 2获取访问令牌
- Graph API不接受使用JavaScript SDK的访问令牌
- 如何从服务器进行Facebook API调用?要使用的令牌
- 如何将访问令牌用于Facebook Opengraph javascript SDK api请求
- 使用基于令牌的身份验证保护Sails.js中的API路由
- AngularJS身份验证,带有一个restful api,成功时返回一个令牌
- 如何获取用户访问令牌,与图形 API 资源管理器中使用的令牌相同
- 从Pinterest API v3获取登录令牌的正确方法
- 如何使用 JavaScript 访问令牌一个驱动器 API
- 保护API令牌,使其成为我的index.js中的一个var