我们有使用 MEAN 堆栈编写的应用程序。现在应用程序正在使用本地身份验证。我们正在尝试用 Azure AD 身份验证替换它。
有一篇不错的文章here显示如何使用 ADAL.JS 库为 Azure 身份验证配置 Angular。这将保护客户端资源。在示例中,服务器端 API 是使用 .Net Web API 编写的,并且使用 OWIN 来保护 Web API。因此 OWIN 负责验证从客户端发送的 Bearer token 。
使用 MEAN 堆栈,服务器端 API 是用 Node.js 编写的,那么如果我们切换到 Azure AD,我们如何保护 Node.js API? Microsoft 是否有可用的 Node 模块?任何示例都会受到极大的赞赏。
最佳答案
我最近使用 Nodejs 后端和 passport-azure-ad
实现了一个 React 应用程序
您可以引用我的帖子进行授权和身份验证 https://stackoverflow.com/a/58761942/8238968
您可以在 https://github.com/AzureADQuickStarts/AppModelv2-WebAPI-nodejs/blob/master/node-server/config.js 找到 BearerStrategyOptions 的键值
此外,仅供引用,我使用了以下公共(public)端点 https://login.microsoftonline.com/common/v2.0/.well-known/openid-configuration对于身份元数据
const BearerStrategyOptions = {
identityMetadata,
clientID,
validateIssuer,
issuer,
passReqToCallback,
allowMultiAudiencesInToken,
audience
};
授权:
passport.use(
new BearerStrategy(BearerStrategyOptions, function(token, done) {
console.log("verifying the user");
console.log(token, "was the token retreived");
findByOid(token.oid, function(err, user) {
if (err) {
return done(err);
}
if (!user) {
// "Auto-registration"
console.log(
"User was added automatically as they were new. Their oid is: ",
token.oid
);
users.push(token);
owner = token.oid;
return done(null, token);
}
owner = token.oid;
return done(null, user, token);
});
})
);
要授权路由,请在您的 api 中使用以下代码
passport.authenticate('oauth-bearer', {session: false})
完成!希望这有帮助:)
关于angularjs - 如何使用 Azure AD 授权 Node.js API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36558526/