首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >即使在设置expiresIn值之后,JWT在nestjs应用程序中也不会过期。

即使在设置expiresIn值之后,JWT在nestjs应用程序中也不会过期。
EN

Stack Overflow用户
提问于 2022-05-20 06:07:33
回答 2查看 1.3K关注 0票数 0

这就是我的auth.module.ts的样子:

代码语言:javascript
运行
复制
import { Module } from "@nestjs/common";
import { ConfigModule, ConfigService } from "@nestjs/config";
import { JwtModule } from "@nestjs/jwt";
import { PassportModule } from "@nestjs/passport";
import { TypeOrmModule } from "@nestjs/typeorm";
import appConfig from "src/config/app.config";
import devConfig from "src/config/dev.config";
import stagConfig from "src/config/stag.config";
import { User } from "src/entities/entity/user.entity";
import { AuthService } from "./auth.service";
import { JwtStrategy } from "./passport-strategies/jwt-strategy";
import { LocalStrategy } from "./passport-strategies/local-strategy";



@Module({
  imports: [
    PassportModule,
    ConfigModule.forRoot({
      load: [appConfig, devConfig, stagConfig],
      ignoreEnvFile: true,
      isGlobal: true,
    }),
    TypeOrmModule.forFeature([
       User
    ]),
    JwtModule.registerAsync({
        imports: [ConfigModule],
        useFactory: async (configService: ConfigService) => ({
          // secret: configService.get<string>('jwt.secret'),
          secret: process.env.TOKEN_KEY,
          signOptions: { expiresIn: 30 }
        }),
        inject: [ConfigService]
      }),
  ],
  providers: [
      AuthService, 
      LocalStrategy, 
      JwtStrategy
  ],
  exports: [AuthService],
})
export class AuthModule {}

如您所见,我已经设置了signOptions: { expiresIn: 30 },但是当我分析令牌时,它没有过期参数,并且不会过期。

我使用https://jwt.io/#encoded-jwt来分析令牌:

EN

回答 2

Stack Overflow用户

发布于 2022-05-20 06:48:29

您必须将expiresIn值作为字符串传递,并在几秒钟内提到s,如下所示

代码语言:javascript
运行
复制
 JwtModule.register({
      secret: jwtConstants.secret,
      signOptions: { expiresIn: '60s' },
    }),

请让我知道这是否有效!

票数 1
EN

Stack Overflow用户

发布于 2022-11-12 15:19:06

同样的问题。搜索ignoreExpiration标志。我的问题是:

代码语言:javascript
运行
复制
@Injectable()
export class JWTStrategy extends PassportStrategy(Strategy) {
  constructor(
    readonly configService: ConfigService,
    private userService: UserService,
  ) {
    super({
      jwtFromRequest: ExtractJwt.fromHeader(
        configService.get('API_ACCESS_TOKEN_HEADER'),
      ),
      ignoreExpiration: true, // *** FIXME: here
      secretOrKey: configService.get('API_ACCESS_TOKEN_SECRET'),
    });
  }
...

以下是类型:

代码语言:javascript
运行
复制
export interface StrategyOptions {
    secretOrKey?: string | Buffer | undefined;
    secretOrKeyProvider?: SecretOrKeyProvider | undefined;
    jwtFromRequest: JwtFromRequestFunction;
    issuer?: string | undefined;
    audience?: string | undefined;
    algorithms?: string[] | undefined;
    ignoreExpiration?: boolean | undefined;
    passReqToCallback?: boolean | undefined;
    jsonWebTokenOptions?: VerifyOptions | undefined;
}

使用的ignoreExpirationhttps://github.com/auth0/node-jsonwebtoken/blob/master/verify.js#L147

希望它能帮到你

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72314255

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档