1 Star 2 Fork 1

罗文星/工具包

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
JwtUtils.java 4.23 KB
一键复制 编辑 原始数据 按行查看 历史
luowenxing 提交于 2021-10-28 15:02 . fix
package com.lwx.utils;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.crypto.spec.SecretKeySpec;
import java.security.Key;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
/**
* Created by luowx on 15/12/24.
*/
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) {
System.out.println(0x10000);
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));
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/luowenxing/utils.git
git@gitee.com:luowenxing/utils.git
luowenxing
utils
工具包
master

搜索帮助