浏览代码

日志关联功能 token变更为为登录获取

jingyuanchao 1 年之前
父节点
当前提交
10cc4b856d

+ 5 - 0
project_data/sql/0.1.1/升级文档.txt

@@ -123,4 +123,9 @@ security:
 
 ###########################     针对服务器shell脚本的修改,若是先在windows上修改完成后复制到服务器上的,避免使用系统自带的文本工具,避免产生因编码差异导致不可预期的错误     ###########################
 
+8.nacos上对system配置文件增加看门猫账号密码:
+例:
+monitor:
+  userName: admin
+  password: Admin123
 

+ 9 - 4
soc-common/soc-common-redis/src/main/java/com/xunmei/common/redis/utils/RedisUtils.java

@@ -1,16 +1,16 @@
 package com.xunmei.common.redis.utils;
 
-import com.alibaba.fastjson2.JSON;
 import com.xunmei.common.core.utils.SpringUtils;
 import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
 import org.redisson.api.*;
-import org.springframework.data.redis.core.RedisTemplate;
 
 import java.text.MessageFormat;
 import java.time.Duration;
-import java.util.*;
-import java.util.concurrent.TimeUnit;
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
 import java.util.function.Consumer;
 import java.util.stream.Collectors;
 import java.util.stream.Stream;
@@ -93,6 +93,11 @@ public class RedisUtils {
         setCacheObject(key, value, false);
     }
 
+    public static <T> void setCacheObject(final String key, final T value, long timeout) {
+        setCacheObject(key, value, false);
+        expire(key, timeout);
+    }
+
     /**
      * 缓存基本的对象,保留当前对象 TTL 有效期
      *

+ 2 - 0
soc-modules/soc-modules-system/src/main/java/com/xunmei/system/SocSystemApplication.java

@@ -5,6 +5,7 @@ import com.xunmei.common.security.annotation.EnableSocFeignClients;
 import com.xunmei.common.swagger.annotation.EnableCustomSwagger2;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
 import org.springframework.cloud.context.config.annotation.RefreshScope;
 import org.springframework.context.annotation.Bean;
 import org.springframework.scheduling.annotation.EnableAsync;
@@ -17,6 +18,7 @@ import org.springframework.web.client.RestTemplate;
  */
 @RefreshScope
 @EnableAsync
+@EnableConfigurationProperties
 @EnableCustomConfig
 @EnableCustomSwagger2
 @EnableSocFeignClients

+ 33 - 0
soc-modules/soc-modules-system/src/main/java/com/xunmei/system/config/MonitorCatConfig.java

@@ -0,0 +1,33 @@
+package com.xunmei.system.config;
+
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.context.annotation.Configuration;
+
+/**
+ * @author jingyuanchao
+ * @date 2024/10/17 9:25
+ */
+@Configuration
+@ConfigurationProperties(prefix = "monitor")
+public class MonitorCatConfig {
+
+    private String userName;
+
+    private String password;
+
+    public String getUserName() {
+        return userName;
+    }
+
+    public void setUserName(String userName) {
+        this.userName = userName;
+    }
+
+    public String getPassword() {
+        return password;
+    }
+
+    public void setPassword(String password) {
+        this.password = password;
+    }
+}

+ 1 - 1
soc-modules/soc-modules-system/src/main/java/com/xunmei/system/controller/SysServerController.java

