Django rest auth 安全地存储令牌

Django rest auth store token securely

我正在尝试在后端使用 django-rest-auth 库实现身份验证,并且我在前端使用 React。 Django-rest-auth returns 身份验证后的令牌,我正在猜测如何处理它。

每种在客户端存储令牌的方法都有一些弱点:

  • 将令牌存储在 HTTPOnly cookie 中会使应用程序容易受到 CSRF 攻击
  • 将令牌存储在 localStorage 中会使应用程序容易受到 XSS 攻击

我个人是用localStorage来存储token的,因为方便。 React 具有内置的 XSS 预防功能,您还可以打开 CSP(内容安全保护)。我写了一篇关于我的方法的文章:https://saasitive.com/tutorial/react-token-based-authentication-django/ - the httpOnly vs localStorage discussion is at the end of the post. There is also full tutorial how to start SaaS app with Django and React (link).