Access spring security principal in microservies ,when actual authentication has done in ZUUL gateway

喜你入骨 提交于 2020-01-15 10:18:08

问题


In our project JWT token validation and other authorization related logics are done in ZUUL gateway. If zuul succesfully validated the token it will proceed the request to the corresponding microservice

In that case, How can i send Principal to other microservices, when JWT authorization has been done from ZUUl gateway .

I can of course fetch the token using @RequestHeader(value="Authorization")in controllers of other services.

But in order to use @Preauthorize(id,principal) kind offunctionality, i need principal in other microservices where the actual authentication has not been taken place.

is it possible to do this?


回答1:


Yeah it's possible.

Whenever creating or generating new Jwt token add your ( user details or any useful information that need to be consumed in downstream microservices ) in the Jwt token claims.

Whenever routing happens in Zuul API gateway it will pass the current request to your downstream microservices. In that incoming request you can get your Jwt token from the request header.

Steps to access the User details or any information using Jwt token from any microservice :

  1. Get the token from the request header.
  2. Parse the token with correct signing key and get the user details from the token claims.

For your reference : https://medium.com/@Baimurzin/how-to-get-the-current-user-in-spring-cloud-microservices-c876e1c6fc65



来源:https://stackoverflow.com/questions/57867056/access-spring-security-principal-in-microservies-when-actual-authentication-has

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!