Browse Source

上传文件

陈士柏 2 years ago
parent
commit
68e4874def
1 changed files with 111 additions and 0 deletions
  1. 111 0
      mhotel/src/com/happy/common/JwtUtil.java

+ 111 - 0
mhotel/src/com/happy/common/JwtUtil.java

@@ -0,0 +1,111 @@
+package com.happy.common;
+
+
+import com.auth0.jwt.JWT;
+import com.auth0.jwt.JWTVerifier;
+import com.auth0.jwt.algorithms.Algorithm;
+import com.auth0.jwt.exceptions.JWTDecodeException;
+import com.auth0.jwt.interfaces.DecodedJWT;
+import org.springframework.stereotype.Component;
+
+import java.io.UnsupportedEncodingException;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Java web token 工具类
+ *
+ * @author qiaokun
+ * @date 2018/08/10
+ */
+@Component
+public class JwtUtil {
+    /**
+     * 过期时间15分钟,毫秒
+     * TODO 正式运行时修改为15分钟
+     */
+    private static final long EXPIRE_TIME = 150 * 60 * 1000;
+    /**
+     * token私钥
+     */
+    private static final String TOKEN_SECRET = "f26e587c28064d0e855e72c0a6a0e618";
+
+    /**
+     * 校验token是否正确
+     *
+     * @param token 密钥
+     * @return 是否正确
+     */
+    public static boolean verify(String token) {
+        try {
+            Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
+            JWTVerifier verifier = JWT.require(algorithm)
+                    .build();
+            DecodedJWT jwt = verifier.verify(token);
+            return true;
+        } catch (Exception exception) {
+            return false;
+        }
+    }
+
+    /**
+     * 获得token中的信息无需secret解密也能获得
+     *
+     * @return token中包含的用户名
+     */
+    public static String getNum(String token) {
+        try {
+            DecodedJWT jwt = JWT.decode(token);
+            return jwt.getClaim("num").asString();
+        } catch (JWTDecodeException e) {
+            return null;
+        }
+    }
+
+    /**
+     * 获取登陆用户ID
+     * @param
+     * @return
+     */
+    public static String getPwd(String token) {
+        try {
+            DecodedJWT jwt = JWT.decode(token);
+            return jwt.getClaim("pwd").asString();
+        } catch (JWTDecodeException e) {
+            return null;
+        }
+    }
+
+    /**
+     * 生成签名,15min后过期
+     *
+     * @param
+     * @return 加密的token
+     */
+    public static String sign(String number,String pwd) {
+        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");
+            // 附带username,userId信息,生成签名
+            return JWT.create()
+                    .withHeader(header)
+                    .withClaim("num", number)
+                    .withClaim("pwd",pwd)
+                    .withExpiresAt(date)
+                    .sign(algorithm);
+        } catch (UnsupportedEncodingException e) {
+            return null;
+        }
+
+    }
+
+}
+