Error 403 in Angular 4.3 Interceptor calls twice

0

I am consuming a service made with Java, which needs to be sent a jwt, previously generated by a login.

For this I created an interceptor in Angular 4.3

vt-auth.interceptor.ts

    import { Injectable } from '@angular/core';
import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest } from '@angular/common/http';
import { Observable } from 'rxjs/observable';
@Injectable()
export class VtAuthInterceptor implements HttpInterceptor {
  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    const authHeader = 'bearer ' + localStorage.getItem('JWT');
    const authReq = req.clone({headers: req.headers.set('Authorization', authHeader)});
    return next.handle(authReq);
  }  
}

The problem comes when I consume an external service, such as link which, does not require jwt, according to what I see in the console of chrome, is that first launches the request without the header 'Authorization', and then repeats the request, with the header inserted.

When I make the call to the backend service in Java, on the first call it rejects it, giving an error 403 and therefore not allowing the operation to continue. For that reason, I do not know where the error is, if what I am programming or in the service, that does not accept this type of requests.

    
asked by carscx 28.09.2017 в 20:08
source

0 answers