0

我有一个nestjs应用程序(我是nestjs的新手),我正在尝试使用护照(passport-azure-ad)来保护使用Azure AD的api:

import * as passport from 'passport';
async function bootstrap() {
...
const optionsBearer: IBearerStrategyOptionWithRequest = {
    identityMetadata:
      'https://login.microsoftonline.com/${tenantId}/v2.0/.well-known/openid-configuration',
    clientID: '${clientId}',
    validateIssuer: false,
    loggingLevel: 'info',
    loggingNoPII: false,
    passReqToCallback: true,
    scopes: ['access_as_user'],
  };
  
  const bearerStrategy = new BearerStrategy(optionsBearer,
    function (req, token, done) {
      return done(null, { name: 'user name' }, token);
    });

  app.use(passport.initialize());
  passport.use(bearerStrategy);

  await app.listen(3000);

}

这是我使用 Guards 的控制器:

import { AuthGuard } from '@nestjs/passport';
...
@Get()
@UseGuards(AuthGuard('oauth-bearer'))
getHello() {
  return "hello world!";
}

我在 Azure AD 租户中注册了应用程序和所有内容。我使用范围公开了一个 api。然后我创建了另一个 Azure AD 应用程序(作为 api 的使用者)并授予对该 api 范围的访问权限。我使用邮递员来调用我的api。我设法使用 oAuth2.0 从邮递员进行身份验证并获取令牌,但是当我尝试使用该令牌调用我的 api 时,我会收到此错误:

身份验证失败,原因是:jwt 受众无效

我在这里做错了什么?(当我使用 .net 应用程序测试相同的 azure 广告配置时,它可以工作,所以 azure 广告应用程序注册似乎很好,而且我没有正确地在嵌套中进行身份验证)

4

0 回答 0