2015-12-18 13 views
15

Oturum açma mekanizmasına sahip olacak olan Angular2.beta.0'da bir demo uygulaması oluşturmaya çalışıyorum ve daha sonra tüm diğer API çağrıları üstbilgiler yoluyla gönderilen kazanılmış oturum simgesine sahip olacaktır.Interceptors

Köşeli 1x'de, belirteci ayrı bir kod içinde http başlığına ekleyebilecek bir Interceptor yazabilirim, angular2'nin böyle bir mekanizmaya veya bunu yapmak için önerilen başka bir yol olup olmadığını bilmek istiyorum.

+2

[JWT for Angular2] 'yi gördünüz mü (https://auth0.com/blog/2015/11/10/introducing-angular2-jwt-a-library-for-angular2-authentication/)? –

cevap

11

İsteklerin HTTP üst bilgisi olmalı mı? Elimizdeki HTTP belgelerine bakarak https://stormpath.com/blog/where-to-store-your-jwts-cookies-vs-html5-web-storage/

: Çerezler iyi bir seçim gibi görünüyor

get(url: string, options?: RequestOptionsArgs) : Observable<Response> 

Performs a request with get http method. 

Elimizdeki RequestOptionsArgs gitmek:
headers : Headers 

Not Yet Documented 

Nihayet Headers indikten.
import {Response} from "angular2/http"; 
import {RequestOptionsArgs} from "angular2/http"; 
import {Headers} from "angular2/http"; 

let token:string = 'my-secret'; 
this.http.get('your/url', <RequestOptionsArgs> { 
    headers: new Headers({ 
     'X-My-JWT-Header': 'sweet' 
    }) 
}) 

bu otomatik şekilde her istek için bu başlık takmak için bir yoldur BaseRequestOptions belgelerine baktığımızda:

import {Headers} from 'angular2/http'; 
var secondHeaders = new Headers({ 
    'X-My-Custom-Header': 'Angular' 
}); 

Yani şöyle bir şey olmalıdır. app.module.ts ilk

+0

BaseRequestOptions, aradığım şey şu an iş yapması gerekiyor. Teşekkürler! – codin

+0

BaseRequestOptions bağlantısı çalışmıyor. Burada en son çalışan bağlantıdır - https://angular.io/docs/ts/latest/api/http/index/BaseRequestOptions-class.html – saurabh

1

sağlayıcıları dizisinde çizgiyi takip ekleyin:

import {Http, RequestOptionsArgs, RequestOptions, Response, Request, ConnectionBackend} from "@angular/http"; 
    import {Observable} from "rxjs/Observable"; 
    import "rxjs/add/operator/catch"; 
    import "rxjs/add/observable/throw"; 
    import "rxjs/add/observable/empty"; 
    import {Router} from "@angular/router"; 

    export class HttpInterceptor extends Http { 
     constructor(backend: ConnectionBackend, defaultOptions: RequestOptions, private _router: Router) { 
      super(backend, defaultOptions); 
     } 

     request(url: string | Request, options?: RequestOptionsArgs):  Observable<Response> { 
      return this.intercept(super.request(url, options)); 
     } 

     get(url: string, options?: RequestOptionsArgs): Observable<Response> { 
      return this.intercept(super.get(url,options)); 
     } 

     post(url: string, body: string, options?: RequestOptionsArgs): Observable<Response> { 
      return this.intercept(super.post(url, body, options)); 
     } 

     put(url: string, body: string, options?: RequestOptionsArgs): Observable<Response> { 
      return this.intercept(super.put(url, body, options)); 
     } 

     delete(url: string, options?: RequestOptionsArgs): Observable<Response> { 
      return this.intercept(super.delete(url, options)); 
     } 

     intercept(observable: Observable<Response>): Observable<Response> { 
      return observable.catch((err, source) => { 
      }); 
     } 
    } 
+0

"catch" iki kez yürütülür, ancak sadece bir istek var. – novaline

0

ile

{provide : Http, useFactory: (xhrBackend: XHRBackend, requestOptions: RequestOptions) => new HttpInterceptor(xhrBackend, requestOptions),deps: [XHRBackend, RequestOptions]} 

oluştur HttpIntreceptor dosya doğrulanmış gelen 401 yanıtları müdahale için bunun nasıl bir örnek var API'ler burada. Yukarıdaki

http://www.annalytics.co.uk/angular2/javascript/typescript/ionic2/2017/02/26/Angular2-Http-Auth-Interceptor/

sonrası tüm giden HTTP isteklerine standart Authorzation üstbilgi ekleyin ve aynı zamanda otomatik belirteci gibi herhangi JWT'yi yenilemek için nasıl gösterir.

İlgili konular