jhipster / jhipster-dotnetcore

JHipster.NET blueprint
Apache License 2.0
310 stars 92 forks source link

`Authorization failed` error in microservice when using gateway and microfrontends #1452

Open egvimo opened 7 months ago

egvimo commented 7 months ago
Overview of the issue

After generating a gateway and a microservice (with microfrontend) the authentication via Keycloak works in the gateway. When calling a microservice API following error is thrown (calls to the microfrontend parts are working):

Log ```log 2024-01-24 13:16:09.410 DBUG 24840 --- [ 7] M.A.Authorization.AuthorizationMiddleware : Policy authentication schemes did not succeed info: Microsoft.AspNetCore.Authorization.DefaultAuthorizationService[2] Authorization failed. These requirements were not met: DenyAnonymousAuthorizationRequirement: Requires an authenticated user. 2024-01-24 13:16:09.415 INFO 24840 --- [ 7] M.A.A.DefaultAuthorizationService : Authorization failed. These requirements were not met: DenyAnonymousAuthorizationRequirement: Requires an authenticated user. fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1] An unhandled exception has occurred while executing the request. System.InvalidOperationException: No authenticationScheme was specified, and there was no DefaultChallengeScheme found. The default schemes can be set using either AddAuthentication(string defaultScheme) or AddAuthentication(Action configureOptions). at Microsoft.AspNetCore.Authentication.AuthenticationService.ChallengeAsync(HttpContext context, String scheme, AuthenticationProperties properties) at Microsoft.AspNetCore.Authorization.Policy.AuthorizationMiddlewareResultHandler.<>c__DisplayClass0_0.<g__Handle|0>d.MoveNext() --- End of stack trace from previous location --- at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context) at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext) at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider) at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context) 2024-01-24 13:16:09.439 EROR 24840 --- [ 7] M.A.D.DeveloperExceptionPageMiddleware : An unhandled exception has occurred while executing the request. System.InvalidOperationException: No authenticationScheme was specified, and there was no DefaultChallengeScheme found. The default schemes can be set using either AddAuthentication(string defaultScheme) or AddAuthentication(Action configureOptions). at Microsoft.AspNetCore.Authentication.AuthenticationService.ChallengeAsync(HttpContext context, String scheme, AuthenticationProperties properties) at Microsoft.AspNetCore.Authorization.Policy.AuthorizationMiddlewareResultHandler.<>c__DisplayClass0_0.<g__Handle|0>d.MoveNext() --- End of stack trace from previous location --- at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context) at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext) at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider) at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context) ```
Motivation for or Use Case

With Java backend this error does not happen and the token is forwarded to the service

Reproduce the error

I did not manage to get it work with Consul (application did not start), so I created a static route to the microservice. Maybe this has something to do with the issue.

JHipster Version(s)

4.1.0 + 8.1.0

JHipster configuration
JDL definitions ```jdl application { config { baseName gateway packageName gateway applicationType gateway authenticationType oauth2 clientFramework react databaseType sql devDatabaseType h2Memory prodDatabaseType h2Memory serviceDiscoveryType consul testFrameworks [cypress] blueprints [generator-jhipster-dotnetcore] microfrontends [myservice] } } application { config { baseName myservice packageName myservice applicationType microservice authenticationType oauth2 buildTool gradle clientFramework react databaseType sql devDatabaseType oracle prodDatabaseType oracle enableHibernateCache false serverPort 8081 serviceDiscoveryType consul testFrameworks [cypress] blueprints [generator-jhipster-dotnetcore] } entities MyEntity } @paginate(pagination) @filter entity MyEntity { number String required } ```