使用 Django 和 React 保护 Web 应用程序
我正在尝试使用这两种技术来制作一个安全的网络应用程序[目前正在学习 React (60%) 和 Django (<50%)。这就像一个医疗数据库,以便医生和护士输入患者的信息。显然他们需要登录。我想实现基于 React 的 UI(而不是使用经典方法从 django 创建视图),所以我发现了很多像这样的教程:
https://www.digitalocean.com/community/tutorials/build-a-to-do-application-using-django-and-react
它基本上将 Django 变成了一个 RestAPI,然后是 React前端使用 axios 从端点检索数据。听起来一点也不差(与 Django 在网页中渲染数据的本机方法相比),但问题是我不知道如何确保安全,你知道,Django 提供了一个auth系统,不得不说,相当不错,也很安全,但是在这样结构的项目中,auth需要在React中完成,所以出现了很多问题:
- 首先,这是一个好主意吗做一个这个项目结构?(如果不是,那么什么可能是一个好的结构)
- 如果是,如何保护 API,以便只有登录的用户才能与其交互?(什么机制可以确保保护)
I'm experimenting with these 2 technologies to make a secure web app [Currently learning React (60%) and Django (<50%). This is intended to be like a medical database, so doctors and nurses enters their patients' information. They need to login obviously. I wanted to implement React-based UI (And not using the classic method to create views from django), so I've found many tutorials just like this one:
https://www.digitalocean.com/community/tutorials/build-a-to-do-application-using-django-and-react
It basically turns Django into a restAPI, and then the React frontend uses axios to retrieve data from the endpoint. Sounds not bad at all (comparing to the native method of rendering data in a webpage from Django), but the problem is that I have no idea on how to make this secure, you know, Django provides an auth system, which is pretty good and secure, I have to say, but in a project with this structure, the auth needs to be done in React, so there many questions appear:
- To start with, is it a good idea to make a project of this structure? (If no, then what could be a good one)
- If it's a yes, how can I protect the API so only logged in users can interact with it? (What mechanisms to ensure protection)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,这绝对是一个将客户端应用程序和后端服务器应用程序分开的好主意。
这样的例子还在继续。
对于 django rest api auth 我会推荐 令牌身份验证 其中已经包含在 Django REST Framework 中并且对于 React 使用 此实现登录的教程和令牌处理。
并且不要忘记在您的服务器上使用 TLS,并创建 API 文档。 (示例)
Yes, this is absolutely a good idea to separate the client application and the backend server application.
And the list goes on.
For django rest api auth I would recommend Token Authentication which is already included in the Django REST Framework and for React use this tutorial for implementing the login and the token handling.
And don't forget to use TLS on your servers, and create API documentation. (Example)