forked from github/dataease
Merge pull request #3669 from dataease/pr@dev@feat_log_ip
feat(日志管理): 客户端IP地址
This commit is contained in:
commit
5d3a4a1828
45
backend/src/main/java/io/dataease/commons/utils/IPUtils.java
Normal file
45
backend/src/main/java/io/dataease/commons/utils/IPUtils.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
@ -22,4 +22,7 @@ public class SysLogGridDTO implements Serializable {
|
|||||||
|
|
||||||
@ApiModelProperty("操作时间")
|
@ApiModelProperty("操作时间")
|
||||||
private Long time;
|
private Long time;
|
||||||
|
|
||||||
|
@ApiModelProperty("IP地址")
|
||||||
|
private String ip;
|
||||||
}
|
}
|
||||||
|
@ -9,6 +9,7 @@ import io.dataease.commons.constants.ParamConstants;
|
|||||||
import io.dataease.commons.constants.SysLogConstants;
|
import io.dataease.commons.constants.SysLogConstants;
|
||||||
import io.dataease.commons.utils.AuthUtils;
|
import io.dataease.commons.utils.AuthUtils;
|
||||||
import io.dataease.commons.utils.BeanUtils;
|
import io.dataease.commons.utils.BeanUtils;
|
||||||
|
import io.dataease.commons.utils.IPUtils;
|
||||||
import io.dataease.commons.utils.ServletUtils;
|
import io.dataease.commons.utils.ServletUtils;
|
||||||
import io.dataease.controller.sys.base.ConditionEntity;
|
import io.dataease.controller.sys.base.ConditionEntity;
|
||||||
import io.dataease.controller.sys.request.KeyGridRequest;
|
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.commons.lang3.StringUtils;
|
||||||
import org.apache.poi.hssf.usermodel.*;
|
import org.apache.poi.hssf.usermodel.*;
|
||||||
import org.apache.poi.ss.usermodel.*;
|
import org.apache.poi.ss.usermodel.*;
|
||||||
|
import org.apache.poi.xssf.usermodel.*;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
@ -308,6 +310,7 @@ public class LogService {
|
|||||||
sysLogGridDTO.setTime(vo.getTime());
|
sysLogGridDTO.setTime(vo.getTime());
|
||||||
sysLogGridDTO.setUser(vo.getNickName());
|
sysLogGridDTO.setUser(vo.getNickName());
|
||||||
sysLogGridDTO.setDetail(logManager.detailInfo(vo));
|
sysLogGridDTO.setDetail(logManager.detailInfo(vo));
|
||||||
|
sysLogGridDTO.setIp(vo.getIp());
|
||||||
return sysLogGridDTO;
|
return sysLogGridDTO;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -330,12 +333,14 @@ public class LogService {
|
|||||||
sysLogWithBLOBs.setLoginName(sysLogDTO.getSourceName());
|
sysLogWithBLOBs.setLoginName(sysLogDTO.getSourceName());
|
||||||
sysLogWithBLOBs.setNickName(sysLogDTO.getSourceName());
|
sysLogWithBLOBs.setNickName(sysLogDTO.getSourceName());
|
||||||
}
|
}
|
||||||
|
sysLogWithBLOBs.setIp(IPUtils.get());
|
||||||
|
|
||||||
sysLogMapper.insert(sysLogWithBLOBs);
|
sysLogMapper.insert(sysLogWithBLOBs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void exportExcel(KeyGridRequest request) throws Exception {
|
public void exportExcel(KeyGridRequest request) throws Exception {
|
||||||
|
request = logRetentionProxy(request);
|
||||||
request = detailRequest(request);
|
request = detailRequest(request);
|
||||||
String keyWord = request.getKeyWord();
|
String keyWord = request.getKeyWord();
|
||||||
List<String> ids = null;
|
List<String> ids = null;
|
||||||
@ -355,24 +360,25 @@ public class LogService {
|
|||||||
List<String[]> details = lists.stream().map(item -> {
|
List<String[]> details = lists.stream().map(item -> {
|
||||||
String operateTypeName = SysLogConstants.operateTypeName(item.getOperateType());
|
String operateTypeName = SysLogConstants.operateTypeName(item.getOperateType());
|
||||||
String sourceTypeName = SysLogConstants.sourceTypeName(item.getSourceType());
|
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[0] = Translator.get(operateTypeName) + " " + Translator.get(sourceTypeName);
|
||||||
row[1] = logManager.detailInfo(item);
|
row[1] = logManager.detailInfo(item);
|
||||||
row[2] = item.getNickName();
|
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;
|
return row;
|
||||||
}).collect(Collectors.toList());
|
}).collect(Collectors.toList());
|
||||||
String[] headArr = {"操作类型", "详情", "用户", "时间"};
|
String[] headArr = {"操作类型", "详情", "用户", "IP地址", "时间"};
|
||||||
details.add(0, headArr);
|
details.add(0, headArr);
|
||||||
|
|
||||||
|
|
||||||
HSSFWorkbook wb = new HSSFWorkbook();
|
XSSFWorkbook wb = new XSSFWorkbook();
|
||||||
//明细sheet
|
//明细sheet
|
||||||
HSSFSheet detailsSheet = wb.createSheet("数据");
|
XSSFSheet detailsSheet = wb.createSheet("数据");
|
||||||
|
|
||||||
//给单元格设置样式
|
//给单元格设置样式
|
||||||
CellStyle cellStyle = wb.createCellStyle();
|
XSSFCellStyle cellStyle = wb.createCellStyle();
|
||||||
Font font = wb.createFont();
|
XSSFFont font = wb.createFont();
|
||||||
//设置字体大小
|
//设置字体大小
|
||||||
font.setFontHeightInPoints((short) 12);
|
font.setFontHeightInPoints((short) 12);
|
||||||
//设置字体加粗
|
//设置字体加粗
|
||||||
@ -386,11 +392,11 @@ public class LogService {
|
|||||||
|
|
||||||
if (CollectionUtils.isNotEmpty(details)) {
|
if (CollectionUtils.isNotEmpty(details)) {
|
||||||
for (int i = 0; i < details.size(); i++) {
|
for (int i = 0; i < details.size(); i++) {
|
||||||
HSSFRow row = detailsSheet.createRow(i);
|
XSSFRow row = detailsSheet.createRow(i);
|
||||||
String[] rowData = details.get(i);
|
String[] rowData = details.get(i);
|
||||||
if (rowData != null) {
|
if (rowData != null) {
|
||||||
for (int j = 0; j < rowData.length; j++) {
|
for (int j = 0; j < rowData.length; j++) {
|
||||||
HSSFCell cell = row.createCell(j);
|
XSSFCell cell = row.createCell(j);
|
||||||
cell.setCellValue(rowData[j]);
|
cell.setCellValue(rowData[j]);
|
||||||
if (i == 0) {// 头部
|
if (i == 0) {// 头部
|
||||||
cell.setCellStyle(cellStyle);
|
cell.setCellStyle(cellStyle);
|
||||||
@ -406,7 +412,7 @@ public class LogService {
|
|||||||
//文件名称
|
//文件名称
|
||||||
String fileName = "DataEase操作日志";
|
String fileName = "DataEase操作日志";
|
||||||
String encodeFileName = URLEncoder.encode(fileName, "UTF-8");
|
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);
|
wb.write(outputStream);
|
||||||
outputStream.flush();
|
outputStream.flush();
|
||||||
outputStream.close();
|
outputStream.close();
|
||||||
|
@ -2605,7 +2605,8 @@ export default {
|
|||||||
time: 'Time',
|
time: 'Time',
|
||||||
export: 'Export',
|
export: 'Export',
|
||||||
confirm: 'Sure Export ?',
|
confirm: 'Sure Export ?',
|
||||||
search_by_key: 'Search by key'
|
search_by_key: 'Search by key',
|
||||||
|
ip: 'IP'
|
||||||
},
|
},
|
||||||
plugin_style: {
|
plugin_style: {
|
||||||
border: 'Border'
|
border: 'Border'
|
||||||
|
@ -2606,7 +2606,8 @@ export default {
|
|||||||
time: '操作時間',
|
time: '操作時間',
|
||||||
export: '導出',
|
export: '導出',
|
||||||
confirm: '確定導出嗎?',
|
confirm: '確定導出嗎?',
|
||||||
search_by_key: '搜索詳情'
|
search_by_key: '搜索詳情',
|
||||||
|
ip: 'IP地址'
|
||||||
},
|
},
|
||||||
plugin_style: {
|
plugin_style: {
|
||||||
border: '邊框'
|
border: '邊框'
|
||||||
|
@ -2606,7 +2606,8 @@ export default {
|
|||||||
time: '操作时间',
|
time: '操作时间',
|
||||||
export: '导出',
|
export: '导出',
|
||||||
confirm: '确定导出吗?',
|
confirm: '确定导出吗?',
|
||||||
search_by_key: '搜索详情'
|
search_by_key: '搜索详情',
|
||||||
|
ip: 'IP地址'
|
||||||
},
|
},
|
||||||
plugin_style: {
|
plugin_style: {
|
||||||
border: '边框'
|
border: '边框'
|
||||||
|
@ -112,6 +112,12 @@
|
|||||||
:label="$t('log.user')"
|
:label="$t('log.user')"
|
||||||
width="100"
|
width="100"
|
||||||
/>
|
/>
|
||||||
|
<el-table-column
|
||||||
|
show-overflow-tooltip
|
||||||
|
prop="ip"
|
||||||
|
:label="$t('log.ip')"
|
||||||
|
width="100"
|
||||||
|
/>
|
||||||
<el-table-column
|
<el-table-column
|
||||||
show-overflow-tooltip
|
show-overflow-tooltip
|
||||||
prop="time"
|
prop="time"
|
||||||
@ -204,7 +210,7 @@ export default {
|
|||||||
const link = document.createElement('a')
|
const link = document.createElement('a')
|
||||||
link.style.display = 'none'
|
link.style.display = 'none'
|
||||||
link.href = URL.createObjectURL(blob)
|
link.href = URL.createObjectURL(blob)
|
||||||
link.download = 'DataEase操作日志.xls' // 下载的文件名
|
link.download = 'DataEase操作日志.xlsx' // 下载的文件名
|
||||||
document.body.appendChild(link)
|
document.body.appendChild(link)
|
||||||
link.click()
|
link.click()
|
||||||
document.body.removeChild(link)
|
document.body.removeChild(link)
|
||||||
|
Loading…
Reference in New Issue
Block a user