Sự miêu tả
Giải pháp tốt nhất mà tôi đã tìm thấy là ghi đè XHRBackend
trạng thái phản hồi HTTP 401
và 403
dẫn đến một hành động cụ thể.
Nếu bạn xử lý xác thực của mình bên ngoài ứng dụng Angular, bạn có thể buộc làm mới trang hiện tại để cơ chế bên ngoài của bạn được kích hoạt. Tôi trình bày chi tiết giải pháp này trong phần triển khai bên dưới.
Bạn cũng có thể chuyển tiếp đến một thành phần bên trong ứng dụng của mình để ứng dụng Angular của bạn không bị tải lại.
Thực hiện
Góc> 2.3.0
Cảm ơn @mrgoos, đây là giải pháp đơn giản hóa cho góc 2.3.0+ do sửa lỗi trong góc 2.3.0 (xem sự cố https://github.com/angular/angular/issues/11606 ) mở rộng trực tiếp Http
mô-đun.
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);
});
}
}
Tệp mô-đun bây giờ chỉ chứa trình cung cấp sau.
providers: [
{ provide: Http, useClass: AuthenticatedHttpService }
]
Một giải pháp khác sử dụng Bộ định tuyến và dịch vụ xác thực bên ngoài được @mrgoos trình bày chi tiết trong ý kiến sau .
Angular trước 2.3.0
Việc triển khai sau đây hoạt động cho Angular 2.2.x FINAL
và RxJS 5.0.0-beta.12
.
Nó chuyển hướng đến trang hiện tại (cộng với một tham số để lấy URL duy nhất và tránh bộ nhớ đệm) nếu trả về mã HTTP 401 hoặc 403.
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;
}
}
với tệp mô-đun sau.
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 {
}