java加密算法之JWT篇

Posted 剑行歌之

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java加密算法之JWT篇相关的知识,希望对你有一定的参考价值。

加密

  字符串
public static String token(String subject, String secretKey, Date date) {
    Key key = decodeKey(secretKey);
    String token = Jwts.builder().setExpiration(date).setSubject(subject).signWith(SignatureAlgorithm.HS256, key).compact();
    String base64Token = new BASE64Encoder().encode(token.getBytes());
    base64Token = base64Token.replace('=', '*');
    base64Token = base64Token.replace("\\n", "");
    base64Token = base64Token.replace("\\r", "");
    return base64Token;
}

  多数据
public static String tokenParams(Map<String, Object> claims, String secretKey, Date date) {
    Key key = decodeKey(secretKey);
    String token = Jwts.builder().setExpiration(date).setClaims(claims).signWith(SignatureAlgorithm.HS256, key).compact();
    String base64Token = new BASE64Encoder().encode(token.getBytes());
    base64Token = base64Token.replace('=', '*');
    base64Token = base64Token.replace("\\n", "");
    base64Token = base64Token.replace("\\r", "");
    return base64Token;
}

解密

  字符串
   public static String verify(String token, String secretKey) {
    try {
        token = token.replace('*', '=');
        Key key = decodeKey(secretKey);
        String decodeToken = new String(new BASE64Decoder().decodeBuffer(token));
        String subject = Jwts.parser().setSigningKey(key).parseClaimsJws(decodeToken).getBody().getSubject();
        return subject;
    } catch (Exception e) {

    }
    return null;
}
  多数据
  public static Map<String, Object> verifyParams(String token, String secretKey) {
    try {
        token = token.replace('*', '=');
        Key key = decodeKey(secretKey);
        String decodeToken = new String(new BASE64Decoder().decodeBuffer(token));
        Map<String, Object> claims = Jwts.parser().setSigningKey(key).parseClaimsJws(decodeToken).getBody();
        return claims;
    } catch (Exception e) {

    }
    return null;
}

测试

public static void main(String[] args) {
    String subject = "A478YSKJDFHJL84673IUASD8";
    String secretKey = generateSecretKey();
    Date date = new Date();
    String strToken = token(subject,secretKey,MyDateUtils.addDays(date,1));
    System.out.println("There is token result :"+strToken);
    String verify = verify(strToken,secretKey);
    System.out.println("There is verify token result :"+verify);
    Map<String,Object> params = new HashMap<>();
    params.put("key","value");
    String objToken = tokenParams(params, secretKey, MyDateUtils.addDays(date, 1));
    System.out.println("There is token object result :"+objToken);
    Map<String,Object> verifyObj = verifyParams(objToken,secretKey);
    System.out.println("There is verify object token result :"+JsonUtils.toJson(verifyObj));
}

测试结果

在这里插入图片描述

token过期测试

public static void main(String[] args) {
    String subject = "A478YSKJDFHJL84673IUASD8";
    String secretKey = generateSecretKey();
    Date date = new Date();
    String strToken = token(subject,secretKey,MyDateUtils.addDays(date,-1));
    System.out.println("There is token result :"+strToken);
    String verify = verify(strToken,secretKey);
    System.out.println("There is verify token result :"+verify);
}

token过期测试结果

在这里插入图片描述

全代码

public class JwtUtils {

    public static String secretKey = "";

    public static String generateSecretKey() {
        String secretKey = "";
        String uuid = new BASE64Encoder().encode(UUID.randomUUID().toString().getBytes());
        secretKey = uuid;
        return secretKey;
    }

    public static Key generateKey(String secretKey) {
        Key key = null;
        try {
            byte[] keyBytes = new BASE64Decoder().decodeBuffer(secretKey);
            key = new SecretKeySpec(keyBytes, SignatureAlgorithm.HS256.getJcaName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return key;
    }

    public static Key decodeKey(String secretKey) {
        Key key = null;
        try {
            byte[] keyBytes = new BASE64Decoder().decodeBuffer(secretKey);
            key = new SecretKeySpec(keyBytes, SignatureAlgorithm.HS256.getJcaName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return key;
    }

    public static String tokenParams(Map<String, Object> claims, String secretKey, Date date) {
        Key key = decodeKey(secretKey);
        String token = Jwts.builder().setExpiration(date).setClaims(claims).signWith(SignatureAlgorithm.HS256, key).compact();
        String base64Token = new BASE64Encoder().encode(token.getBytes());
        base64Token = base64Token.replace('=', '*');
        base64Token = base64Token.replace("\\n", "");
        base64Token = base64Token.replace("\\r", "");
        return base64Token;
    }

    public static Map<String, Object> verifyParams(String token, String secretKey) {
        try {
            token = token.replace('*', '=');
            Key key = decodeKey(secretKey);
            String decodeToken = new String(new BASE64Decoder().decodeBuffer(token));
            Map<String, Object> claims = Jwts.parser().setSigningKey(key).parseClaimsJws(decodeToken).getBody();
            return claims;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String token(String subject, String secretKey, Date date) {
        Key key = decodeKey(secretKey);
        String token = Jwts.builder().setExpiration(date).setSubject(subject).signWith(SignatureAlgorithm.HS256, key).compact();
        String base64Token = new BASE64Encoder().encode(token.getBytes());
        base64Token = base64Token.replace('=', '*');
        base64Token = base64Token.replace("\\n", "");
        base64Token = base64Token.replace("\\r", "");
        return base64Token;
    }

    public static String verify(String token, String secretKey) {
        try {
            token = token.replace('*', '=');
            Key key = decodeKey(secretKey);
            String decodeToken = new String(new BASE64Decoder().decodeBuffer(token));
            String subject = Jwts.parser().setSigningKey(key).parseClaimsJws(decodeToken).getBody().getSubject();
            return subject;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        String subject = "A478YSKJDFHJL84673IUASD8";
        String secretKey = generateSecretKey();
        Date date = new Date();
        String strToken = token(subject,secretKey,MyDateUtils.addDays(date,-1));
        System.out.println("There is token result :"+strToken);
        String verify = verify(strToken,secretKey);
        System.out.println("There is verify token result :"+verify);
        Map<String,Object> params = new HashMap<>();
        params.put("key","value");
        String objToken = tokenParams(params, secretKey, MyDateUtils.addDays(date, 1));
        System.out.println("There is token object result :"+objToken);
        Map<String,Object> verifyObj = verifyParams(objToken,secretKey);
        System.out.println("There is verify object token result :"+JsonUtils.toJson(verifyObj));
    }
}

>>>源码下载链接>>>

以上是关于java加密算法之JWT篇的主要内容,如果未能解决你的问题,请参考以下文章

java加密算法之RSA篇

java加密算法之RSA篇

java加密算法之RSA篇

java加密算法之AES篇

java加密算法之DES篇

java加密算法之base64篇