• Token 基本操作


    微信公众平台开发概述

    1. 导入
    <dependency>
          <groupId>io.jsonwebtoken</groupId>
          <artifactId>jjwt</artifactId>
          <version>0.6.0</version>
    </dependency>
    
    1. 格式化代码:ctrl+alt+L

    CreateJwtParse01

    public class CreateJwtParse01 {
        public static void main(String[] args) {
            String jwtTest = CreateJwtTest();
            System.out.println(jwtTest);
            System.out.println("______________________________");
            ParseJwtTest(jwtTest);
        }
    
        static String CreateJwtTest() {
            JwtBuilder builder = Jwts.builder()
                    .setId("2450")
                    .setSubject("张三三")
                    .setIssuedAt(new Date()) //设置签发时间
                    .signWith(SignatureAlgorithm.HS256, "cheep1"); //设置签名秘钥
            return builder.compact();
        }
    
        static void ParseJwtTest(String str) {
            Claims claims =
                    Jwts.parser().setSigningKey("cheep1").parseClaimsJws(str).getBody();
            System.out.println("id:" + claims.getId());
            System.out.println("subject:" + claims.getSubject());
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            System.out.println("IssuedAt:" + sdf1.format(claims.getIssuedAt()));
        }
    }
    //eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiIyNDUwIiwic3ViIjoi5byg5LiJ5LiJIiwiaWF0IjoxNjA0OTA3NzUyfQ.1c1SK9Knij6i3hucepdv-C97pq-dYmUltovNKqc_Wv8
    //______________________________
    //id:2450
    //subject:张三三
    //IssuedAt:2020-11-09 15:42:32
    

    CreateJwtParse02 设置过期时间为1分钟

    public class CreateJwtParse02 {
        public static void main(String[] args) {
            String jwtTest01 = CreateJwtTest01();
            System.out.println(jwtTest01);
            System.out.println("______________________________");
            ParseJwtTest01(jwtTest01);
        }
    
        static String CreateJwtTest01() {
            //为了方便测试,我们将过期时间设置为1分钟
            long now = System.currentTimeMillis();//当前时间
            long exp = now + 1000 * 60;//过期时间为1分钟:60000
            JwtBuilder builder = Jwts.builder().setId("888")
                    .setSubject("小白")
                    .setIssuedAt(new Date())
                    .signWith(SignatureAlgorithm.HS256, "itcast")
                    .setExpiration(new Date(exp)); // 设置过期时间
            return builder.compact();
        }
    
        static void ParseJwtTest01(String str) {
            Claims claims = Jwts.parser().setSigningKey("itcast").parseClaimsJws(str).getBody();
            System.out.println("id:" + claims.getId());
            System.out.println("subject:" + claims.getSubject());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy‐MM‐dd hh:mm:ss");
            System.out.println("签发时间:" + sdf.format(claims.getIssuedAt()));
            System.out.println("过期时间:" + sdf.format(claims.getExpiration()));
            System.out.println("当前时间:" + sdf.format(new Date()));
            // 测试运行,当未过期时可以正常读取,当过期时会引发
            // io.jsonwebtoken.ExpiredJwtException异常。
        }
    }
    /*eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiI4ODgiLCJzdWIiOiLlsI_nmb0iLCJpYXQiOjE2MDQ5MDg2MDAsImV4cCI6MTYwNDkwODY2MH0.uNQxJOpi-sn7zTR2v2Fttt1A8pfhLChWGPFxdby7WRs
                ______________________________
        id:888
        subject:小白
        签发时间:2020‐11‐09 03:56:40
        过期时间:2020‐11‐09 03:57:40
        当前时间:2020‐11‐09 03:56:42*/
    

    CreateJwtParse01 Token添加自定义属性

    public class CreateJwtParse03 {
        public static void main(String[] args) {
            String jwtTest02 = CreateJwtTest02();
            System.out.println(jwtTest02);
            System.out.println("______________________________");
            ParseJwtTest02(jwtTest02);
        }
    
        static String CreateJwtTest02() {
            //为了方便测试,我们将过期时间设置为1分钟
            long now = System.currentTimeMillis();//当前时间
            long exp = now + 1000 * 60;//过期时间为1分钟
            JwtBuilder builder = Jwts.builder().setId("888")
                    .setSubject("小白")
                    .setIssuedAt(new Date())
                    .signWith(SignatureAlgorithm.HS256, "itcast")
                    .setExpiration(new Date(exp))
                    .claim("roles", "admin")
                    .claim("logo", "logo.png");
            return builder.compact();
        }
    
        static void ParseJwtTest02(String str) {
            Claims claims = Jwts.parser().setSigningKey("itcast").parseClaimsJws(str).getBody(
            );
            System.out.println("id:" + claims.getId());
            System.out.println("subject:" + claims.getSubject());
            System.out.println("roles:" + claims.get("roles"));
            System.out.println("logo:" + claims.get("logo"));
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy‐MM‐dd hh:mm:ss");
            System.out.println("签发时间:" + sdf.format(claims.getIssuedAt()));
            System.out.println("过期时间:" + sdf.format(claims.getExpiration()));
            System.out.println("当前时间:" + sdf.format(new Date()));
        }
    
        /*eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiI4ODgiLCJzdWIiOiLlsI_nmb0iLCJpYXQiOjE2MDQ5MDg5OTYsImV4cCI6MTYwNDkwOTA1NSwicm9sZXMiOiJhZG1pbiIsImxvZ28iOiJsb2dvLnBuZyJ9.JP85Ez64SEcssRRQ-SB6Ba-WvXa_52r1pRjAnxZxeHQ
                ______________________________
        id:888
        subject:小白
        roles:admin
        logo:logo.png
        签发时间:2020‐11‐09 04:03:16
        过期时间:2020‐11‐09 04:04:15
        当前时间:2020‐11‐09 04:03:17*/
    }
    
  • 相关阅读:
    python 元组及操作
    python 字典所有操作
    python 列表操作
    python 运算符
    python while循环
    python 分支语句 等值判断 逻辑运算符
    GDI+_从Bitmap里得到的Color数组值分解
    服务器设计策略
    服务器设计策略
    IOCP的缓冲区
  • 原文地址:https://www.cnblogs.com/Twittery/p/13949094.html
Copyright © 2020-2023  润新知