feat: 工具类完成
This commit is contained in:
parent
3c6e6ad0a0
commit
7660a1a9ad
|
@ -0,0 +1,52 @@
|
||||||
|
package com.sky.common.utils;
|
||||||
|
|
||||||
|
import io.jsonwebtoken.Claims;
|
||||||
|
import io.jsonwebtoken.JwtBuilder;
|
||||||
|
import io.jsonwebtoken.Jwts;
|
||||||
|
import io.jsonwebtoken.SignatureAlgorithm;
|
||||||
|
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class JwtUtil {
|
||||||
|
/**
|
||||||
|
* 生成JWT
|
||||||
|
* 使用Hs256算法, 私匙使用固定秘钥
|
||||||
|
* @param secretKey jwt秘钥
|
||||||
|
* @param ttlMillis jwt过期时间(毫秒)
|
||||||
|
* @param claims 设置的信息
|
||||||
|
* @return 加密后JWT
|
||||||
|
*/
|
||||||
|
public static String createJWT(String secretKey, long ttlMillis, Map<String, Object> claims) {
|
||||||
|
// 指定签名的时候使用的签名算法,也就是header那部分
|
||||||
|
SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256;
|
||||||
|
// 生成JWT的时间
|
||||||
|
long expMillis = System.currentTimeMillis() + ttlMillis;
|
||||||
|
Date date = new Date(expMillis);
|
||||||
|
JwtBuilder builder = Jwts.builder()
|
||||||
|
// 如果有私有声明,一定要先设置这个自己创建的私有的声明,这个是给builder的claim赋值,一旦写在标准的声明赋值之后,就是覆盖了那些标准的声明的
|
||||||
|
.setClaims(claims)
|
||||||
|
// 设置签名使用的签名算法和签名使用的秘钥
|
||||||
|
.signWith(signatureAlgorithm, secretKey.getBytes(StandardCharsets.UTF_8))
|
||||||
|
// 设置过期时间
|
||||||
|
.setExpiration(date);
|
||||||
|
|
||||||
|
return builder.compact();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Token解密
|
||||||
|
* @param secretKey jwt秘钥
|
||||||
|
* @param token token
|
||||||
|
* @return 解密后数据
|
||||||
|
*/
|
||||||
|
public static Claims parseJWT(String secretKey, String token) {
|
||||||
|
// 得到DefaultJwtParser
|
||||||
|
return Jwts.parser()
|
||||||
|
// 设置签名的秘钥
|
||||||
|
.setSigningKey(secretKey.getBytes(StandardCharsets.UTF_8))
|
||||||
|
// 设置需要解析的jwt
|
||||||
|
.parseClaimsJwt(token).getBody();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,35 @@
|
||||||
|
package com.sky.common.utils;
|
||||||
|
|
||||||
|
import java.security.MessageDigest;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
|
||||||
|
|
||||||
|
public final class MD5 {
|
||||||
|
|
||||||
|
public static String encrypt(String strSrc) {
|
||||||
|
try {
|
||||||
|
char hexChars[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
|
||||||
|
'9', 'a', 'b', 'c', 'd', 'e', 'f' };
|
||||||
|
byte[] bytes = strSrc.getBytes();
|
||||||
|
MessageDigest md = MessageDigest.getInstance("MD5");
|
||||||
|
md.update(bytes);
|
||||||
|
bytes = md.digest();
|
||||||
|
int j = bytes.length;
|
||||||
|
char[] chars = new char[j * 2];
|
||||||
|
int k = 0;
|
||||||
|
for (int i = 0; i < bytes.length; i++) {
|
||||||
|
byte b = bytes[i];
|
||||||
|
chars[k++] = hexChars[b >>> 4 & 0xf];
|
||||||
|
chars[k++] = hexChars[b & 0xf];
|
||||||
|
}
|
||||||
|
return new String(chars);
|
||||||
|
} catch (NoSuchAlgorithmException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
throw new RuntimeException("MD5加密出错!!+" + e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
System.out.println(MD5.encrypt("111111"));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue