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));
}
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);
}
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));
}
}
>>>源码下载链接>>>