I have a problem when consuming services with a token using JWT. When I consume the service to obtain the token, it works perfectly and I return the token to consume the services, but when sending the headers it does not work correctly and it returns error 401. I have looked many times at the source code, and I do not see any error about it .
import { Injectable } from '@angular/core';
import {HttpClient,HttpHeaders} from '@angular/common/http';
import 'rxjs/add/operator/toPromise';
@Injectable({
providedIn: 'root'
})
export class CoordinadoresService {
private static BASEURL:string = "http://localhost:8080/coordinador/";
private static BASEURLTOKEN:string = "http://localhost:8080/auth";
token:string = "";
constructor(private http:HttpClient) { }
headers(){
this.token = sessionStorage.getItem("Token");
let headers = new HttpHeaders({
'Content-Type': 'application/json',
'Authorization': this.token });
return headers;
}
getServices(url:string): Promise<any> {
this.token = sessionStorage.getItem("Token");
let header = new HttpHeaders({
'Content-Type': 'application/json',
'Authorization': this.token });
return this.http.get(CoordinadoresService.BASEURL + url,{headers:header})
.toPromise()
.then(response => {
return response;
})
.catch(err => err);
}
postServicesToken(data:any): Promise<any> {
let header = new HttpHeaders({
'Content-Type': 'application/json'});
return this.http.post(CoordinadoresService.BASEURLTOKEN,data,{headers:header})
.toPromise()
.then(response => {
return response;
})
.catch(err => err);
}
}
In spring boot in console it appears that a null user arrives:
in postman the services work without any problem, sending the token in the header. I do not know what I'm doing wrong.
thanks for your help