2017-05-31 24 views
0

我在使用AuthHttp時遇到錯誤,但authenticate返回一個好的標記。我在https://jwt.io/上驗證它,這很好。Angular angular2-jwt:使用AuthHttp獲取錯誤:JWT必須有3個部分

我採用了棱角分明4. 我的代碼如下:

登入碼

signIn(login: string, password: string) { 
    this.UserLogin.name = login; 
    this.UserLogin.password = password; 

    this.http.post(this.baselink + '.json', this.UserLogin, { headers: contentHeaders }) 
     .subscribe(
     response => { 
     localStorage.setItem('id_toke', response.json().token); 
     this.jwt = localStorage.getItem('id_toke'); 
     this.decodedJwt = this.jwtHelper.decodeToken(this.jwt); 
     this.userRole = this.decodedJwt.roles; 
     console.log(this.userRole); 
     this.useJwtHelper(); 
     this.router.navigate(['/']); 
     }, 
     error => { 
     console.log('Http Error ' + error.text()); 
     this.router.navigate(['http-error']); 
     } 
    ); 
    } 

token ![enter image description here]1:"eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJhZG1pbiIsInJvbGVzIjpbInJlYWQtb25seSIsInJlYWQtd3JpdGUiXSwiaWF0IjoxNDk2MTkxNDg4LCJleHAiOjE0OTYyMDAwODh9.qPCQY3yHujuCLb9g-F5b0MUQO3tyTr_Y5YRoMOQ0DBA"

enter image description here

沒有從中得到的數據是拋出錯誤

getPayments() { 
     return this.authHttp.get(this.baselink + '/getPayments', { headers: contentHeaders }) 
      .map(
      (response: Response) => { 
       const data = response.json(); 
       return data; 
      } 
      ) 
      .catch(
      (error: Response) => { 
       console.log('Error getPayments: ' + error); 
       return Observable.throw('Something went wrong: ' + error.text); 
      } 
      ); 
    } 

並獲得

Error getPayments: Error: JWT must have 3 parts

話說回來,如果我用return this.http.get(this.baselink + '/getPayments'),而不是它的工作原理。 AuthHttp的連接沒有到達服務器。這種特定的控制器並不侷限於作爲服務器的時間,但其目的是保護它 可能令牌不AuthHttp頭包括

@NgModule({ 
    providers: [ 
     { 
      provide: AuthHttp, 
      useFactory: authHttpServiceFactory, 
      deps: [Http, RequestOptions] 
     } 
    ] 
}) 
export class AuthorizationModule { } 


export function authHttpServiceFactory(http: Http, options: RequestOptions) { 
    return new AuthHttp(new AuthConfig({ 
     headerName: 'Authorization', 
     headerPrefix: 'bearer', 
     tokenName: 'token', 
     tokenGetter: (() => localStorage.getItem('id_token')), 
     globalHeaders: [{ 'Content-Type': 'application/json' }], 
     noJwtError: true 
    }), http, options); 
} 

和全局頭部

export const contentHeaders = new Headers(); 
contentHeaders.append('Accept', 'application/json'); 
contentHeaders.append('Content-Type', 'application/json'); 
+0

**更新** 這是一個服務器端交叉問題。顯然,瀏覽器會發送2個呼叫,要求服務器允許該標頭和服務器批准它。 在我的情況下,在服務器端,我需要將我的url添加到FilterRegistraionBean。我添加了它,但不正確 – Rami

回答

0

我改變了我功能下面

getPayments(): Observable<Payment[]> { 
    // add authorization header with jwt token - mine is saved in authService service in jwt variable 
    let headers = new Headers({ 'Authorization': 'Bearer ' + this.authService.jwt }); 
    let options = new RequestOptions({ headers: headers }); 


    return this.http.get(this.baselink + '/getPayments', options) 
     .map((response: Response) => response.json()) 
     .catch(
     (error: Response) => { 
      console.log(error); 
      return Observable.throw('Something went wrong: ' + error.text); 
     }); 
} 

現在收到此錯誤

function (encodingHint) { 
     if (encodingHint === void 0) { encodingHint = 'legacy'; } 
     if (this._body instanceof URLSearchParams) { 
      return this._body.toString(); 
     } 
     if (this._body instanceof ArrayBuffer) { 
      switch (encodingHint) { 
       case 'legacy': 
        return String.fromCharCode.apply(null, new Uint16Array(/** @type {?} */ (this._body))); 
       case 'iso-8859': 
        return String.fromCharCode.apply(null, new Uint8Array(/** @type {?} */ (this._body))); 
       default: 
        throw new Error("Invalid value for encodingHint: " + encodingHint); 
      } 
     } 
     if (this._body == null) { 
      return ''; 
     } 
     if (typeof this._body === 'object') { 
      return JSON.stringify(this._body, null, 2); 
     } 
     return this._body.toString(); 
    } 

檢查服務器端,授權是否丟失!

更新 這是一個服務器端交叉問題。顯然,瀏覽器會發送2個呼叫,要求服務器允許該標頭和服務器批准它。 在我的情況下,在服務器端,我需要將我的url添加到FilterRegistraionBean。我有它添加但不正確

相關問題