I have an angular 9 application + spring service which are authenticated against keycloak. The problem is that i want to open the spring swagger link in a new tab from the angular ui. Currently I am trying to add the bearer token to the authorization header when calling the url, but without much luck.
This is a snippet of what i am trying
var currentUserObject = JSON.parse(sessionStorage.getItem('currentUser'));
this.http.get(this.swaggerUrl, {
headers: new HttpHeaders({
'Authorization': 'Bearer ' + currentUserObject.access_token
})
}).subscribe(() => window.location.href=this.swaggerUrl)
}
I use this lib in my Angular apps to handle user authentication and Bearer tokens header (access-token refreshing, automaticaly add
Authorizationheader to configured routes, etc.)Also, this is not directly related but if your API is documented with Swagger, have you considered using OpenAPI generator to generate an Angular client lib? It exposes very convenient services to query your API.