Express-StormPathauthentication

我正在尝试使用StormPath访问令牌对node.js express Web应用程序进行身份validation。 初始页面启动(索引)工作正常,但后续访问(约)不能按预期使用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); }); 

后续页面访问(redirect到login页面)

https://开头本地主机:8000 /约

注意:从StormPath文档中,我认为cookies只是为https会话创build的,所以我创build了具有通用名“localhost”的自签名证书,并使用“ https:// localhost:8000 ”运行请求。

与Robert打过电话,并通过创build访问令牌的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 });