Merge pull request #3669 from dataease/pr@dev@feat_log_ip

feat(日志管理): 客户端IP地址
This commit is contained in:
xuwei-fit2cloud 2022-11-08 10:41:28 +08:00 committed by GitHub
commit 5d3a4a1828
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 77 additions and 14 deletions

View File

@ -0,0 +1,45 @@
package io.dataease.commons.utils;
import org.apache.commons.lang3.StringUtils;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
public class IPUtils {
private static final String HEAD_KEYS = "x-forwarded-for, Proxy-Client-IP, WL-Proxy-Client-IP";
private static final String UNKNOWN = "unknown";
private static final String LOCAL_IP_KEY = "0:0:0:0:0:0:0:1";
private static final String LOCAL_IP_VAL = "127.0.0.1";
public static String get() {
String ipStr = null;
boolean isProxy = false;
HttpServletRequest request = null;
try {
request = ServletUtils.request();
} catch (Exception e) {
LogUtil.error(e.getMessage(), e);
return null;
}
String[] keyArr = HEAD_KEYS.split(",");
for (String key : keyArr) {
String header = request.getHeader(key.trim());
if (StringUtils.isNotBlank(header) && !StringUtils.equalsIgnoreCase(UNKNOWN, header)) {
ipStr = header;
isProxy = true;
break;
}
}
if (!isProxy) {
ipStr = request.getRemoteAddr();
}
ipStr = Arrays.stream(ipStr.split(",")).filter(item -> StringUtils.isNotBlank(item) && !StringUtils.equalsIgnoreCase(UNKNOWN, item.trim())).findFirst().orElse(ipStr);
return StringUtils.equals(LOCAL_IP_KEY, ipStr) ? LOCAL_IP_VAL : ipStr;
}
}

View File

@ -22,4 +22,7 @@ public class SysLogGridDTO implements Serializable {
@ApiModelProperty("操作时间")
private Long time;
@ApiModelProperty("IP地址")
private String ip;
}

View File

@ -9,6 +9,7 @@ import io.dataease.commons.constants.ParamConstants;
import io.dataease.commons.constants.SysLogConstants;
import io.dataease.commons.utils.AuthUtils;
import io.dataease.commons.utils.BeanUtils;
import io.dataease.commons.utils.IPUtils;
import io.dataease.commons.utils.ServletUtils;
import io.dataease.controller.sys.base.ConditionEntity;
import io.dataease.controller.sys.request.KeyGridRequest;
@ -28,6 +29,7 @@ import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@ -308,6 +310,7 @@ public class LogService {
sysLogGridDTO.setTime(vo.getTime());
sysLogGridDTO.setUser(vo.getNickName());
sysLogGridDTO.setDetail(logManager.detailInfo(vo));
sysLogGridDTO.setIp(vo.getIp());
return sysLogGridDTO;
}
@ -330,12 +333,14 @@ public class LogService {
sysLogWithBLOBs.setLoginName(sysLogDTO.getSourceName());
sysLogWithBLOBs.setNickName(sysLogDTO.getSourceName());
}
sysLogWithBLOBs.setIp(IPUtils.get());
sysLogMapper.insert(sysLogWithBLOBs);
}
public void exportExcel(KeyGridRequest request) throws Exception {
request = logRetentionProxy(request);
request = detailRequest(request);
String keyWord = request.getKeyWord();
List<String> ids = null;
@ -355,24 +360,25 @@ public class LogService {
List<String[]> details = lists.stream().map(item -> {
String operateTypeName = SysLogConstants.operateTypeName(item.getOperateType());
String sourceTypeName = SysLogConstants.sourceTypeName(item.getSourceType());
String[] row = new String[4];
String[] row = new String[5];
row[0] = Translator.get(operateTypeName) + " " + Translator.get(sourceTypeName);
row[1] = logManager.detailInfo(item);
row[2] = item.getNickName();
row[3] = DateUtil.formatDateTime(new Date(item.getTime()));
row[3] = item.getIp();
row[4] = DateUtil.formatDateTime(new Date(item.getTime()));
return row;
}).collect(Collectors.toList());
String[] headArr = {"操作类型", "详情", "用户", "时间"};
String[] headArr = {"操作类型", "详情", "用户", "IP地址", "时间"};
details.add(0, headArr);
HSSFWorkbook wb = new HSSFWorkbook();
XSSFWorkbook wb = new XSSFWorkbook();
//明细sheet
HSSFSheet detailsSheet = wb.createSheet("数据");
XSSFSheet detailsSheet = wb.createSheet("数据");
//给单元格设置样式
CellStyle cellStyle = wb.createCellStyle();
Font font = wb.createFont();
XSSFCellStyle cellStyle = wb.createCellStyle();
XSSFFont font = wb.createFont();
//设置字体大小
font.setFontHeightInPoints((short) 12);
//设置字体加粗
@ -386,11 +392,11 @@ public class LogService {
if (CollectionUtils.isNotEmpty(details)) {
for (int i = 0; i < details.size(); i++) {
HSSFRow row = detailsSheet.createRow(i);
XSSFRow row = detailsSheet.createRow(i);
String[] rowData = details.get(i);
if (rowData != null) {
for (int j = 0; j < rowData.length; j++) {
HSSFCell cell = row.createCell(j);
XSSFCell cell = row.createCell(j);
cell.setCellValue(rowData[j]);
if (i == 0) {// 头部
cell.setCellStyle(cellStyle);
@ -406,7 +412,7 @@ public class LogService {
//文件名称
String fileName = "DataEase操作日志";
String encodeFileName = URLEncoder.encode(fileName, "UTF-8");
response.setHeader("Content-disposition", "attachment;filename=" + encodeFileName + ".xls");
response.setHeader("Content-disposition", "attachment;filename=" + encodeFileName + ".xlsx");
wb.write(outputStream);
outputStream.flush();
outputStream.close();

View File

@ -2605,7 +2605,8 @@ export default {
time: 'Time',
export: 'Export',
confirm: 'Sure Export ?',
search_by_key: 'Search by key'
search_by_key: 'Search by key',
ip: 'IP'
},
plugin_style: {
border: 'Border'

View File

@ -2606,7 +2606,8 @@ export default {
time: '操作時間',
export: '導出',
confirm: '確定導出嗎?',
search_by_key: '搜索詳情'
search_by_key: '搜索詳情',
ip: 'IP地址'
},
plugin_style: {
border: '邊框'

View File

@ -2606,7 +2606,8 @@ export default {
time: '操作时间',
export: '导出',
confirm: '确定导出吗?',
search_by_key: '搜索详情'
search_by_key: '搜索详情',
ip: 'IP地址'
},
plugin_style: {
border: '边框'

View File

@ -112,6 +112,12 @@
:label="$t('log.user')"
width="100"
/>
<el-table-column
show-overflow-tooltip
prop="ip"
:label="$t('log.ip')"
width="100"
/>
<el-table-column
show-overflow-tooltip
prop="time"
@ -204,7 +210,7 @@ export default {
const link = document.createElement('a')
link.style.display = 'none'
link.href = URL.createObjectURL(blob)
link.download = 'DataEase操作日志.xls' //
link.download = 'DataEase操作日志.xlsx' //
document.body.appendChild(link)
link.click()
document.body.removeChild(link)