JHipster: getting 401 unauthorized access when calling the microservice API

I am currently using Jhipster to create the following components:

  • UAA - Auth Server
  • API Gateway
  • Microservice - Product1
  • Discovery Service - Consul

Other components:

  • Custom Frontend (Angular 4) - in a separate project

It is also important to note that the user interface uses Jhipster angular 4 code, which can be found in the vanilla Jipster Api Gateway. This includes the custom HTTPProvider.

The included classes can be seen in the image below: enter image description here

At the moment, I can successfully log in with this setting and call the API in UAA, however, when I try to call any of the APIS on the Product, I get 401 Unauthorized , for example Publish to Product1 / api / zcd .

The Consul has all visible and green services, and Gateway also has UAA and Product1 as registered and accessible routes.

enter image description here

So far, I have found that it does not appear that the AuthInterceptor is called when I make an api call for Product. I tried to manually add the jwt token to the methods, and this fixes the problem, but I cannot understand why customHttpProvider is not used to intercept the request and add the token.

My ProductService below works when I insert a token manually, as shown, but this is obviously not the most correct way to do this.

@Injectable() export class ProductService { private options = new Headers(); constructor(private http: Http) { this.options.append('Authorization', 'Bearer ' + 'token is inserted here'); } priceProduct(productPriceRequest: productPriceRequest): Observable<IdResponse> { return this.http.post('Product1/api/zcd', productPriceRequest, { headers: this.options }) .map(response => response.json()); } } 
+5
source share
1 answer

It is decided:

There were two things that were causing problems for me. It is important to note that they were not directly related to JHipster, but rather with issues related to Jhipster integrating with:

Problems:

  • Axon 3
  • Angular 4 user interface that has Lazy Loaded Modules.

Solutions:

    • I included axon 3 in the Microservice product and as part of the axon configuration, it initializes the token store (has nothing to do with security).
    • The tokenStore bean function in MicroserviceSecurityConfiguration, which must be of type JwtTokenStore, has been redefined as InMemoryTokenStore.
    • The solution was to rename the tokenStore bean in MicroserviceSecurityConfiguration to jwtTokenStore.
    • I had some lazy loadable modules. According to the documentation in this case, there is a SharedServiceModule that uses forRoot () and is imported into the AppModule.
    • However, when I had the .eg ProductService service that was imported into the ProductModule, it redefined the Http Factory, which was imported into the SharedServiceModule (the same behavior when importing the Http Factory into the AppModule).
    • The solution was to create an HttpProviderService, which is provided at the same level as the custom HttpProvider function (in the SharedServiceModule). He then administers Http for all other services at lower levels in the application. SharedServiceModule HttpProviderService
+2
source

Source: https://habr.com/ru/post/1271196/


All Articles