zoukankan      html  css  js  c++  java
  • Token工具类

    <dependency>
                <groupId>com.auth0</groupId>
                <artifactId>java-jwt</artifactId>
                <version>3.4.0</version>
    </dependency>
    import com.auth0.jwt.JWT;
    import com.auth0.jwt.JWTVerifier;
    import com.auth0.jwt.algorithms.Algorithm;
    import com.auth0.jwt.interfaces.DecodedJWT;
    
    import java.util.Date;
    import java.util.HashMap;
    import java.util.Map;
    
    public class TokenUtil {
        //密钥
        private static final String TOKEN_SECRET = "5R5roUcuAu3o5C3o";
        //30分钟超时
        private static final long TIME_OUT = 30 * 60 * 1000;
        //加密
        public static String sign(Long uid) {
            try {
                Date expiration_time = new Date(System.currentTimeMillis() + TIME_OUT);
                Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
                Map<String, Object> headerMap = new HashMap<>(2);
                headerMap.put("type", "JWT");
                headerMap.put("alg", "HS256");
                return JWT.create().withHeader(headerMap).withClaim("uid", uid).withExpiresAt(expiration_time).sign(algorithm);
            } catch (Exception e) {
                return null;
            }
        }
        //解密
        public static DecodedJWT verify(String token) {
            try {
                JWTVerifier verifier = JWT.require(Algorithm.HMAC256(TOKEN_SECRET)).build();
                DecodedJWT jwt = verifier.verify(token);
                return jwt;
            } catch (Exception e) {
                //解码异常
                return null;
            }
        }
    
        public static void main(String[] args) {
            String token = sign(170L);
            System.out.println("token::" + token);
            DecodedJWT jwt = verify(token);
            if (jwt != null) {
                //UID
                System.out.println("uid::" + jwt.getClaim("uid").asLong());
                //TIMEOUT
                System.out.println("timeout::" + jwt.getExpiresAt());
                //ALG
                System.out.println("alg::" + jwt.getAlgorithm());
                //TOKEN
                System.out.println("token::" + jwt.getToken());
                //HEADER
                System.out.println("header::" + jwt.getHeader());
                //PAYLOAD
                System.out.println("payload::" + jwt.getPayload());
                //SIGNATURE
                System.out.println("signature::" + jwt.getSignature());
            } else {
                System.out.println("Decoded JWT Failure");
            }
        }
    }
  • 相关阅读:
    【codeforces 411B】Multi-core Processor
    【codeforces 314C】Sereja and Subsequences
    【hdu 1890】Robotic Sort
    【图灵杯 A】谷神的赌博游戏
    【图灵杯 J】简单的变位词
    【图灵杯 F】一道简单的递推题(矩阵快速幂,乘法模板)
    【图灵杯 E也即POJ 3368】简单的RMQ
    【codeforces 496E】Distributing Parts
    【codeforces 553C】Love Triangles
    Diffie-Hellman Key Exchange – A Non-Mathematician’s Explanation
  • 原文地址:https://www.cnblogs.com/i-tao/p/12882521.html
Copyright © 2011-2022 走看看