From 19321e1348baefa2a9f5211c42f8b797c0fcccd1 Mon Sep 17 00:00:00 2001
From: rk <94314517@qq.com>
Date: 星期一, 27 十月 2025 18:11:56 +0800
Subject: [PATCH] 登录验证

---
 server/system_service/src/main/java/com/doumee/config/jwt/JwtTokenUtil.java |  121 ++++++++++++++++++++++++++++++++++++++--
 1 files changed, 114 insertions(+), 7 deletions(-)

diff --git a/server/system_service/src/main/java/com/doumee/config/jwt/JwtTokenUtil.java b/server/system_service/src/main/java/com/doumee/config/jwt/JwtTokenUtil.java
index f88920c..ec844c8 100644
--- a/server/system_service/src/main/java/com/doumee/config/jwt/JwtTokenUtil.java
+++ b/server/system_service/src/main/java/com/doumee/config/jwt/JwtTokenUtil.java
@@ -1,29 +1,46 @@
 package com.doumee.config.jwt;
 
+import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
-import com.doumee.core.model.LoginUserInfo;
+import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
+import com.doumee.biz.system.SystemDictDataBiz;
+import com.doumee.dao.system.model.SystemDictData;
+import com.doumee.service.business.third.model.LoginUserInfo;
 import com.doumee.core.utils.Constants;
+import com.doumee.core.utils.HttpsUtil;
+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.apache.http.HttpEntity;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.impl.client.HttpClientBuilder;
+import org.apache.http.util.EntityUtils;
 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.util.Date;
-import java.util.HashMap;
-import java.util.Map;
+import java.io.IOException;
+import java.util.*;
 import java.util.concurrent.TimeUnit;
 
 @Component
