azure - Nestjs - 实现 Azure-Ad Passport 身份验证

标签 azure authentication nestjs passport.js passport-azure-ad

如何实现 Azure-Ad Passport 身份验证?找不到任何相关文档,在网上看到有问题。

最佳答案

将 MSAL 用于前端。 对于后端,请使用 Passport 和 passport-azure-ad npm。 enter image description here

  • 然后,您应该在 Azure AD 中创建应用
  • 然后,获取tenantId并 应用设置页面中的 appId,
  • 然后,使用类似的代码来获取访问权限 token 并检查用户身份验证。

//我的 auth-guard.ts

import { AuthGuard, PassportStrategy } from '@nestjs/passport';
import { BearerStrategy } from 'passport-azure-ad';
import { Injectable } from '@nestjs/common';


@Injectable()
export class AzureADStrategy extends PassportStrategy(
  BearerStrategy,
  'azure-ad',
) {
  constructor() {
    super({
      identityMetadata: `https://login.microsoftonline.com/${tenantID}/v2.0/.well-known/openid-configuration`,
      clientID,
    });
  }

  async validate(data) {
    return data;
  }
}

export const AzureADGuard = AuthGuard('azure-ad');

// app.controller.ts

  @UseGuards(AzureADGuard)
  @Get('/api')
  get_api(): string {
    return 'OK';
  }
}

尝试一下应该可以。

关于azure - Nestjs - 实现 Azure-Ad Passport 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72801893/

相关文章:

visual-studio - VS2013 和 Azure - 部署模板验证失败,尽管我既没有编辑模板也没有编辑项目属性

spring - 从 AuthenticationSuccessHandler 访问 RequestContextHolder 和 HttpServletRequest.getUserPrincipal()

swift - typealias swift 的问题

unit-testing - 如何对 NestJS 的自定义 CacheInterceptor 进行单元测试?

powershell - 防止在停止 VM 之前需要使用 Login-AzureRmVm

java - azure服务总线方法中的Abandon()向死信队列发送特定消息

c# - 如何从 C# 代码生成 Azure 表服务 SAS token ?

java - 在 LDAP 身份验证中使用正确凭据的未分类异常

nestjs - 将信息从 NestJS 中间件传递到 Controller /服务

postgresql - TypeORM 找不到\"MyEntity\"的元数据