zoukankan      html  css  js  c++  java
  • 简单的JWT示例

    相关POM引用

            <dependency>
                <groupId>io.jsonwebtoken</groupId>
                <artifactId>jjwt</artifactId>
                <version>0.9.0</version>
            </dependency>

    相关代码

    package com.dhh;
    
    import io.jsonwebtoken.Claims;
    import io.jsonwebtoken.JwtBuilder;
    import io.jsonwebtoken.Jwts;
    import io.jsonwebtoken.SignatureAlgorithm;
    import org.apache.commons.codec.binary.Base64;
    
    import javax.crypto.SecretKey;
    import javax.crypto.spec.SecretKeySpec;
    import java.util.Date;
    import java.util.HashMap;
    import java.util.Map;
    
    /**@author denghanghui
     * @Date 2020 03 06
     * Json Web Token  简单示例
     */
    public class TWJTemple {
    
        public static void main(String[] args) {
            SignatureAlgorithm signatureAlgorithm = getSignatureAlgorithm();
            SecretKey key = getKey();
    
            //设置启动时间和过期时间
            long nowMillis = System.currentTimeMillis();
            long expMillis = nowMillis + 300000;
            Date now = new Date(nowMillis);
            Date expDate = new Date(expMillis);
    
            //JWT中存的信息
            Map map=new HashMap();
            map.put("username","yunnengkeji");
            map.put("password","123456789");
    
            JwtBuilder builder = Jwts.builder();
            builder.setId("1").setSubject("jwt").setIssuer("denghanghui").setIssuedAt(now).signWith(signatureAlgorithm,key).setExpiration(expDate).addClaims(map);
    
            //获取koken
            String token= builder.compact();
            Claims claims=null;
            //解析token内的容器
            try {
                claims =  parseJWT(token);
                Object username= claims.get("username");
                Object password= claims.get("password");
                System.out.println("*****************************************************************");
                System.out.println(username);
                System.out.println(password);
                System.out.println("*****************************************************************");
            }catch (Exception e){
                System.out.println("解析异常");
            }
        }
    
        /**
         * 解析JWT字符串获取Claims容器
         * @param jwt JWT字符串
         * @return Claims容器
         * @throws Exception 解析异常
         */
        public static Claims parseJWT(String jwt) throws Exception {
            SecretKey secretKey = getParseKey();
            return Jwts.parser()
                    .setSigningKey(secretKey)
                    .parseClaimsJws(jwt)
                    .getBody();
        }
    
        /**
         * 获取解析用密钥
         * @return 解析用密钥对象
         */
        public static SecretKey getParseKey(){
            byte[] encodedKey = Base64.decodeBase64("denghanghui");
            return new SecretKeySpec(encodedKey, 0, encodedKey.length, "");
        }
    
        /**
         * 获取加密用密钥
         * @return 加密用密钥对象
         */
        public static SecretKey getKey(){
            byte[] encodedKey = Base64.decodeBase64("denghanghui");
            return new SecretKeySpec(encodedKey, 0, encodedKey.length, "");
        }
    
    
        /**
         * 设置加密签名算法
         * @return 设置加密签名算法
         */
        public static SignatureAlgorithm getSignatureAlgorithm(){
            return SignatureAlgorithm.HS256;
        }
    }
  • 相关阅读:
    【凡尘】---react-redux---【react】
    React生命周期详解
    写文章很难,ai自动生成文章为你来排忧
    怎么用ai智能写作【智媒ai伪原创】快速写文章?
    给大家介绍个Seo伪原创工具吧,可以免费用的哈
    自媒体文章难写,在线伪原创文章生成就简单了
    内容创作难吗 不妨试试智媒ai伪原创
    Ai伪原创工具,轻松几秒出爆文
    什么AI写作软件靠谱,好用?
    分享个免费伪原创工具 关键词自动生成文章
  • 原文地址:https://www.cnblogs.com/blackdeng/p/12426736.html
Copyright © 2011-2022 走看看