使用 Django 和 React 保护 Web 应用程序

发布于 2025-01-20 11:04:19 字数 763 浏览 1 评论 0原文

我正在尝试使用这两种技术来制作一个安全的网络应用程序[目前正在学习 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情绪少女 2025-01-27 11:04:19

是的,这绝对是一个将客户端应用程序和后端服务器应用程序分开的好主意。

  • 您基本上可以使用任何前端框架/应用程序/脚本通过其余 api 访问后端。
  • 客户可以利用您的后端服务的功能来扩展自己的应用程序。
  • 您可以通过其余 api(多品牌、转售)创建使用相同后端或同一后端的不同部分的多个不同前端。或者您可以每隔一年将前端框架更换为新框架。
  • 使用其余 api 也可以更轻松地创建不同的自动化。

这样的例子还在继续。

对于 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.

  • You can access the backend through the rest api basically with any frontend framework/app/script.
  • Customers are able to extend their own applications with the abilities of your backend service.
  • You can create multiple different frontends that use the same backend or different parts of the same backend via the rest api (multi-branding, reselling). Or you can just swap the frontend framework every second year to a new one.
  • It's also easier to create different automations by using the rest api.

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文