Add token to web service header rest angular6

0

Good morning,

I have a rest webservice made with spring boot that works correctly when the token was sent by postman but when sending it from my front it does not work correctly, it showed that the token sent from another application travels this way

  

GET / xxxxx / yyy / v0 / commercialmanagement / opportunities / 10010999   HTTP / 1.1   Host: conecta.medplus.net.co:7443   Connection: keep-alive   Accept: /    Authorization : Bearer eyJhbGciOiJIUzI1NiJ9.eyJpZExvZ2luIjoyNDQ1NywiY2xhdmVWZW50YUFzZXNvciI6IjUzMDQiLCJ1c2VyTmFtZSI6IkNvbmVjdGFNZWRwbHVzIiwiY29kU3VjdXJzYWwiOjEsImNvZERlcGFydGFtZW50byI6MTEwMDEsInN1YiI6IlZJQ1RPUiBXSUxMSUFNIExFT04gUklOQ09OIiwiYXVkIjoiWzE1MF0iLCJleHAiOjE1NDUzMzIwNDMsImlhdCI6MTU0NTMxNzY0M30.YrBba-B3byCVbTrnDC9e-kc5bwCNB6tOBw3XV-URzCE   User-Agent: Mozilla / 5.0 (Windows NT 10.0; Win64; x64) AppleWebKit / 537.36 (KHTML, like Gecko) Chrome / 71.0.3578.98 Safari / 537.36   Referer: link   Accept-Encoding: gzip, deflate, br   Accept-Language: en-419, es; q = 0.9, en; q = 0.8

But when sending it from my front it does not leave the authorization field appears in the field of access-control-request-headers

  

OPTIONS / v1 / inputParameter / country   HTTP / 1.1   Host: localhost: 9002   Connection: keep-alive   Access-Control-Request-Method: GET   Origin: link   User-Agent: Mozilla / 5.0 (Windows NT 10.0; Win64; x64) AppleWebKit / 537.36 (KHTML, like Gecko) Chrome / 71.0.3578.98 Safari / 537.36   Access-Control-Request-Headers: authorization, content-type   Accept: /   Referer: link   Accept-Encoding: gzip, deflate, br   Accept-Language: en-419, es; q = 0.9, en; q = 0.8

My angular code where I assign the token is the following

import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from 
 '@angular/common/http';
import { Injectable } from '@angular/core';

import { Observable } from 'rxjs/Observable';

import { System } from 'src/System';

@Injectable()
export class JwtInterceptor implements HttpInterceptor {

    system : System;


    intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {

        this.system = new System();

        req = req.clone({
          setHeaders: {
            'Content-Type' : 'application/json; charset=utf-8',
            'Accept'       : 'application/json',
            'Authorization': 'Bearer '+this.system.getToken(),
          },
        });

        return next.handle(req);
      }
    }
    
asked by Jorge Reinaldo Linares Pineda 20.12.2018 в 15:58
source

1 answer

0

The problem is that the headers of request do not match those of response . You must do the following:

  • You must add the following headers to your response, that is, in your API
  • Only in OPTIONS (as you will notice, here we say that you must allow the "Authorization" header):

    Access-Control-Allow-Headers: Origin, Content-Type, Accept, Authorization
    

    Only in POST:

    Access-Control-Allow-Credentials: true
    

    In both:

    Access-Control-Allow-Origin: http://domain.com:3000

    or to authorize queries from any source:

    Access-Control-Allow-Origin: *       
    

    With this I should already allow you to make inquiries. Greetings!

        
    answered by 20.12.2018 в 17:11