1

在我的 api 验证节点应用程序中,我在passport-azure-ad包中使用BearerStrategy 。

文档中,指定

用户向受保护的 Web api 发送请求,该请求在授权标头或正文中包含 access_token。

如果访问令牌存储在cookie 标头而不是授权标头中,是否可以验证 api?

代码如下:

const authenticationStrategy = new BearerStrategy(config.credentials, (token, 
done) => {

let currentUser = null;
let userToken = authenticatedUserTokens.find((user) => {
currentUser = user;
user.sub === token.sub;
});

if (!userToken) {
    authenticatedUserTokens.push(token);
}

return done(null, currentUser, token);
 });

passport.use(authenticationStrategy);

server.get('/api/test', passport.authenticate('oauth-bearer', {
session: false
}), (req, res, next) => {
   res.send({"message":"Success"});
   return next();
 });

因此,如果我在 cookie 标头中传递访问令牌 - 它未经过验证。我应该使用其他一些包,如护照 cookie 吗?那么如何通过 Azure 凭据与 Azure Active Directory 集成?

4

1 回答 1

0

访问令牌也可以通过浏览器 cookie 传输。您选择哪种传输方法(cookie 标头或授权标头)取决于您的应用程序和用例。对于移动应用程序,标题是要走的路。

对于 Web 应用程序,建议使用 HttpOnly cookie 而不是 HTML5 存储/标头,以更好地抵御 XSS 攻击。请务必注意,使用 cookie 意味着您需要保护表单免受 CSRF 攻击。

于 2020-07-17T10:38:08.160 回答