我使用keycloak-jsVersion8.0.1,我有一个函数getToken,它测试令牌在刷新令牌时过期,或者令牌未过期,因此返回令牌。问题是,当令牌过期时,它会显示警告消息,但不会更改它
TokenService
@Injectable()
export class TokenService {
static auth: any = {};
constructor() { }
static init(): Promise<any> {
const keycloakAuth: Keycloak.KeycloakInstance = Keycloak({
url: config.keycloak.url,
realm: config.keycloak.realm,
clientId: config.keycloak.clientId
});
return new Promise((resolve, reject) => {
keycloakAuth.init({ onLoad: 'check-sso', flow: 'implicit', checkLoginIframe: false }).success(() => {
TokenService.auth.authz = keycloakAuth;
resolve();
}).error(() => {
reject();
});
});
}
getToken(): Promise<string> {
return new Promise<string>((resolve, reject) => {
if (TokenService.auth.authz.isTokenExpired()) {
console.warn("Token expired !");
TokenService.auth.authz.updateToken(1800).success(() => {
console.log("Token updated");
resolve(<string>TokenService.auth.authz.token);
}).error(() => {
reject('Failed to refresh token');
});
} else {
resolve(<string>TokenService.auth.authz.token);
}
});
}
}发布于 2020-01-31 09:39:43
我认为问题可能是函数TokenService.auth.authz.updateToken(1800),这意味着(如果我正确理解的话),如果令牌将在1800秒内过期,那么执行刷新令牌。
在您的示例中,令牌已经过期,因此函数不会执行任何操作。我建议不要检查isExpired(),而是直接这样做
getToken(): Promise<string> {
return new Promise<string>((resolve, reject) => {
TokenService.auth.authz.updateToken(100).success(() => {
console.log("Token updated");
resolve(<string>TokenService.auth.authz.token);
}).error(() => {
reject('Failed to refresh token');
});
resolve(<string>TokenService.auth.authz.token);
});
}https://stackoverflow.com/questions/59990658
复制相似问题