描述
我发现最好的解决方案是重写XHRBackend
HTTP响应状态,401
并403
导致特定操作。
如果您在Angular应用程序之外处理身份验证,则可以强制刷新当前页面,从而触发您的外部机制。我在下面的实现中详细介绍了该解决方案。
您还可以转发到应用程序内部的组件,这样就不会重新加载Angular应用程序。
实作
角度> 2.3.0
感谢@mrgoos,这是对angular 2.3.0+的简化解决方案,这是由于直接扩展了模块的angular 2.3.0中的错误修复(请参见问题https://github.com/angular/angular/issues/11606)Http
。
import { Injectable } from '@angular/core';
import { Request, XHRBackend, RequestOptions, Response, Http, RequestOptionsArgs, Headers } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';
@Injectable()
export class AuthenticatedHttpService extends Http {
constructor(backend: XHRBackend, defaultOptions: RequestOptions) {
super(backend, defaultOptions);
}
request(url: string | Request, options?: RequestOptionsArgs): Observable<Response> {
return super.request(url, options).catch((error: Response) => {
if ((error.status === 401 || error.status === 403) && (window.location.href.match(/\?/g) || []).length < 2) {
console.log('The authentication session expires or the user is not authorised. Force refresh of the current page.');
window.location.href = window.location.href + '?' + new Date().getMilliseconds();
}
return Observable.throw(error);
});
}
}
现在,模块文件仅包含以下提供程序。
providers: [
{ provide: Http, useClass: AuthenticatedHttpService }
]
@mrgoos 在以下要点中详细介绍了使用路由器和外部身份验证服务的另一种解决方案。
Angular 2.3.0版
以下实现适用于Angular 2.2.x FINAL
和RxJS 5.0.0-beta.12
。
如果返回HTTP代码401或403,它将重定向到当前页面(加上用于获取唯一URL并避免缓存的参数)。
import { Request, XHRBackend, BrowserXhr, ResponseOptions, XSRFStrategy, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';
export class AuthenticationConnectionBackend extends XHRBackend {
constructor(_browserXhr: BrowserXhr, _baseResponseOptions: ResponseOptions, _xsrfStrategy: XSRFStrategy) {
super(_browserXhr, _baseResponseOptions, _xsrfStrategy);
}
createConnection(request: Request) {
let xhrConnection = super.createConnection(request);
xhrConnection.response = xhrConnection.response.catch((error: Response) => {
if ((error.status === 401 || error.status === 403) && (window.location.href.match(/\?/g) || []).length < 2) {
console.log('The authentication session expires or the user is not authorised. Force refresh of the current page.');
window.location.href = window.location.href + '?' + new Date().getMilliseconds();
}
return Observable.throw(error);
});
return xhrConnection;
}
}
与以下模块文件。
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { HttpModule, XHRBackend } from '@angular/http';
import { AppComponent } from './app.component';
import { AuthenticationConnectionBackend } from './authenticated-connection.backend';
@NgModule({
bootstrap: [AppComponent],
declarations: [
AppComponent,
],
entryComponents: [AppComponent],
imports: [
BrowserModule,
CommonModule,
HttpModule,
],
providers: [
{ provide: XHRBackend, useClass: AuthenticationConnectionBackend },
],
})
export class AppModule {
}