|
|
@@ -24,14 +24,16 @@ public class JWTUtil {
|
|
|
/**
|
|
|
* token过期时间,7天
|
|
|
*/
|
|
|
-// private static final long EXPIRED = 1000 * 60 * 60 * 24 * 7;
|
|
|
- private static final long EXPIRED = 1000 * 60 * 60 * 24;
|
|
|
+ //要加l long类型才可以
|
|
|
+// private static final long EXPIRED = 7 * 24l * 60 * 60 * 1000;
|
|
|
+ private static final long EXPIRED = 7 * 24l * 60 * 60 * 1000;
|
|
|
|
|
|
/**
|
|
|
* 生成token 有过期时间
|
|
|
+ *
|
|
|
* @return 返回token
|
|
|
*/
|
|
|
- public static TokenDateVo getToken(String studentCard,Integer userId,Long expired){
|
|
|
+ public static TokenDateVo getToken(String studentCard, Integer userId, Long expired) {
|
|
|
|
|
|
TokenDateVo result = new TokenDateVo();
|
|
|
// 签发时间
|
|
|
@@ -45,13 +47,13 @@ public class JWTUtil {
|
|
|
Map<String, Object> map = new HashMap<>();
|
|
|
map.put("alg", "HMAC256");
|
|
|
map.put("typ", "JWT");
|
|
|
- Date expireTime = new Date(CommonUtil.getCurrentTimestamp() + (expired == null ? EXPIRED : expired));
|
|
|
+ Date expireTime = new Date(CommonUtil.getCurrentTimestamp() + (expired == null ? EXPIRED : expired));
|
|
|
String token = JWT.create()
|
|
|
.withHeader(map) // header
|
|
|
// .withClaim("identityType", ms.getIdentityType()) // 认证类型
|
|
|
// .withClaim("identifier", ms.getIdentifier()) // 登录标识
|
|
|
- .withClaim("userId",String.valueOf( userId))
|
|
|
- .withClaim("studentCard",studentCard)
|
|
|
+ .withClaim("userId", String.valueOf(userId))
|
|
|
+ .withClaim("studentCard", studentCard)
|
|
|
.withExpiresAt(expireTime) // 设置过期时间。过期时间要大于签发时间
|
|
|
.withIssuedAt(iatDate) // 设置签发时间
|
|
|
.sign(Algorithm.HMAC256(SIGNATURE)); // 加密
|
|
|
@@ -80,28 +82,28 @@ public class JWTUtil {
|
|
|
|
|
|
/**
|
|
|
* 验证token
|
|
|
+ *
|
|
|
* @param token
|
|
|
*/
|
|
|
- public static void verify(String token){
|
|
|
+ public static void verify(String token) {
|
|
|
JWT.require(Algorithm.HMAC256(SIGNATURE)).build().verify(token);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取token中payload
|
|
|
+ *
|
|
|
* @param token
|
|
|
* @return
|
|
|
*/
|
|
|
- public static DecodedJWT getToken(String token){
|
|
|
+ public static DecodedJWT getToken(String token) {
|
|
|
return JWT.require(Algorithm.HMAC256(SIGNATURE)).build().verify(token);
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
/*
|
|
|
* 解密Token
|
|
|
* */
|
|
|
- public static Map<String, Claim> verifyToken(String token) {
|
|
|
-
|
|
|
+ public static Map<String, Claim> verifyToken(String token) {
|
|
|
|
|
|
|
|
|
JWTVerifier verifier = JWT.require(Algorithm.HMAC256(SIGNATURE)).build();
|
|
|
@@ -115,7 +117,7 @@ public class JWTUtil {
|
|
|
} catch (Exception e) {
|
|
|
//throw new RuntimeException("登录凭证已过期,请重新登录");
|
|
|
Map<String, Claim> result = new HashMap<>();
|
|
|
- result.put("登录失效",null);
|
|
|
+ result.put("登录失效", null);
|
|
|
return result;
|
|
|
}
|
|
|
|