【问题标题】:NestJS Fastify AuthenticationNestJS Fastify 身份验证
【发布时间】:2021-02-12 01:01:16
【问题描述】:

所以我用 Fastify 替换了 ExpressJS,但我的问题是 Nest-Passport 不支持 fastify,我们有 Nest-Passport 的替代品吗?或有关如何使用令牌在nestJS 中保护RestAPI 的任何解决方案?

【问题讨论】:

    标签: passport.js nestjs fastify nestjs-fastify


    【解决方案1】:

    据我所知,没有即时的 Fastify NestJJS 身份验证包(我确信那里有一些东西),但 I do have a sample of JWT authentication with Fastify and NestJS without Passport。这个想法是使用 Nest 的 @nestjs/jwt 包或直接使用 jsonwebtoken,并使用它创建身份验证令牌,而不是委托给 Passport。这实际上是我更喜欢的一种方法,因为我发现 Passport 有时有点太神秘了。

    【讨论】:

      【解决方案2】:

      我不知道这是否是正确的方式。但是如果我更改默认的 jwt 提取器

      ExtractJwt.fromAuthHeaderAsBearerToken

      (described within the doc) 由一个自定义的它起作用。

      const fromFastifyAuthHeaderAsBearerToken = (request: FastifyRequest): string => {
      const auth = request.headers['authorization'];
      const token = auth?.split(' ')[1];
      return token;
      }
      

      【讨论】:

      • 这将是正确的链接github.com/mikenicholson/…
      • 你也可以像在JWT策略jwtFromRequest: (request: fastify.FastifyRequest) => { console.log('As it is if not signed', request?.cookies?.Authentication); console.log('After unsigning the cookie', request?.unsignCookie(request.cookies?.Authentication)); }987654325@这样的代码中使用它
      猜你喜欢
      • 2021-01-13
      • 2018-11-02
      • 2019-03-28
      • 2019-09-05
      • 1970-01-01
      • 2019-04-14
      • 2019-09-13
      • 2021-05-31
      • 2021-03-02
      相关资源
      最近更新 更多