@@ -59,7 +59,7 @@ public class SysServerController extends BaseController {
 
     @ApiOperation(value = "删除SysServer")
     //@RequiresPermissions("system:server:remove")
-    @Log(title = "服务器维护", businessType = BusinessType.DELETE)
+    @Log(title = "服务器删除", businessType = BusinessType.DELETE)
     @DeleteMapping("/{ids}")
     public AjaxResult remove(@PathVariable Long[] ids) {
         return toAjax(sysServerService.deleteSysServerByIds(ids));

+ 39 - 7
soc-modules/soc-modules-system/src/main/java/com/xunmei/system/service/impl/SysServerServiceImpl.java

@@ -1,6 +1,7 @@
 package com.xunmei.system.service.impl;
 
 import cn.hutool.core.util.ObjectUtil;
+import cn.hutool.crypto.digest.DigestUtil;
 import cn.hutool.http.HttpException;
 import cn.hutool.http.HttpRequest;
 import cn.hutool.http.HttpResponse;
@@ -17,8 +18,10 @@ import com.xunmei.common.core.thread.ThreadPoolConfig;
 import com.xunmei.common.core.util.BeanHelper;
 import com.xunmei.common.core.utils.DateUtils;
 import com.xunmei.common.core.web.page.TableDataInfo;
+import com.xunmei.common.redis.utils.RedisUtils;
 import com.xunmei.common.security.utils.SecurityUtils;
 import com.xunmei.system.api.RemoteFileService;
+import com.xunmei.system.config.MonitorCatConfig;
 import com.xunmei.system.domain.SysServer;
 import com.xunmei.system.dto.server.SysServerEditDto;
 import com.xunmei.system.dto.server.SysServerPageDto;
@@ -29,6 +32,7 @@ import com.xunmei.system.vo.server.CheckListVo;
 import com.xunmei.system.vo.server.FileInfoVo;
 import com.xunmei.system.vo.server.SysServerPageVo;
 import feign.Response;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Qualifier;
@@ -36,12 +40,10 @@ import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
+import javax.annotation.Resource;
 import javax.servlet.http.HttpServletResponse;
 import java.io.*;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
-import java.util.UUID;
+import java.util.*;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.TimeUnit;
@@ -54,6 +56,7 @@ import java.util.stream.Collectors;
  * @author xunmei
  * @date 2024-09-29
  */
+@Slf4j
 @Service
 public class SysServerServiceImpl extends ServiceImpl<SysServerMapper, SysServer> implements ISysServerService {
 
@@ -64,6 +67,8 @@ public class SysServerServiceImpl extends ServiceImpl<SysServerMapper, SysServer
     @Autowired
     @Qualifier(ThreadPoolConfig.SOC_EXECUTOR)
     private ThreadPoolTaskExecutor threadPoolTaskExecutor;
+    @Resource
+    private MonitorCatConfig monitorCatConfig;
 
     @Override
     public TableDataInfo<SysServerPageVo> selectPage(SysServerPageDto req) {
@@ -107,17 +112,43 @@ public class SysServerServiceImpl extends ServiceImpl<SysServerMapper, SysServer
             String baseUrl = String.format("http://%s:%d", server.getServerIp(), server.getServerPort());
             String getServicesApi = "/result/list?size=10000&current=1";
             HttpResponse response = HttpUtil.createPost(baseUrl + getServicesApi)
-                    .header("token", server.getServerToken())
+                    .header("token", getToken(server))
                     .execute();
             String serverJson = response.body();
             JSONObject parse = JSONObject.parseObject(serverJson);
             return parse.getObject("rows", new TypeReference<List<CheckListVo>>() {
             });
+
         } catch (HttpException e) {
             return new ArrayList<>();
         }
     }
 
+    private String getToken(SysServer server) {
+        final String tokenCache = RedisUtils.getCacheObject("MonitorCatToken:" + server.getServerIp());
+        if (ObjectUtil.isNotEmpty(tokenCache)) {
+            return tokenCache;
+        }
+        String baseUrl = String.format("http://%s:%d", server.getServerIp(), server.getServerPort());
+        String loginUrl = "/login/login";
+        final HashMap<String, Object> hashMap = new HashMap<>();
+        hashMap.put("userName", monitorCatConfig.getUserName());
+        hashMap.put("userPwd", DigestUtil.sha1Hex(monitorCatConfig.getPassword()));
+        Object post = new Object();
+        try {
+            post = HttpUtil.get(baseUrl + loginUrl, hashMap, 3000);
+            final Map map = JSONObject.parseObject(post.toString(), Map.class);
+            final String token = (String) map.get("token");
+            if (ObjectUtil.isNotEmpty(token)) {
+                RedisUtils.setCacheObject("MonitorCatToken:" + server.getServerIp(), token, 1000 * 60 * 60 * 24);
+            }
+            return token;
+        } catch (Exception e) {
+            log.error("获取看门猫token失败, ip:{},{}", server.getServerIp(), post.toString());
+            throw new RuntimeException(e);
+        }
+    }
+
     @Override
     public Object getLogList(Long serverId, Long checkId) {
         SysServer server = this.selectSysServerById(serverId);
@@ -126,7 +157,7 @@ public class SysServerServiceImpl extends ServiceImpl<SysServerMapper, SysServer
         String getLogs = "/result/fileList?checkId=" + checkId;
         //获取服务器列表
         HttpResponse response = HttpUtil.createPost(baseUrl + getLogs)
-                .header("token", server.getServerToken())
+                .header("token", getToken(server))
                 .execute();
         String serverJson = response.body();
         List<FileInfoVo> checks = JSONArray.parseArray(serverJson, FileInfoVo.class);
@@ -210,7 +241,7 @@ public class SysServerServiceImpl extends ServiceImpl<SysServerMapper, SysServer
     public void downloadLogData(TransferLogDto req, HttpServletResponse response) {
         final SysServer sysServer = getById(req.getServerId());
         final String serverIp = sysServer.getServerIp();
-        FileDownloadDto dto=new FileDownloadDto();
+        FileDownloadDto dto = new FileDownloadDto();
         dto.setServerName(serverIp);
         dto.setFilePath(req.getParentPath().split("logs")[1]);
         dto.setFileName(req.getPath());
@@ -241,6 +272,7 @@ public class SysServerServiceImpl extends ServiceImpl<SysServerMapper, SysServer
             throw new RuntimeException(e);
         }
     }
+
     /**
      * 查询服务器维护
      *