使用 ReactJS 在 localStorage 中存储 JWT 是否安全? [英] Is it safe to store a JWT in localStorage with ReactJS?

查看:35
本文介绍了使用 ReactJS 在 localStorage 中存储 JWT 是否安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用 ReactJS 构建一个单页应用程序.

I'm currently building a single page application using ReactJS.

我读到不使用 localStorage 的原因之一是 XSS 漏洞.

I read that one of the reasons for not using localStorage is because of XSS vulnerabilities.

由于 React 会转义所有用户输入,现在使用 localStorage 是否安全?

Since React escapes all user input, would it now be safe to use localStorage?

推荐答案

在大多数现代单页应用程序中,我们确实必须将令牌存储在客户端的某个地方(最常见的用例 - 保持用户登录页面刷新后).

In most of the modern single page applications, we indeed have to store the token somewhere on the client side (most common use case - to keep the user logged in after a page refresh).

共有 2 个选项可用:Web 存储(会话存储、本地存储)和客户端 cookie.这两个选项都被广泛使用,但这并不意味着它们非常安全.

There are a total of 2 options available: Web Storage (session storage, local storage) and a client side cookie. Both options are widely used, but this doesn't mean they are very secure.

Tom Abbott 很好地总结了 JWTsessionStorage 和 localStorage 安全:

Tom Abbott summarizes well the JWT sessionStorage and localStorage security:

Web 存储 (localStorage/sessionStorage) 可通过同一域中的 JavaScript 访问.这意味着在您的网站上运行的任何 JavaScript 都可以访问网络存储,因此可能容易受到跨站点脚本 (XSS) 攻击.简而言之,XSS 是一种漏洞,攻击者可以在其中注入将在您的页面上运行的 JavaScript.基本的 XSS 攻击尝试通过表单输入注入 JavaScript,攻击者将

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