+@Slf4j
 public class JwtTokenUtil {
 
     @Autowired
     private RedisTemplate<String,Object> redisTemplate;
     @Resource
     private JwtProperties jwtProperties;
-
-
+    @Autowired
+    private SystemDictDataBiz systemDictDataBiz ;
+    @Autowired
+    private SystemUserMapper systemUserMapper;
     /**
      * 鐢熸垚token浠ょ墝
      *
@@ -113,11 +130,61 @@
      */
     public void logout(String token) {
         try {
-            redisTemplate.delete(Constants.REDIS_TOKEN_KEY+token);//鍒犻櫎鑰佺殑token
+            //鐧诲嚭娴峰悍绯荤粺鏁版嵁
+            LoginUserInfo loginUserInfo = this.getUserInfoByToken(token);
+            String url = systemDictDataBiz.queryByCode(Constants.HK_PARAM,Constants.HK_HTTPS).getCode() +
+                    systemDictDataBiz.queryByCode(Constants.HK_PARAM,Constants.HK_HOST).getCode() +
+                    systemDictDataBiz.queryByCode(Constants.HK_PARAM,Constants.LOGIN_OUT_URL).getCode();
+            if(StringUtils.isNotBlank(loginUserInfo.getHkMenuToken())){
+                log.info("璋冭捣娴峰悍閫�鍑虹櫥褰�=======================>"+url+"?token="+loginUserInfo.getHkMenuToken());
+//                this.hkLoginOut(url+"?token="+loginUserInfo.getHkMenuToken());
+                HttpsUtil.get(url+"?token="+loginUserInfo.getHkMenuToken(),true);
+            }
+            //鍒犻櫎鑰佺殑token
+            redisTemplate.delete(Constants.REDIS_TOKEN_KEY+token);
         } catch (Exception e) {
             e.printStackTrace();
         }
     }
+
+    public void logoutForH5(String token) {
+        try {
+            //鐧诲嚭娴峰悍绯荤粺鏁版嵁
+            LoginUserInfo loginUserInfo = this.getUserInfoByToken(token);
+            //鍒犻櫎鑰佺殑token
+            redisTemplate.delete(Constants.REDIS_TOKEN_KEY+token);
+            systemUserMapper.update(null,new UpdateWrapper<SystemUser>().lambda().set(SystemUser::getOpenid,null)
+                    .eq(SystemUser::getId,loginUserInfo.getId()));
+
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+    public void hkLoginOut(String url){
+        try {
+            // 鍒涘缓HttpClient瀵硅薄
+            HttpClient httpClient = HttpClientBuilder.create().build();
+            // 鍒涘缓HttpGet瀵硅薄锛屾寚瀹氳璁块棶鐨刄RL鍦板潃
+            HttpGet httpGet = new HttpGet(url);
+            // 鍙戦�丟ET璇锋眰锛岃幏鍙栧搷搴�
+            HttpResponse response = httpClient.execute(httpGet);
+            // 鑾峰彇鍝嶅簲鐘舵�佺爜
+            int statusCode = response.getStatusLine().getStatusCode();
+            // 鍒ゆ柇璇锋眰鏄惁鎴愬姛
+            if (statusCode == 200) {
+                // 鑾峰彇鍝嶅簲鍐呭
+                HttpEntity entity = response.getEntity();
+                String responseContent = EntityUtils.toString(entity, "UTF-8");
+                System.out.println(responseContent);
+                log.info("璋冭捣娴峰悍閫�鍑虹櫥褰曡繑鍥炰俊鎭�=======================>"+responseContent);
+            } else {
+                System.out.println("璇锋眰澶辫触锛屽搷搴旂爜锛�" + statusCode);
+            }
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+
 
     /**
      * 楠岃瘉浠ょ墝
@@ -147,6 +214,34 @@
                 .signWith(SignatureAlgorithm.HS512, jwtProperties.getSecret())
                 .compact();
         redisTemplate.opsForValue().set(Constants.REDIS_TOKEN_KEY+token,JSONObject.toJSONString(userInfo),jwtProperties.getExpiration(), TimeUnit.MILLISECONDS);
+
+        String userTokenJsonStr = (String) redisTemplate.opsForValue().get(Constants.REDIS_USER_KEY+userInfo.getId());
+        if(StringUtils.isEmpty(userTokenJsonStr)){
+            redisTemplate.opsForValue().set(Constants.REDIS_USER_KEY+userInfo.getId(),Constants.REDIS_TOKEN_KEY+token);
+        }else{
+            List<String> list  = Arrays.asList(userTokenJsonStr.split(","));
+            SystemDictData jointAccount = systemDictDataBiz.queryByCode(Constants.SYSTEM,Constants.OPEN_JOINT_ACCOUNT);
+            //鍏抽棴鍏辩敤璐︽埛 闇�瑕佹竻绌哄叾浠杢oken淇℃伅
+            if(Objects.nonNull(jointAccount)&&Constants.equalsInteger(Integer.valueOf(jointAccount.getCode()),Constants.ONE)){
+                for (String s:list) {
+                    redisTemplate.delete(s);
+                }
+                redisTemplate.delete(Constants.REDIS_USER_KEY+userInfo.getId());
+                redisTemplate.opsForValue().set(Constants.REDIS_USER_KEY+userInfo.getId(),Constants.REDIS_TOKEN_KEY+token);
+            }else{
+                Boolean isHave = false;
+                for (String s:list) {
+                    if(s.equals(Constants.REDIS_TOKEN_KEY+token)){
+                        isHave = true;
+                        break;
+                    }
+                }
+                if(!isHave){
+                    redisTemplate.opsForValue().set(Constants.REDIS_USER_KEY+userInfo.getId(),userTokenJsonStr + "," + Constants.REDIS_TOKEN_KEY+token);
+                }
+            }
+        }
+
         return token;
     }
 
@@ -162,9 +257,21 @@
         try {
             String userInfo = (String) redisTemplate.opsForValue().get(Constants.REDIS_TOKEN_KEY+token);
             claims = JSONObject.toJavaObject(JSONObject.parseObject(userInfo),LoginUserInfo.class);
+            refreshTokenTime(token);
         } catch (Exception e) {
             claims = null;
         }
         return claims;
     }
+
+    /**
+     * 鍚戝悗寤朵几鏈夋晥鏈熶繚鎸佷細璇濈户缁�
+     * @param token
+     */
+    public void refreshTokenTime(String token ) {
+        log.error("===============寮�濮嬪埛鏂扮櫥褰晅oken"+token);
+        redisTemplate.expire(Constants.REDIS_TOKEN_KEY+token,jwtProperties.getExpiration(), TimeUnit.MILLISECONDS);
+        log.error("===============缁撴潫鍒锋柊鐧诲綍token"+token);
+//        redisTemplate.opsForValue().set(Constants.REDIS_TOKEN_KEY+token,usrerInfo,jwtProperties.getExpiration(), TimeUnit.MILLISECONDS);
+    }
 }
\ No newline at end of file

--
Gitblit v1.9.3