简体   繁体   English

使用Node.js在JWT中存储秘密信息-Express后端

[英]Storing secret info in JWT, using Node.js - Express backend

I am developing an Angular web app that communicates to its back-end via RESTful API. 我正在开发一个通过RESTful API与后端通信的Angular Web应用程序。 Currently I am using JWTs to store the state (current user ID, etc). 目前,我正在使用JWT来存储状态(当前用户ID等)。 I use jsonwebtoken and passport-jwt packages to implement the JWT with my Node.js-Express application. 我使用jsonwebtoken和passport-jwt软件包通过Node.js-Express应用程序实现JWT。

Sometimes I might need to store in the state some information (for example, a secret encryption key) that is relevant to the session, but that should not be known by the user. 有时,我可能需要在状态中存储与会话相关但用户不应该知道的某些信息(例如,秘密加密密钥)。

Since the JWT payload is signed but not encrypted, its contents are plainly visible to anyone who cares to decode it, it is not appropriate to store secret content directly in the payload. 由于JWT有效负载已签名但未加密,因此任何希望对其进行解码的人都可以清楚地看到其内容,因此不适合将秘密内容直接存储在有效负载中。 While I could implement some kind of self-made encryption either of the whole JWT string or just the secret variable value in the payload, I am looking for the "right" or "traditional" way to handle this. 尽管我可以对整个JWT字符串或仅对有效负载中的秘密变量值实施某种自制的加密,但我正在寻找“正确”或“传统”的方式来进行处理。

The best way would be to store it only on the back end in some kind of persistent storage preferable hashed. 最好的方法是仅将其存储在后端(最好是散列的某种持久性存储)中。

If that isn't possible then you can store in the JWT but you would need to encrypt the data before putting it in there. 如果无法做到这一点,那么您可以将其存储在JWT中,但需要先对数据进行加密,然后再将其放入其中。

Bcrypt is probably the easiest solution that is still secure is https://www.npmjs.com/package/bcryptjs . Bcrypt可能是最安全的最简单解决方案,是https://www.npmjs.com/package/bcryptjs Be aware that the more secure you want it the slower the encryption will be. 请注意,您想要的安全性越高,加密速度就越慢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Node.js中将配置文件用于JWT机密 - Using a config file for JWT secret in Node.js JWT:什么是一个好的秘密密钥,以及如何将它存储在Node.js / Express应用程序中? - JWT: What's a good secret key, and how to store it in an Node.js/Express app? Session object 存储 JWT + 在 node.js 后端处理刷新令牌 - Session object storing JWT + handling refresh tokens in node.js backend 无法将文件上传到 Node.js,使用 Multer 的 Express 后端 - Unable to upload files to Node.js, Express backend using Multer 如何使用JWT从前端(Angular 4)将密钥传递到后端(node js) - How to pass secret key to backend(node js) from frontend(angular 4) using JWT 将 node.js express 后端部署到 heroku - Deploy node.js express backend to heroku 会话不存储在Express for Node.js中 - Session not storing in Express for Node.js 存储和使用JWT和密钥 - Storing and Using JWT & Secret Key 如何在 node.js backEnd 和 angular 2 frontEnd 中使用 jwt 实现社交登录 - how to implement social logins using jwt in node.js backEnd and angular 2 frontEnd 如何在没有前端的情况下使用 Postman 在 Node.js Express 后端服务器上测试 Passport JS Google OAuth - How to test Passport JS Google OAuth on Node.js Express backend server without a frontend using Postman
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM