阅读将在Nestjs的JWT护照中验证的访问令牌

发布于 2025-02-06 14:52:00 字数 803 浏览 3 评论 0原文

我正在构建Nestjs后端API服务器,并且使用JWT通过护照策略来验证和验证用户请求。 我的问题:我想阅读JWT策略的验证功能中的访问令牌。 下图:我的代码的示例 注意:我的目标是解码访问令牌,并获取我使用的用户代理来生成额外验证层的令牌(以确保请求来自同一设备)。如果您有更好的方法,我很乐意知道它:)

    import { ExtractJwt, Strategy } from 'passport-jwt';
import { PassportStrategy } from '@nestjs/passport';
import { Injectable } from '@nestjs/common';


@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
  constructor() {
    super({
      jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
      ignoreExpiration: false,
      secretOrKey: process.env.JWTSecret,
    });
  }

  async validate(payload: any) {
   
    //I want to read the access token that would be validated here 
    return { userId: payload.sub, username: payload.username , useragent: payload.useragent};
  }
}

I am building a nestjs back-end api server, and I'm using JWT to authenticate and validate user requests through passport strategies.
My question: I want to read the access token inside the validate function of JWT strategy.
Below: an example of my code
Note: my goal is to decode the access token and get the user agent I use to generate the token for an extra validation layer (to make sure the request is coming from the same device). If you have a better way, I would be happy to know it :)

    import { ExtractJwt, Strategy } from 'passport-jwt';
import { PassportStrategy } from '@nestjs/passport';
import { Injectable } from '@nestjs/common';


@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
  constructor() {
    super({
      jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
      ignoreExpiration: false,
      secretOrKey: process.env.JWTSecret,
    });
  }

  async validate(payload: any) {
   
    //I want to read the access token that would be validated here 
    return { userId: payload.sub, username: payload.username , useragent: payload.useragent};
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自此以后,行同陌路 2025-02-13 14:52:00

您可以将请求转发到validate方法。

@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
    constructor() {
        super({
            jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
            ignoreExpiration: false,
            secretOrKey: 'SECRET'
            passReqToCallback: true, // This is the key parameter
        })
    }

    // First parameter is the Request object
    async validate(req: Request, payload: DecryptedPayload) {
        const token = ExtractJwt.fromAuthHeaderAsBearerToken()(req)
        if (!token) throw new UnauthorizedException()

        // Do whatever you need to do with the encrypted access token

        return payload
    }
}

You can forward the request to the validate method.

@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
    constructor() {
        super({
            jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
            ignoreExpiration: false,
            secretOrKey: 'SECRET'
            passReqToCallback: true, // This is the key parameter
        })
    }

    // First parameter is the Request object
    async validate(req: Request, payload: DecryptedPayload) {
        const token = ExtractJwt.fromAuthHeaderAsBearerToken()(req)
        if (!token) throw new UnauthorizedException()

        // Do whatever you need to do with the encrypted access token

        return payload
    }
}
扶醉桌前 2025-02-13 14:52:00

事实证明,我没有在有效载荷中使用用户代理。
因此,要回答我自己的问题,就像@WS所说,您可以使用有效载荷查找所有有效载荷组件。

It turn out that I was not using the user agent in payload.
So to answer my own question and like @W.S said, you can use the payload to find all of the payload components.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文