Express-StormPath身份验证

时间:2016-04-22 22:18:17

标签: node.js express stormpath

我正在尝试使用StormPath访问令牌对node.js表示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);
});

后续页面访问(重定向到登录页面)

https://localhost:8000/about

注意:在StormPath文档中,我假设cookie仅为https会话创建,因此我创建了具有通用名称“localhost”的自签名证书,并使用“https://localhost:8000”运行请求。

1 个答案:

答案 0 :(得分:2)

与罗伯特通过电话并通过为访问令牌创建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
    });