我正在尝试使用StormPath访问令牌对node.js express web应用程序进行身份验证。初始页面启动(索引)运行良好,但是后续访问(大约)使用cookie会话不能按预期工作。下面是代码片段。
app.js
app.use(stormpath.init(app, {
apiKey: {
id: appConfig.stormpath.id,
secret: appConfig.stormpath.secret
},
application: {
href: appConfig.stormpath.href
},
web: {
register: {
enabled: false
},
accessTokenCookie: {
domain: "localhost",
httpOnly: true,
path: "/",
secure: null
},
refreshTokenCookie: {
domain: "localhost",
httpOnly: true,
path: "/",
secure: null
}
}
}));index.js (路由器)
router.get('/', stormpath.loginRequired , function (req, res) {
res.render('index', { });
});about.js (路由器)
router.get('/', stormpath.loginRequired , function (req, res) {
res.render('about', { });
});初始启动请求提供对索引页的访问,
request({
Url: https://localhost:8000/index,
method: 'GET',
auth: {
'bearer': 'eyJraWQiOiI2R1lVQ1BHTkROM0FYNEpRWkVKVjRTSlJOIiwiYWxnIjoi'
},
rejectUnauthorized: false
}, function (err, response) {
res.send(response.body);
});后续页面访问(重定向到登录页面)
注意:从StormPath文档中,我假设cookie只为https会话创建,所以我创建了带有通用名称'localhost‘的自签名证书,并使用'https://localhost:8000’运行请求。
发布于 2016-04-26 05:32:55
与Robert进行了通话,并通过创建访问令牌的cookie解决了此问题。
var cookies = new Cookies(req, res);
if (!accesstoken) {
res.status(401).send('Unauthorized');
return;
};
cookies.set('access_token', accesstoken, {
expires: new Date(new Date().getTime() + (1000 * 60 * 60 * 8)), // 8 hours
httpOnly: true,
path: '/',
secure: false // https or http
});https://stackoverflow.com/questions/36804154
复制相似问题