|
@@ -0,0 +1,88 @@
|
|
|
|
|
+package com.roma.romaapi.utils;
|
|
|
|
|
+
|
|
|
|
|
+import com.auth0.jwt.JWT;
|
|
|
|
|
+import com.auth0.jwt.JWTCreator;
|
|
|
|
|
+import com.auth0.jwt.JWTVerifier;
|
|
|
|
|
+import com.auth0.jwt.algorithms.Algorithm;
|
|
|
|
|
+import com.auth0.jwt.interfaces.DecodedJWT;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
+import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
|
+
|
|
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
|
|
+import java.util.Calendar;
|
|
|
|
|
+import java.util.Date;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+
|
|
|
|
|
+/***
|
|
|
|
|
+ * Author: YL.Lou
|
|
|
|
|
+ * Class: JWTUtil
|
|
|
|
|
+ * Project: washes_base_backstage
|
|
|
|
|
+ * Introduce: JWT的验证工具类 被生成Token的方法Service调用 也会被 拦截器调用 验签
|
|
|
|
|
+ * DateTime: 2022-06-29 19:23
|
|
|
|
|
+ ***/
|
|
|
|
|
+
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+@Component
|
|
|
|
|
+public class JWTUtil {
|
|
|
|
|
+ // 为Redis存储准备的Key前缀 后边跟的是用户的 ID 例如:JWT_SIGN_1 查询到的是 MD5 之后的 用户密码 信息
|
|
|
|
|
+ public static final String SIGN = "JWT_SIGN_";
|
|
|
|
|
+
|
|
|
|
|
+ // 加盐
|
|
|
|
|
+ private static final String SECRET = "lou123321!!!";
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ StringRedisTemplate stringRedisTemplate;
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 获取token
|
|
|
|
|
+ * @return token
|
|
|
|
|
+ */
|
|
|
|
|
+ public String getToken(Map userMapInfo) {
|
|
|
|
|
+
|
|
|
|
|
+ Calendar instance = Calendar.getInstance();
|
|
|
|
|
+
|
|
|
|
|
+ //默认令牌过期时间30天
|
|
|
|
|
+ SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
|
|
+
|
|
|
|
|
+ instance.add(Calendar.DATE, 7);
|
|
|
|
|
+
|
|
|
|
|
+ JWTCreator.Builder builder = JWT.create();
|
|
|
|
|
+
|
|
|
|
|
+ builder.withClaim("userId", String.valueOf(userMapInfo.get("id")))
|
|
|
|
|
+ .withClaim("userLoginTime", String.valueOf(userMapInfo.get("loginTime")))
|
|
|
|
|
+ .withClaim("userName", String.valueOf(userMapInfo.get("name")))
|
|
|
|
|
+ .withClaim("expTime", simpleDateFormat.format(new Date(instance.getTime().getTime())));
|
|
|
|
|
+
|
|
|
|
|
+ // 将 用户ID + 用户密码 用MD5 混淆 再加盐 获取的字符串 用来生成签名
|
|
|
|
|
+ return builder.withExpiresAt(instance.getTime())
|
|
|
|
|
+ .sign(Algorithm.HMAC256(String.valueOf(userMapInfo.get("id")) + SECRET));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 验证token合法性 成功返回token
|
|
|
|
|
+ */
|
|
|
|
|
+ public DecodedJWT verify(String token, Long uId) throws Exception {
|
|
|
|
|
+
|
|
|
|
|
+ // 从Redis中获取用户ID + 密码 并被MD5 混淆后的字符串
|
|
|
|
|
+ String strSign = stringRedisTemplate.opsForValue().get(SIGN + uId);
|
|
|
|
|
+
|
|
|
|
|
+ if(null == strSign){
|
|
|
|
|
+ throw new Exception("Original Token 无效或已过期");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if(token==null || token.equals("")){
|
|
|
|
|
+ throw new Exception("token不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ JWTVerifier build = JWT.require(Algorithm.HMAC256(strSign + SECRET)).build();
|
|
|
|
|
+
|
|
|
|
|
+ return build.verify(token);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /* public static void main(String[] args) {
|
|
|
|
|
+ DecodedJWT verify = verify("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2MTcxMDg1MDAsInVzZXJuYW1lIjoiYWRtaW4ifQ.geBEtpluViRUg66_P7ZisN3I_d4e32Wms8mFoBYM5f0");
|
|
|
|
|
+ System.out.println(verify.getClaim("password").asString());
|
|
|
|
|
+ }*/
|
|
|
|
|
+}
|