When I tried to visit a protected URL of microservice through the gateway eg: HTTP://localhost:8080/services/microservicename/api/** I am getting the following error
401 Unauthorized, full authentication requested .
I am using JWT authentication for microservices and I do have a separate registry service. What might be causing this issue?
The first step in debugging should be to see if you are getting authenticated with the gateway.
To get authenticated with the gateway pass in request body raw json data
{"username":"admin","password":"admin","rememberMe":"true"}to the gateway authentication URL
http://localhost:8080/api/authenticateand you will get anid_tokenin response. Use this id_token data asAuthorization Bearertoken in every request you would like to process which is protected eg:-http://localhost:8080/services/microservicename/pathUpon gateway authentication confirmation the second step to debug would be to see SecurityConfiguration.java in the config folder of a particular microservice you are trying to access. In the
SecurityFilterChainfunction you would see something like.antMatchers("/api/**").authenticated(). The end of the antMatcher would be eitherpermitAll()authenticated()orhasAuthority(AuthoritiesConstants.ADMIN).If it is
permitAll()all requests would reach microservice and get processed upon gateway authentication.if it is
hasAuthority(AuthoritiesConstants.Admin)implies you would have to log in as that particular user. By default there are two users in Jhipster 'admin' {username:admin, password:admin} or user {username:user, password:user} and only then microservice controller will process your request.if it
authenticated()then any user type login would process the request.If you are still facing the
401 Unauthorized, full authentication requestedit's probably the JWT secret that is causing the issue. When I was debugging the problem, I initially missed the details on the original documentation page. https://www.jhipster.tech/security/Make sure you have the same jwt secret in
application-dev.ymlandapplication-prod.ymlas the Gateway's application-dev.yml and application-prod.yml files.