简体   繁体   English

如何通过 django rest 框架中的其他微服务对属于另一个数据库的用户进行身份验证?

[英]How can I authenticate a user who belongs to another database through my other microservice in django rest framework?

I'm new to django and I am required to create two microservices with separate databases;我是 django 的新手,我需要创建两个具有不同数据库的微服务;

One to hold user information and the other to hold todo/tasks information.一个保存用户信息,另一个保存待办事项/任务信息。 So far, I have created two separate projects with two separate databases,到目前为止,我已经用两个独立的数据库创建了两个独立的项目,

  1. To authenticate the user using simplejwt authentication.使用 simplejwt 身份验证对用户进行身份验证。 (todo_auth project with todo_auth database) (带有 todo_auth 数据库的 todo_auth 项目)
  2. To show the todo/task information specific to that user.显示特定于该用户的待办事项/任务信息。 (todo project with todo database) (带有 todo 数据库的 todo 项目)

I need the todo project to verify the token by routing it back to the todo_auth project, and then I need the todo_auth project to send a response to the todo project.我需要 todo 项目通过将令牌路由回 todo_auth 项目来验证令牌,然后我需要 todo_auth 项目向 todo 项目发送响应。 (By specifying the port) (通过指定端口)

How can I achieve this?我怎样才能做到这一点? Many thanks.非常感谢。

PS: I'm running the two django projects on the same server with different port numbers. PS:我在具有不同端口号的同一台服务器上运行两个 django 项目。

Simple JWT provides a verify route that you can pass a token to which will validate it was singed by the server and it is not expired.简单的 JWT 提供了一个verify路由,您可以将一个令牌传递给该路由,该令牌将验证它是否由服务器签名并且它没有过期。

From the documentation:从文档中:

You can also include a route for Simple JWT's TokenVerifyView if you wish to allow API users to verify HMAC-signed tokens without having access to your signing key:如果您希望允许 API 用户在无需访问您的签名密钥的情况下验证 HMAC 签名的令牌,您还可以包含 Simple JWT 的 TokenVerifyView 的路由:

 from rest_framework_simplejwt.views import TokenVerifyView urlpatterns = [... path('api/token/verify/', TokenVerifyView.as_view(), name='token_verify'), ... ]

If you want to do some other logic you should just write a normal view, use the JWT auth provided, and have the other one forward the token in the request如果您想做一些其他逻辑,您应该只编写一个普通视图,使用提供的 JWT 身份验证,并让另一个在请求中转发令牌

 requests.get(
    "http://service/api/do-thing/", 
    headers={
        "Authorization": self.request.headers["authorization"]
    }
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 django rest 框架对用户进行身份验证 - How to authenticate a user using django rest framework 如何使用Django Rest Framework向用户添加汽车 - How can I add a car to a user with Django Rest Framework 我如何在 django rest 框架中过滤当前用户的查询集 - How i can to filter queryset by current user in django rest framework 如何以编程方式对 Django 中的用户进行身份验证? - How can I programmatically authenticate a user in Django? 如何连接我现有的外部 PostgreSQL 数据库以自动为 Django Rest 框架创建我的 Models.py? - How can I connect my existing external PostgreSQL Database to automatically create my Models.py for a Django Rest Framework? 如何在Django rest框架中对用户进行身份验证? - how to authenticate users in Django rest framework? 我如何使用 django 中的额外字段(除用户名/ email 和密码之外)验证任何用户登录 - How can i authenticate any user for login with an extra field (other then username/ email and password) in django 如何在 django rest 中自动更新我的数据库? - How can i auto update my database in django rest? Django如何删除仅属于某个用户的文章? - Django How can I delete an article that belongs to a certain user only? 如何在 django rest 框架中找到 1 个用户与其他用户之间的距离 - how to find the distance between 1 user and other users in django rest framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM