Nestjs - 实施 Azure-Ad 护照身份验证

Yah*_*tzi 0 authentication azure passport.js passport-azure-ad nestjs

如何实施 Azure-Ad Passport 身份验证?找不到任何相关文档,并在网上阅读发现存在问题。

Max*_*kov 6

将 MSAL 用于前端。对于后端,请使用 Passport 和Passport-azure-ad npm。 在此输入图像描述

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

// 我的 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';
  }
}
Run Code Online (Sandbox Code Playgroud)

尝试一下应该可以。