简体   繁体   English

Node.js表示JWT如何从Web /电话客户端(jsonwebtoken)发出请求并发送令牌

[英]Nodejs express JWT how to strore and send token from web/phone client (jsonwebtoken)

I'm using NodeJs with Express app to develop webs for browsers and mobile phones. 我正在使用带有Express应用程序的NodeJ来开发用于浏览器和手机的Web。 I'm using JWT because it seems to be the standard and I read that sessions doesn't work well in phones (without browser). 我正在使用JWT,因为它似乎是标准的,而且我读到会话在手机(没有浏览器)中不能很好地工作。 I have this code in the backend configured using "jsonwebtoken": 我在使用“ jsonwebtoken”配置的后端中有以下代码:

'use strict';

const jwt = require('jsonwebtoken');
const path = require('path');
const fs = require('fs');
const privateKEY  = fs.readFileSync(path.join(__dirname, 'private.key'), 'utf8');
const publicKEY  = fs.readFileSync(path.join(__dirname, 'public.key'), 'utf8');

module.exports = {

    sign: (payload) => {
        var signOptions = {
            expiresIn: process.env.TOKEN_EXPIRE_TIME,
            algorithm: "RS256"
        };
        return jwt.sign(payload, privateKEY, signOptions);
    },

    verify: (token) => {
        var verifyOptions = {
            expiresIn: process.env.TOKEN_EXPIRE_TIME,
            algorithm: ["RS256"]
        };
        try {
            return jwt.verify(token, publicKEY, verifyOptions);
        } catch (err) {
            return false;
        }
    },

    decode: (token) => {
        return jwt.decode(token, {
            complete: true
        });
    }

};

But I don't know which is the way to implement the front end side. 但是我不知道哪种方法可以实现前端。 I need to know how to store the token for both devices (localStorage? sessionStorage? cookies? others?) and what could be a good way to make the links (href tag in html) sending the token from that storage method. 我需要知道如何为两种设备(localStorage,sessionStorage,Cookie或其他设备)存储令牌,以及使链接(HTML中的href标记)从该存储方法发送令牌的好方法是什么?

Type of storage for the JWT token at the front-end is totally dependent on your applications requirement and how do you want to handle user's login/logout session. 前端JWT令牌的存储类型完全取决于您的应用程序要求以及您要如何处理用户的登录/注销会话。

See this article on JWT uses, give a clap if you like 请参阅有关JWT用法的这篇文章 ,如果喜欢,请鼓掌

If you're not clear about this then you can usr localStorage as the data stored in it stays event after the browser is closed - if it doesn't bother your requirement. 如果不清楚这一点,则可以在关闭浏览器后usr localStorage,因为其中存储的数据将保持事件状态-如果不打扰您的需求。

For sending token from link - there is two things you can do - 要从链接发送令牌-您可以执行以下两项操作-

  1. Create the link dynamically and add the token as query parameter to your link 动态创建链接并将令牌作为查询参数添加到链接
  2. Or you can create onClick event which fetches the token from storage and fires a request to your server 或者,您可以创建onClick事件,该事件从存储中获取令牌并向服务器发送请求

I hope that helps. 希望对您有所帮助。

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

相关问题 jsonwebtoken不解码nodejs express中的jwt - jsonwebtoken not decoding jwt in nodejs express 如何在node express中实现JWT(JsonWebToken)注销 - How to implement JWT(JsonWebToken) logout in node express 如何使用 express 在标头中发送 JWT 令牌? - How send JWT token in headers using express? 如何使用passport-jwt在nodejs中发送/提取JWT令牌? - How to send/extract JWT token in nodejs with passport-jwt? NodeJS-在注册验证电子邮件中发送Json Web令牌(JWT)是否安全? - NodeJS - Is it safe to send Json Web Token(JWT) in register verification email? 如何使用Express(NodeJS)从服务器向客户端发送大量文件 - How to send a lot of files from server to client using express (NodeJS) 如何从后端(快递)在nodeJS中创建html文件并将其发送到客户端? - How to create and send html file to client in nodeJS from backend(express)? NodeJs Express passport-jwt 从令牌中获取错误的用户 - NodeJs Express passport-jwt getting wrong user from token 如何安全地保存从 auth0 登录收到的 JWT 令牌(nodejs express) - How to save JWT token recieved from auth0 login securely (nodejs express) 我如何将 jsonwebtoken 存储在前端的 cookie 上,以便客户端可以发回令牌以进行身份验证 - How do i store jsonwebtoken on cookie for front-end so client can send back the token for auth
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM