首页 > 解决方案 > Angular JWT 包未正确验证我的令牌

问题描述

所以我使用 angular 7 和 @auth0/angular-jwt 包。我在某些路线上有一个身份验证守卫,我检查 jwt 令牌是否已过期。不管我做什么,这个方法总是说令牌已过期。我知道令牌是有效的,因为它们是邮递员。有人对这个 isTokenExpired 方法有经验吗?

import { Injectable } from '@angular/core';
import { JwtHelperService } from '@auth0/angular-jwt';


@Injectable({ providedIn: 'root' })
export class AuthService {

private jwtHelper = new JwtHelperService()



// ...
public isAuthenticated(): boolean {
const token = localStorage.getItem('token');


// Check whether the token is expired and return
// true or false
return !this.jwtHelper.isTokenExpired(token);

  }
}

标签: angularjwtangular7

解决方案


你能在后端显示你设置为 exp 的内容吗?

如果您使用 'expiresIn',请记住它必须作为对象传递,如下所示:

{ expiresIn : '1h' }

推荐阅读