我需要使用https://github.com/auth0/angular2-jwt/tree/v1.0
JWT 拦截器获得基于角色的身份验证的建议。如何使用 Angular 5 执行“管理员”角色身份验证?
现在我有:在登录服务器发送回带有有效负载中用户 ID 的 jwt 令牌并使用 canActivate 后,我的应用程序检查令牌是否存在,然后允许进入安全站点。
@Injectable()
export class EnsureAuthenticated implements CanActivate {
constructor(private auth: AuthService, private router: Router) {}
canActivate(): boolean {
if (localStorage.getItem('token')) {
return true;
} else {
this.router.navigateByUrl('/login');
return false;
}
}
}
和我的安全死记硬背:
export const SECURE_ROUTES: Routes = [
{ path: 'home', component: HomeComponent, canActivate: [EnsureAuthenticated] },
{ path: 'homeadmin', component: HomeadminComponent, canActivate: [AuthenticatedAdmin] },
];
之后我想创建类似的东西:
@Injectable()
export class AuthenticatedAdmin implements CanActivate {
constructor(private auth: AuthService, private router: Router) {}
canActivate(): boolean {
if ("in token is admin") {
return true;
} else {
this.router.navigateByUrl('/login');
return false;
}
}
}
https://github.com/auth0/jwt-decode
在这种方法中,我需要用你认为这是正确的方法来解码令牌吗?如果您对此问题有更好的解决方案,请告诉我。