【问题标题】:Express-StormPath AuthenticationExpress-StormPath 身份验证
【发布时间】:2016-04-22 22:18:17
【问题描述】:

我正在尝试使用 StormPath 访问令牌对 node.js express Web 应用程序进行身份验证。初始页面启动(索引)工作正常,但后续访问(关于)无法使用 cookie 会话按预期工作。下面是代码sn-p。

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”运行请求。

【问题讨论】:

  • 您好,我在Stormpath 工作,应该可以提供帮助。对于 about 页面,您的意思是使用 router.get('/', 吗?应该是router.get('/about',
  • 在 app.js 中,var routes = require('./routes/index'); var about = require('./routes/about'); app.use('/', 路线); app.use('/about', about);谢谢!

标签: node.js express stormpath


【解决方案1】:

与 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
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-04
    • 2011-06-13
    • 2016-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多