是否有快速选项来安全保存 JWT 令牌?

Is there a quick option to save the JWT token secure?

在学习 MERN 堆栈教程时,我问自己将 JWT 令牌存储在本地存储中是否安全。谷歌搜索 1 秒后,我得到了类似“绝对不是”的答案。

当前api调用如下:

const {data} = await Axios.post("/api/users/signin", {email, password});
localStorage.setItem('localUserInfo', JSON.stringify(data));

我想知道,因为讲师使用了其他安全技术,例如使用角色和 bcrypt(并且应该保护 react 免受 xss)。

如果不是:是否有其他快速选项来安全保存它?

您可以使用 http-only cookie to prevent malicious scripts from reading sensible cookie data, e.g. in to use them in Cross Site Scripting 次攻击。

既然你提到你正在学习 MERN 堆栈——你可以使用 cookie-session middleware, learn more about security in express here

它提供多种配置来确保会话 cookie 的安全:

app.use(session({
  name: 'session',
  keys: ['key1', 'key2'],
  cookie: {
    secure: true, // enforce https
    httpOnly: true, // ensure cookies are only sent via http(s)
    domain: 'example.com', // specify hosts that are allowed to receive the cookie
    expires: expiryDate // define lifespan
  }
}))