代码拉取完成,页面将自动刷新
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));
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。