nnnn
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
package com.ruoyi.web.controller.cai.admin;
|
||||
|
||||
import cn.dev33.satoken.annotation.SaCheckPermission;
|
||||
import com.ruoyi.cai.util.LoginLogByFileUtil;
|
||||
import com.ruoyi.common.core.controller.BaseController;
|
||||
import com.ruoyi.common.core.domain.R;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 账户明细
|
||||
*
|
||||
* @author 77
|
||||
* @date 2024-01-05
|
||||
*/
|
||||
@Validated
|
||||
@RequiredArgsConstructor
|
||||
@RestController
|
||||
@RequestMapping("/cai/loginLog")
|
||||
public class LoginLogController extends BaseController {
|
||||
|
||||
|
||||
/**
|
||||
* 查询账户明细列表
|
||||
*/
|
||||
@SaCheckPermission("cai:loginLog:list")
|
||||
@GetMapping("/list")
|
||||
public R<List<LoginLogInfo>> list(String mobile) {
|
||||
// List<String> list = LoginLogByFileUtil.getLog(mobile);
|
||||
List<String> list = new ArrayList<>();
|
||||
list.add("2025-12-21 08:31:43 [XNIO-1 task-193] INFO c.ruoyi.framework.aspectj.LogAspect - record logs:ip=172.56.45.250;url=/api/auth/login;method=POST;title=登陆;user-agent=yan yu/1.0.6 (iPhone; iOS 26.2; Scale/3.00);params={\"username\":\"13588246608\"};result=null;exception=您的账号已被封禁;;StartTime:2025-12-21 08:31:43,EndTime:2025-12-21 08:31:43,CostTime:5ms");
|
||||
list.add("2025-12-21 09:14:34 [XNIO-1 task-8] INFO c.ruoyi.framework.aspectj.LogAspect - record logs:ip=172.56.44.24;url=/api/auth/login;method=POST;title=登陆;currentUserId=44554;user-agent=yan yu/1.0.6 (iPhone; iOS 26.2; Scale/3.00);params={\"username\":\"13588246608\"};result=null;exception=您的账号已被封禁;;StartTime:2025-12-21 09:14:34,EndTime:2025-12-21 09:14:34,CostTime:7ms");
|
||||
list.add("2025-12-21 09:55:08 [XNIO-1 task-193] INFO c.ruoyi.framework.aspectj.LogAspect - record logs:ip=172.56.44.156;url=/api/auth/login;method=POST;title=登陆;currentUserId=6501;user-agent=yan yu/1.0.6 (iPhone; iOS 26.2; Scale/3.00);params={\"username\":\"13588246608\"};result={\"code\":200,\"msg\":\"操作成功\",\"data\":{\"token\":\"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dpblR5cGUiOiJsb2dpbiIsImxvZ2luSWQiOiJhcHBfdXNlcjo2NTAxIiwicm5TdHIiOiJYR3pFcngxOWxXcm1MSEZhMGRhV0dDWFNERFhPaDBGeiIsInVzZXJJZCI6NjUwMX0.Coy1DgKMgtyEIpEpLKrvm_3w8SEjeKujfaTKu3l9AyI\",\"userInfo\":{\"userId\":6501,\"inviteId\":4387,\"type\":0,\"usercode\":\"6953\",\"nickname\":\"用户6953\",\"mobile\":\"13588246608\",\"avatar\":\"images/avatar/man.png\",\"avatarState\":0,\"gender\":2,\"birthday\":null,\"age\":18,\"cityId\":0,\"city\":null,\"isAnchor\":0,\"openVideoStatus\":1,\"status\":0,\"finishStatus\":0,\"imToken\":\"716d4d9bbe1b4441b88065b6bc0c543d\",\"userAccount\":{\"userId\":6501,\"coin\":2537,\"incomeCoin\":0},\"userCount\":{\"userId\":6501,\"newFansCount\":266,\"newVisitorCount\":5302,\"fansCount\":36,\"followCount\":24,\"footCount\":6691,\"visitorCount\":5302}}}};;StartTime:2025-12-21 09:55:07,EndTime:2025-12-21 09:55:08,CostTime:148ms");
|
||||
list.add("2025-12-21 10:30:21 [XNIO-1 task-46] INFO c.ruoyi.framework.aspectj.LogAspect - record logs:ip=172.56.45.234;url=/api/auth/login;method=POST;title=登陆;currentUserId=6501;user-agent=yan yu/1.0.6 (iPhone; iOS 26.2; Scale/3.00);params={\"username\":\"13588246608\"};result={\"code\":200,\"msg\":\"操作成功\",\"data\":{\"token\":\"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dpblR5cGUiOiJsb2dpbiIsImxvZ2luSWQiOiJhcHBfdXNlcjo2NTAxIiwicm5TdHIiOiI4U2dKZ3hiWVF0VHpjNmhSRzdXV3NYSGMyaGFQYmJ3eCIsInVzZXJJZCI6NjUwMX0.2KrY8jHwHl67A25JtRCmAldnOvSpcU1mtnYlcUFLhy0\",\"userInfo\":{\"userId\":6501,\"inviteId\":4387,\"type\":0,\"usercode\":\"6953\",\"nickname\":\"用户6953\",\"mobile\":\"13588246608\",\"avatar\":\"images/avatar/man.png\",\"avatarState\":0,\"gender\":2,\"birthday\":null,\"age\":18,\"cityId\":0,\"city\":null,\"isAnchor\":0,\"openVideoStatus\":1,\"status\":0,\"finishStatus\":0,\"imToken\":\"716d4d9bbe1b4441b88065b6bc0c543d\",\"userAccount\":{\"userId\":6501,\"coin\":2517,\"incomeCoin\":0},\"userCount\":{\"userId\":6501,\"newFansCount\":266,\"newVisitorCount\":5302,\"fansCount\":36,\"followCount\":24,\"footCount\":6692,\"visitorCount\":5302}}}};;StartTime:2025-12-21 10:30:21,EndTime:2025-12-21 10:30:21,CostTime:148ms");
|
||||
List<LoginLogInfo> collect = list.stream().map(LoginLogInfo::new).collect(Collectors.toList());
|
||||
return R.ok(collect);
|
||||
}
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
public static class LoginLogInfo {
|
||||
private String info;
|
||||
|
||||
public LoginLogInfo(String info) {
|
||||
this.info = info;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -19,6 +19,11 @@
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-exec</artifactId>
|
||||
<version>1.3</version> <!-- 最新稳定版 -->
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.ruoyi</groupId>
|
||||
<artifactId>ruoyi-framework</artifactId>
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
package com.ruoyi.cai.util;
|
||||
|
||||
import com.ruoyi.common.exception.base.BaseException;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.exec.*;
|
||||
import org.apache.commons.exec.environment.EnvironmentUtils;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Slf4j
|
||||
public class LoginLogByFileUtil {
|
||||
|
||||
public static List<String> getLog(String mobile){
|
||||
// 1. 定义命令(带管道的Linux命令,需用/bin/sh -c包裹)
|
||||
String commandFormat = "tac /home/server/api/logs/sys-console.log | grep \"%s\" | grep -m 10 \"auth/login\"";
|
||||
String command = String.format(commandFormat, mobile);
|
||||
CommandLine cmdLine = CommandLine.parse("/bin/sh -c " + command); // 自动处理参数分隔
|
||||
|
||||
// 2. 配置执行器(设置超时、流处理)
|
||||
DefaultExecutor executor = new DefaultExecutor();
|
||||
executor.setExitValue(0); // 期望的退出码(0表示成功)
|
||||
|
||||
// 设置进程超时(5秒,适配你的快速执行场景)
|
||||
ExecuteWatchdog watchdog = new ExecuteWatchdog(5000); // 5000ms = 5s
|
||||
executor.setWatchdog(watchdog);
|
||||
|
||||
// 3. 处理输出(ByteArrayOutputStream存储10行结果,或用PumpStreamHandler实时处理)
|
||||
// 场景1:存储结果到内存(仅10行,内存无压力)
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream, outputStream); // 合并标准输出/错误流
|
||||
executor.setStreamHandler(streamHandler);
|
||||
|
||||
// 场景2:实时处理每行输出(推荐,避免存储)
|
||||
// PumpStreamHandler streamHandler = new PumpStreamHandler(new LogOutputStream() {
|
||||
// @Override
|
||||
// protected void processLine(String line, int level) {
|
||||
// // 实时处理每行数据(如输出到前端、写入日志)
|
||||
// System.out.println(line);
|
||||
// }
|
||||
// });
|
||||
// executor.setStreamHandler(streamHandler);
|
||||
|
||||
try {
|
||||
// 4. 执行命令(环境变量使用系统默认,可自定义)
|
||||
Map<String, String> env = EnvironmentUtils.getProcEnvironment();
|
||||
executor.execute(cmdLine, env);
|
||||
|
||||
// 5. 解析结果(场景1的方式)
|
||||
String result = outputStream.toString(StandardCharsets.UTF_8.name());
|
||||
String[] split = result.split(System.lineSeparator());
|
||||
List<String> lines = Arrays.stream(split).collect(Collectors.toList());
|
||||
System.out.println("匹配的行数:" + lines.size());
|
||||
lines.forEach(System.out::println);
|
||||
|
||||
} catch (ExecuteException e) {
|
||||
if (watchdog.killedProcess()) {
|
||||
log.error("命令执行超时,已终止进程");
|
||||
} else {
|
||||
log.error("命令执行失败,退出码:" + e.getExitValue());
|
||||
}
|
||||
log.error(e.getMessage(),e);
|
||||
throw new BaseException("命令执行失败");
|
||||
} catch (IOException e) {
|
||||
log.error("IO异常:" + e.getMessage());
|
||||
log.error(e.getMessage(),e);
|
||||
throw new BaseException("IO异常");
|
||||
}
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user