|
@@ -37,12 +37,8 @@ public class JwtUtil {
|
|
|
|
|
|
/**
|
|
/**
|
|
* 生成签名,6EXPIRE_TIME过期
|
|
* 生成签名,6EXPIRE_TIME过期
|
|
- *
|
|
|
|
- * @param **User**
|
|
|
|
- * @param **password**
|
|
|
|
- * @return
|
|
|
|
*/
|
|
*/
|
|
- public static String createToken(Integer userID) {
|
|
|
|
|
|
+ public static String createToken(Integer userId) {
|
|
try {
|
|
try {
|
|
// 设置过期时间
|
|
// 设置过期时间
|
|
Date date = new Date(System.currentTimeMillis() + EXPIRE_TIME);
|
|
Date date = new Date(System.currentTimeMillis() + EXPIRE_TIME);
|
|
@@ -55,30 +51,8 @@ public class JwtUtil {
|
|
// 返回token字符串
|
|
// 返回token字符串
|
|
return JWT.create()
|
|
return JWT.create()
|
|
.withHeader(header)
|
|
.withHeader(header)
|
|
- .withClaim("uid", userID)
|
|
|
|
- /*.withClaim("aud", mobileOrEmail)*/
|
|
|
|
- .withExpiresAt(date)
|
|
|
|
- .sign(algorithm);
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- e.printStackTrace();
|
|
|
|
- return null;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- public static String createTokenBySecret(String secret) {
|
|
|
|
- try {
|
|
|
|
- // 设置过期时间
|
|
|
|
- Date date = new Date(System.currentTimeMillis() + EXPIRE_TIME);
|
|
|
|
- // 私钥和加密算法
|
|
|
|
- Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
|
|
|
|
- // 设置头部信息
|
|
|
|
- Map<String, Object> header = new HashMap<>(2);
|
|
|
|
- header.put("typ", "JWT");
|
|
|
|
- header.put("alg", "HS256");
|
|
|
|
- // 返回token字符串
|
|
|
|
- return JWT.create()
|
|
|
|
- .withHeader(header)
|
|
|
|
- .withClaim("uid", secret)
|
|
|
|
|
|
+ .withClaim("uid", userId)
|
|
|
|
+ /*.withClaim("aud", mobile)*/
|
|
.withExpiresAt(date)
|
|
.withExpiresAt(date)
|
|
.sign(algorithm);
|
|
.sign(algorithm);
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
@@ -89,9 +63,6 @@ public class JwtUtil {
|
|
|
|
|
|
/**
|
|
/**
|
|
* 检验token是否正确
|
|
* 检验token是否正确
|
|
- *
|
|
|
|
- * @param **token**
|
|
|
|
- * @return
|
|
|
|
*/
|
|
*/
|
|
public static boolean isVerify(String token) {
|
|
public static boolean isVerify(String token) {
|
|
try {
|
|
try {
|
|
@@ -106,31 +77,14 @@ public class JwtUtil {
|
|
|
|
|
|
/**
|
|
/**
|
|
* 从token解析出uid信息,用户ID
|
|
* 从token解析出uid信息,用户ID
|
|
- *
|
|
|
|
- * @param token
|
|
|
|
- * @return
|
|
|
|
*/
|
|
*/
|
|
public static int parseTokenUid(String token) {
|
|
public static int parseTokenUid(String token) {
|
|
DecodedJWT jwt = JWT.decode(token);
|
|
DecodedJWT jwt = JWT.decode(token);
|
|
return jwt.getClaim("uid").asInt();
|
|
return jwt.getClaim("uid").asInt();
|
|
}
|
|
}
|
|
|
|
|
|
- /**
|
|
|
|
- * 从token解析出aud信息,用户名
|
|
|
|
- *
|
|
|
|
- * @param token
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- /*public static String parseTokenAud(String token) {
|
|
|
|
- DecodedJWT jwt = JWT.decode(token);
|
|
|
|
- return jwt.getClaim("aud").asString();
|
|
|
|
- }*/
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* 从token解析出过期日期时间
|
|
* 从token解析出过期日期时间
|
|
- *
|
|
|
|
- * @param token
|
|
|
|
- * @return Date
|
|
|
|
*/
|
|
*/
|
|
public static Date paraseExpiresAt(String token) {
|
|
public static Date paraseExpiresAt(String token) {
|
|
DecodedJWT jwt = JWT.decode(token);
|
|
DecodedJWT jwt = JWT.decode(token);
|
|
@@ -146,4 +100,14 @@ public class JwtUtil {
|
|
return EXPIRE_TIME / 1000;
|
|
return EXPIRE_TIME / 1000;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 从token解析出aud信息,用户名
|
|
|
|
+ *
|
|
|
|
+ * @param token
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ /*public static String parseTokenAud(String token) {
|
|
|
|
+ DecodedJWT jwt = JWT.decode(token);
|
|
|
|
+ return jwt.getClaim("aud").asString();
|
|
|
|
+ }*/
|
|
}
|
|
}
|