javascript - 拦截器Angular 6中的刷新令牌(JWT)
问题描述
最初,我有一个简单地检查令牌是否存在的函数,如果它不存在,则将用户发送到登录标头。现在我需要在刷新令牌的帮助下实现令牌过期时刷新的逻辑。但是我得到一个错误401。刷新功能没有时间工作,拦截器中的工作进一步导致错误。如何修复代码,以便等待刷新完成、获取新令牌而不重定向到登录页面?
令牌拦截器
import {HttpErrorResponse, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest} from "@angular/common/http";
import {Injectable, Injector} from "@angular/core";
import {AuthService} from "../services/auth.service";
import {Observable, throwError} from "rxjs";
import {catchError, tap} from "rxjs/operators";
import {Router} from "@angular/router";
import {JwtHelperService} from "@auth0/angular-jwt";
@Injectable({
providedIn: 'root'
})
export class TokenInterceptor implements HttpInterceptor{
private auth: AuthService;
constructor(private injector: Injector, private router: Router) {}
jwtHelper: JwtHelperService = new JwtHelperService();
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
this.auth = this.injector.get(AuthService);
const accToken = this.auth.getToken();
const refToken = this.auth.getRefreshToken();
if ( accToken && refToken ) {
if ( this.jwtHelper.isTokenExpired(accToken) ) {
this.auth.refreshTokens().pipe(
tap(
() => {
req = req.clone({
setHeaders: {
Authorization: `Bearer ${accToken}`
}
});
}
)
)
} else {
req = req.clone({
setHeaders: {
Authorization: `Bearer ${accToken}`
}
});
}
}
return next.handle(req).pipe(
catchError(
(error: HttpErrorResponse) => this.handleAuthError(error)
)
);
}
private handleAuthError(error: HttpErrorResponse): Observable<any>{
if (error.status === 401) {
this.router.navigate(['/login'], {
queryParams: {
sessionFailed: true
}
});
}
return throwError(error);
}
}
身份验证服务
import {Injectable} from "@angular/core";
import {HttpClient, HttpHeaders} from "@angular/common/http";
import {Observable, of} from "rxjs";
import {RefreshTokens, Tokens, User} from "../interfaces";
import {map, tap} from "rxjs/operators";
@Injectable({
providedIn: 'root'
})
export class AuthService{
private authToken = null;
private refreshToken = null;
constructor(private http: HttpClient) {}
setToken(authToken: string) {
this.authToken = authToken;
}
setRefreshToken(refreshToken: string) {
this.refreshToken = refreshToken;
}
getToken(): string {
this.authToken = localStorage.getItem('auth-token');
return this.authToken;
};
getRefreshToken(): string {
this.refreshToken = localStorage.getItem('refresh-token');
return this.refreshToken;
};
isAuthenticated(): boolean {
return !!this.authToken;
}
isRefreshToken(): boolean {
return !!this.refreshToken;
}
refreshTokens(): Observable<any> {
const httpOptions = {
headers: new HttpHeaders({
'Authorization': 'Bearer ' + this.getRefreshToken()
})
};
return this.http.post<RefreshTokens>('/api2/auth/refresh', {}, httpOptions)
.pipe(
tap((tokens: RefreshTokens) => {
localStorage.setItem('auth-token', tokens.access_token);
localStorage.setItem('refresh-token', tokens.refresh_token);
this.setToken(tokens.access_token);
this.setRefreshToken(tokens.refresh_token);
console.log('Refresh token ok');
})
);
}
}
解决方案
req = this.auth.refreshTokens().pipe(
switchMap(() => req.clone({
setHeaders: {
Authorization: `Bearer ${this.auth.getToken()}`
}
}))
)
这将首先调用refreshToken
并在tap
那里运行,然后使用 new 发出请求this.auth.getToken()
,请注意accToken
仍然具有旧值,因为代码不会重新运行。
推荐阅读
- c# - Microsoft.CognitiveServices.Speech.SpeechRecognizer - 获取文件中结果的时间偏移,持续识别
- mysql - 插入两个表不起作用。有正确的方法吗?
- python - 计算行数并以更少的数量打印
- vim - 在vim中替换匹配分隔符内的单个字符
- java - 在 ListView 中从 XML 资源文件添加项目
- javascript - 如何在 element-ui vuejs 中使用 :error 获得成功 validateState
- amazon-web-services - AWS Cognito 用户注册前验证。检查用户是否存在于其他数据库中
- php - Paypal Payflow 透明重定向错误代码:PL001
- mongodb - 如何在开玩笑测试中正确关闭猫鼬连接
- testing - 集成测试,通配符忽略标签