zoukankan      html  css  js  c++  java
  • [Node.js] Creating JWTs (JSON Web Tokens) in Node

    In this lesson we will look at all of the pieces that combine together to create a JWT (j AWT) or JSON Web Token. You will use node to create a JWT, and then verify it in the JWT debugger.

    What is the JSON Web Token structure?

    JSON Web Tokens consist of three parts separated by dots (.), which are:

    • Header
    • Payload
    • Signature

    Therefore, a JWT typically looks like the following.

    xxxxx.yyyyy.zzzzz

    Let's break down the different parts.

    Create a header:

    The header typically consists of two parts: the type of the token, which is JWT, and the hashing algorithm being used, such as HMAC SHA256 or RSA.

    let header = {
      typ: 'JWT',
      alg: 'HS256'
    };
    
    header = new Buffer(JSON.stringify(header)).toString('base64');
    
    console.log(header);

    Create a paylaod:

    The second part of the token is the payload, which contains the claims. Claims are statements about an entity (typically, the user) and additional metadata. There are three types of claims: reservedpublic, and privateclaims.

    let payload = {
      iat: Date.now(),
      iss: 'nodebotanist',
      username: 'nodebotanist'
    };
    
    payload = new Buffer(JSON.stringify(payload)).toString('base64');
    
    console.log("payload", payload);

    Create a signature:

    To create the signature part you have to take the encoded header, the encoded payload, a secret, the algorithm specified in the header, and sign that.

    For example if you want to use the HMAC SHA256 algorithm, the signature will be created in the following way:

    HMACSHA256(
      base64UrlEncode(header) + "." +
      base64UrlEncode(payload),
      secret)
    let key = header + '.' + payload;
    let signature = crypto.createHmac('sha256', 'zhentian');
    signature.update(key);
    key = signature.digest('base64');
    
    let token = header + '.' +payload + '.' + key
    console.log("token", token)

     ----------------

    let header = {
      typ: 'JWT',
      alg: 'HS256'
    };
    
    header = new Buffer(JSON.stringify(header)).toString('base64');
    
    console.log(header);
    
    
    let payload = {
      iat: Date.now(),
      iss: 'nodebotanist',
      username: 'nodebotanist'
    };
    
    payload = new Buffer(JSON.stringify(payload)).toString('base64');
    
    console.log("payload", payload);
    
    
    let key = header + '.' + payload;
    let signature = crypto.createHmac('sha256', 'zhentian');
    signature.update(key);
    key = signature.digest('base64');
    
    let token = header + '.' +payload + '.' + key
    console.log("token", token)

    Debugger

  • 相关阅读:
    Xshell_Plus破解永久版绿色安装使用
    springboot接入mybatis管理数据库
    idea 配置依赖管理Gradle
    破解版Navicat安装使用
    Centos8安装 python3并保留系统的python2
    windows 命令行 jps查看java进程没有反应,如何用jps查看java进程
    @ConfigurationProperties 无法将yml或properties中的内容读取到bean
    Python 生成requirement 使用requirements.txt
    优秀的博客网站
    将application.yml放到jar包外
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5634696.html
Copyright © 2011-2022 走看看