From ce920867ae3a55d17ce5caf30961b6d51e2a7078 Mon Sep 17 00:00:00 2001
From: k94314517 <8417338+k94314517@user.noreply.gitee.com>
Date: 星期五, 11 七月 2025 09:03:59 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/master'

---
 server/services/src/main/java/com/doumee/config/jwt/JwtTokenUtil.java |  137 +++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 137 insertions(+), 0 deletions(-)

diff --git a/server/services/src/main/java/com/doumee/config/jwt/JwtTokenUtil.java b/server/services/src/main/java/com/doumee/config/jwt/JwtTokenUtil.java
new file mode 100644
index 0000000..f1e4399
--- /dev/null
+++ b/server/services/src/main/java/com/doumee/config/jwt/JwtTokenUtil.java
@@ -0,0 +1,137 @@
+package com.doumee.config.jwt;
+
+import com.alibaba.fastjson.JSONObject;
+import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
+import com.doumee.biz.system.SystemDictDataBiz;
+import com.doumee.core.constants.Constants;
+import com.doumee.core.utils.HttpsUtil;
+import com.doumee.dao.business.model.Member;
+import com.doumee.dao.system.SystemUserMapper;
+import com.doumee.dao.system.model.SystemUser;
+import io.jsonwebtoken.Jwts;
+import io.jsonwebtoken.SignatureAlgorithm;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.Resource;
+import java.io.IOException;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+import java.util.concurrent.TimeUnit;
+
+@Component
+@Slf4j
+public class JwtTokenUtil {
+
+    public static final String HEADER_KEY = "token";
+
+    public static final String MEMBER_ID = "MEMBER_ID";
+
+    public static final String MEMBER_INFO = "MEMBER_INFO";
+
+    @Resource
+    private RedisTemplate<String,Object> redisTemplate;
+
+    @Resource
+    private JwtProperties jwtProperties;
+
+    /**
+     * 鐢熸垚token浠ょ墝
+     *
+     * @param member 浠ょ墝涓惡甯︾殑闄勫姞淇℃伅
+     * @return 浠oken鐗�
+     */
+    public String generateToken(Member member) {
+        if(member == null){
+            return  null;
+        }
+        Map<String,Object> map = new HashMap<>();
+        map.put("id",member.getId());
+        return generateTokenDo(member);
+    }
+
+
+    public Member getUserInfoByToken(String token) {
+        try {
+            Member member = getClaimsFromToken(token);
+            return member;
+        } catch (Exception e) {
+           e.printStackTrace();
+        }
+        return null;
+    }
+
+
+    /**
+     * 鍒锋柊浠ょ墝
+     *
+     * @param token 鍘熶护鐗�
+     * @return 鏂颁护鐗�
+     */
+    public void refreshToken(String token,Member member) {
+        try {
+            if(Objects.nonNull(member)){
+                redisTemplate.opsForValue().set(Constants.REDIS_TOKEN_KEY+token,JSONObject.toJSONString(member),jwtProperties.getExpiration(), TimeUnit.MILLISECONDS);
+            }
+        } catch (Exception e) {
+
+        }
+    }
+
+
+    /**
+     * 閫�鍑虹櫥闄�
+     *
+     * @param token 鍘熶护鐗�
+     * @return 鏂颁护鐗�
+     */
+    public void logoutForH5(String token) {
+        try {
+            //鍒犻櫎鑰佺殑token
+            redisTemplate.delete(Constants.REDIS_TOKEN_KEY+token);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+    /**
+     * 浠巆laims鐢熸垚浠ょ墝,濡傛灉鐪嬩笉鎳傚氨鐪嬭皝璋冪敤瀹�
+     *
+     * @return 浠ょ墝
+     */
+    private String generateTokenDo(Member member) {
+        Map<String, Object> claims = new HashMap<>();
+        claims.put("id",member.getId());
+        Date expirationDate = new Date(System.currentTimeMillis() + jwtProperties.getExpiration());
+        String token = Jwts.builder().setClaims(claims)
+                .setExpiration(expirationDate)
+                .signWith(SignatureAlgorithm.HS512, jwtProperties.getSecret())
+                .compact();
+        redisTemplate.opsForValue().set(Constants.REDIS_TOKEN_KEY+token,JSONObject.toJSONString(member),jwtProperties.getExpiration(), TimeUnit.MILLISECONDS);
+        return token;
+    }
+
+
+    /**
+     * 浠庝护鐗屼腑鑾峰彇鏁版嵁澹版槑,楠岃瘉JWT绛惧悕
+     *
+     * @param token 浠ょ墝
+     * @return 鏁版嵁澹版槑
+     */
+    private Member getClaimsFromToken(String token) {
+        Member claims;
+        try {
+            String userInfo = (String) redisTemplate.opsForValue().get(Constants.REDIS_TOKEN_KEY+token);
+            claims = JSONObject.toJavaObject(JSONObject.parseObject(userInfo),Member.class);
+        } catch (Exception e) {
+            claims = null;
+        }
+        return claims;
+    }
+
+}
\ No newline at end of file

--
Gitblit v1.9.3