zoukankan      html  css  js  c++  java
  • Token的生成和检验

    package TestToken;
    
    import com.auth0.jwt.JWT;
    import com.auth0.jwt.JWTVerifier;
    import com.auth0.jwt.algorithms.Algorithm;
    import com.auth0.jwt.interfaces.Claim;
    import com.auth0.jwt.interfaces.DecodedJWT;
    import org.junit.Test;
    
    import java.io.UnsupportedEncodingException;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.HashMap;
    import java.util.Map;
    
    public class TokenTest {
    
        //公共密钥客户端不会知道
        public static String SECRET="FreeMaNong";
    
        public static  String  createToken() throws UnsupportedEncodingException {
            //签名发布时间
            Date iatDate=new Date();
            System.out.println(iatDate);//英文时间
    
            //设置签名过期时间  1分钟
            Calendar nowTime=Calendar.getInstance();
            nowTime.add(Calendar.MINUTE,1);
            Date expiresDate=nowTime.getTime();
            //System.out.println(expiresDate);
    
            Map<String,Object> map=new HashMap<String, Object>();
            map.put("alg","HS256");//设置算法 为HS256
            map.put("typ","JWT");//设置类型为JWT
            String token=JWT.create().withHeader(map)
                    .withClaim("name","Free码农")
                    .withClaim("age","28")
                    .withClaim("org","今日头条")
                    .withClaim("username","chenyu")
                    .withIssuedAt(iatDate)//设置签发时间
                    .withExpiresAt(expiresDate)//设置过去时间 过期时间大于签发时间
                    .sign(Algorithm.HMAC256(SECRET));//用公共密钥加密
           //System.out.println(token);
           return token;
        }
    
        public static Map<String,Claim> verifyToken(String token) throws UnsupportedEncodingException {
            JWTVerifier verifier =JWT.require(Algorithm.HMAC256(SECRET)).build();//用公共密钥解密验证
            DecodedJWT jwt=null;
            try{
                jwt=verifier.verify(token);
            }catch (Exception e)
            {
                throw new RuntimeException("登录凭证已过去,请重新登录");
            }
            return jwt.getClaims();
        }
    
    
        @Test
        public void TestToken() throws UnsupportedEncodingException {
            String token=createToken();
            System.out.println("Token:"+token);
            Map<String,Claim> claims=verifyToken(token);
            System.out.println(claims.get("name").asString());
            System.out.println(claims.get("age").asString());
            System.out.println(claims.get("username").asString());
            System.out.println(claims.get("org")==null?null:claims.get("org").asString());
    
            //测试过期token
    //        String GuoQiToken="eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJhdXRoMCJ9.izVguZPRsBQ5Rqw6dhMvcIwy8_9lQnrO3vpxGwPCuzs";
    //        Map<String,Claim> claims2=verifyToken(GuoQiToken);
        }
    
    
    
        @Test
        public void Test() throws UnsupportedEncodingException {
            Algorithm algorithm = Algorithm.HMAC256("secret");
            String token = JWT.create().withIssuer("auth0") .sign(algorithm);
            System.out.println(token);
        }
    }
    

      

  • 相关阅读:
    使用crontab定时执行脚本时别忘了输出重定向>
    php 中函数获取可变参数的方法, 这个语法有点像 golang 语言中的
    单词number 和 numeral 的区别
    vim 调到闭合的{
    [转]文件IO详解(二)---文件描述符(fd)和inode号的关系
    js中有包装类,java中也有包装类
    cin中函数的作用
    string类小结
    结构、位域、联合、枚举之小小总结
    运算符重载(C++)
  • 原文地址:https://www.cnblogs.com/CY-947205926/p/8904086.html
Copyright © 2011-2022 走看看