zoukankan      html  css  js  c++  java
  • jwt生成token

    jwt官网:https://jwt.io/

    import com.auth0.jwt.JWT;
    import com.auth0.jwt.algorithms.Algorithm;
    import com.auth0.jwt.exceptions.JWTCreationException;
    import org.junit.Assert;
    import org.junit.Test;
    
    import java.io.UnsupportedEncodingException;
    import java.util.HashMap;
    import java.util.Map;
    
    /*
     *
     * @author maduar
     * @date 02/07/2018 10:18 AM
     * @email maduar@163.com
     *
     * */
    public class JWTTest {
    
      @Test
      public void test() throws UnsupportedEncodingException {
    
        // 1. create head
        Map<String, Object> headerClaims = new HashMap();
        headerClaims.put("alg", "HS256");
        headerClaims.put("typ", "JWT");
    
        try {
          // 2. create signture
          Algorithm algorithm = Algorithm.HMAC256("secret");
          String token = JWT.create()
              .withHeader(headerClaims)
              .withClaim("sub", "1234567890") // 2. create playload
              .withClaim("name", "John Doe") // 2. create playload
              .withClaim("admin", true) // 2. create playload
              .sign(algorithm);
    
          String result = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ";
    
          Assert.assertEquals(token, result);
    
        } catch (JWTCreationException exception){
          //Invalid Signing configuration / Couldn't convert Claims.
          exception.printStackTrace();
        }
      }
    }
    

      

  • 相关阅读:
    团队项目-典型用户及用户场景分析
    课堂小练习-找“水王”
    课堂小练习—电梯
    团队项目—用户需求调研报告
    课堂小练习
    团队项目的NABC
    梦断代码—随笔三
    梦断代码—随笔二
    结对开发5_循环二维数组最大字数组
    结对开发4_循环数组的最大值
  • 原文地址:https://www.cnblogs.com/maduar/p/9254541.html
Copyright © 2011-2022 走看看