添加大量访问日志记录
This commit is contained in:
@@ -37,9 +37,6 @@ public class DataController {
|
||||
@Autowired
|
||||
private HomeworkService homeworkService;
|
||||
|
||||
@Autowired
|
||||
private FileService fileService;
|
||||
|
||||
@Autowired
|
||||
private VUserfileService vUserfileService;
|
||||
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
package com.fjy.spring.controller;
|
||||
|
||||
import com.fjy.spring.domain.TbFile;
|
||||
import com.fjy.spring.domain.TbLog;
|
||||
import com.fjy.spring.domain.TbUser;
|
||||
import com.fjy.spring.enums.ResultEnum;
|
||||
import com.fjy.spring.exception.UserException;
|
||||
import com.fjy.spring.service.FileService;
|
||||
import com.fjy.spring.service.LogService;
|
||||
import com.fjy.spring.untils.GetIPAddrUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
@@ -12,13 +16,28 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.File;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
import static com.fjy.spring.constant.GlobalConstant.USER_SESSION_KEY;
|
||||
|
||||
@Controller
|
||||
@Slf4j
|
||||
public class DeleteController {
|
||||
/**
|
||||
* 文件相关数据库操作
|
||||
*/
|
||||
@Autowired
|
||||
private FileService fileService;//文件相关数据库操作
|
||||
private FileService fileService;
|
||||
|
||||
@Autowired
|
||||
private LogService logService;
|
||||
|
||||
@Resource
|
||||
HttpServletRequest request;
|
||||
|
||||
@GetMapping("/home/findfile")
|
||||
@ResponseBody
|
||||
@@ -68,6 +87,22 @@ public class DeleteController {
|
||||
if (file.delete()) {
|
||||
fileService.deleteFileById(tbFile);
|
||||
log.info("删除单个文件" + fileName + "成功!");
|
||||
|
||||
TbUser user =(TbUser)request.getSession().getAttribute(USER_SESSION_KEY);
|
||||
//写入日志信息
|
||||
Date date = new Date();
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
String dateNowStr = sdf.format(date);
|
||||
TbLog log = new TbLog();
|
||||
log.setUserid(user.getColuserid());
|
||||
log.setColtime(dateNowStr);
|
||||
log.setColheader("删除单个文件" + fileName + "成功!");
|
||||
log.setRequestURL(request.getRequestURL().toString());
|
||||
|
||||
//解决nginx代理后IP地址获取问题
|
||||
log.setColip(GetIPAddrUtil.getIpAddr(request));
|
||||
logService.addLogRec(log);
|
||||
|
||||
return true;
|
||||
} else {
|
||||
log.info("删除单个文件" + fileName + "失败!");
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
package com.fjy.spring.controller;
|
||||
|
||||
import com.fjy.spring.domain.TbFile;
|
||||
import com.fjy.spring.domain.TbLog;
|
||||
import com.fjy.spring.domain.TbUser;
|
||||
import com.fjy.spring.enums.ResultEnum;
|
||||
import com.fjy.spring.exception.UserException;
|
||||
import com.fjy.spring.properties.ServerProperties;
|
||||
import com.fjy.spring.service.FileService;
|
||||
import com.fjy.spring.service.LogService;
|
||||
import com.fjy.spring.untils.LogUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
@@ -28,12 +31,18 @@ import static com.fjy.spring.constant.GlobalConstant.USER_SESSION_KEY;
|
||||
|
||||
@Controller
|
||||
public class DownLoadController {
|
||||
/**
|
||||
* 服务器配置信息
|
||||
*/
|
||||
@Autowired
|
||||
private ServerProperties serverProperties;//服务器配置信息
|
||||
private ServerProperties serverProperties;
|
||||
|
||||
@Autowired
|
||||
private FileService fileService;
|
||||
|
||||
@Autowired
|
||||
private LogService logService;
|
||||
|
||||
@Resource
|
||||
HttpServletRequest request;
|
||||
|
||||
@@ -45,12 +54,12 @@ public class DownLoadController {
|
||||
@GetMapping("/home/admin/download/findall")
|
||||
@ResponseBody
|
||||
public List<TbFile> toDownloadAll() {
|
||||
List<TbFile> files = fileService.findAllFile();//此处做空指针判断并抛出错误
|
||||
//此处做空指针判断并抛出错误
|
||||
List<TbFile> files = fileService.findAllFile();
|
||||
if (files != null) {
|
||||
return files;
|
||||
}
|
||||
new UserException(ResultEnum.EMPTY_DATA);
|
||||
return null;
|
||||
throw new UserException(ResultEnum.EMPTY_DATA);
|
||||
}
|
||||
|
||||
@GetMapping("/home/download/findone")
|
||||
@@ -63,8 +72,7 @@ public class DownLoadController {
|
||||
if (files != null) {
|
||||
return files;
|
||||
}
|
||||
new UserException(ResultEnum.EMPTY_DATA);
|
||||
return null;
|
||||
throw new UserException(ResultEnum.EMPTY_DATA);
|
||||
}
|
||||
|
||||
@RequestMapping("/home/download/dodownload")
|
||||
@@ -102,6 +110,10 @@ public class DownLoadController {
|
||||
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
|
||||
bos.write(buff, 0, bytesRead);
|
||||
}
|
||||
//记录下载日志
|
||||
addVisitLog("下载文件"+tbFile.getColrealname()+" "+tbFile.getColfilename());
|
||||
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
@@ -164,7 +176,7 @@ public class DownLoadController {
|
||||
for (int i = 0; i < path.length; i++) {
|
||||
File file = new File(path[i]);
|
||||
//将需要压缩的文件格式化为输入流
|
||||
if (!file.isDirectory()){
|
||||
if (!file.isDirectory()) {
|
||||
zipSource = new FileInputStream(file);
|
||||
//压缩条目不是具体独立的文件,而是压缩包文件列表中的列表项,称为条目,就像索引一样
|
||||
ZipEntry zipEntry = new ZipEntry(file.getName());
|
||||
@@ -183,6 +195,9 @@ public class DownLoadController {
|
||||
}
|
||||
}
|
||||
|
||||
//记录下载日志
|
||||
addVisitLog("下载压缩文件"+zipFile.getName());
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
@@ -281,4 +296,15 @@ public class DownLoadController {
|
||||
System.out.println(it1.next());
|
||||
}
|
||||
}*/
|
||||
|
||||
/**
|
||||
* 登陆后的访问日志记录
|
||||
*
|
||||
* @param content
|
||||
*/
|
||||
private void addVisitLog(String content) {
|
||||
TbUser user = (TbUser) request.getSession().getAttribute(USER_SESSION_KEY);
|
||||
TbLog log = LogUtil.addLog(user, content, request);
|
||||
logService.addLogRec(log);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
package com.fjy.spring.controller;
|
||||
|
||||
import com.fjy.spring.domain.TbLog;
|
||||
import com.fjy.spring.domain.TbUser;
|
||||
import com.fjy.spring.service.LogService;
|
||||
import com.fjy.spring.untils.GetIPAddrUtil;
|
||||
import com.fjy.spring.untils.LogUtil;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||
@@ -10,6 +15,9 @@ import org.springframework.web.bind.support.SessionStatus;
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
import static com.fjy.spring.constant.GlobalConstant.USER_SESSION_KEY;
|
||||
|
||||
@Controller
|
||||
@@ -18,70 +26,108 @@ public class NavController {
|
||||
@Resource
|
||||
HttpServletRequest request;
|
||||
|
||||
@Autowired
|
||||
private LogService logService;
|
||||
|
||||
@GetMapping(value = {"index",""})
|
||||
public String toLoginPage(){
|
||||
//获取访问头信息
|
||||
addIndexLog(request.getHeader("user-agent"));
|
||||
return "login";
|
||||
}
|
||||
|
||||
@GetMapping(value = {"testthymeleaf"})
|
||||
public String toTestPage(){
|
||||
addVisitLog("测试页面 testthymeleaf");
|
||||
return "/dist/thymeleafTest";
|
||||
}
|
||||
|
||||
@GetMapping(value = {"axiosTest"})
|
||||
public String toTestAxiosPage(){
|
||||
addVisitLog("测试页面 axiosTest");
|
||||
return "/dist/axiosTest";
|
||||
}
|
||||
|
||||
@GetMapping(value = {"/home"})
|
||||
public String toHomePage(){
|
||||
addVisitLog("首页 home");
|
||||
return "home/home";
|
||||
}
|
||||
|
||||
@GetMapping(value = {"/logout"})
|
||||
public String toLogOut(SessionStatus status){
|
||||
//request.getSession().getAttribute(USER_SESSION_KEY).invalidate();
|
||||
addVisitLog("登出 logout");
|
||||
status.setComplete();
|
||||
return "login";
|
||||
}
|
||||
|
||||
@GetMapping(value = {"/home/feedback"})
|
||||
public String toFeedbackPage(){
|
||||
addVisitLog("反馈页面 /home/feedback");
|
||||
return "home/feedback";
|
||||
}
|
||||
|
||||
@GetMapping(value = {"/home/about"})
|
||||
public String toAboutPage(){
|
||||
addVisitLog("关于页面 /home/about");
|
||||
return "home/about";
|
||||
}
|
||||
|
||||
@GetMapping(value = {"/home/admin"})
|
||||
public String toAdminPage(){
|
||||
addVisitLog("管理员页面 /home/admin");
|
||||
return "home/admin";
|
||||
}
|
||||
|
||||
@GetMapping(value = {"/home/admin/managecourse"})
|
||||
public String toManageCoursePage(){
|
||||
addVisitLog("课程管理页面 /home/admin/managecourse");
|
||||
return "home/managecourse";
|
||||
}
|
||||
|
||||
@GetMapping(value = {"/home/admin/manageuser"})
|
||||
public String toManageUserPage(){
|
||||
addVisitLog("用户管理页面 /home/admin/manageuser");
|
||||
return "home/manageuser";
|
||||
}
|
||||
|
||||
@GetMapping(value = {"/home/admin/homework"})
|
||||
public String toHomeworkPage(){
|
||||
addVisitLog("作业管理页面 /home/admin/homework");
|
||||
return "home/homework";
|
||||
}
|
||||
|
||||
@GetMapping(value = {"/home/user"})
|
||||
public String toUserPage(){
|
||||
addVisitLog("个人中心页面 /home/user");
|
||||
return "home/user";
|
||||
}
|
||||
|
||||
@GetMapping(value = {"/error"})
|
||||
public String toErrorPage(){
|
||||
addVisitLog("404页面 error");
|
||||
return "error";
|
||||
}
|
||||
|
||||
/**
|
||||
* 首页的访问日志记录
|
||||
* @param content
|
||||
*/
|
||||
private void addIndexLog(String content){
|
||||
TbUser user = new TbUser();
|
||||
user.setColname("访客");
|
||||
TbLog log = LogUtil.addLog(user,content,request);
|
||||
logService.addLogRec(log);
|
||||
}
|
||||
|
||||
/**
|
||||
* 登陆后的访问日志记录
|
||||
* @param content
|
||||
*/
|
||||
private void addVisitLog(String content){
|
||||
TbUser user =(TbUser)request.getSession().getAttribute(USER_SESSION_KEY);
|
||||
TbLog log = LogUtil.addLog(user,content,request);
|
||||
logService.addLogRec(log);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import com.fjy.spring.service.HomeworkService;
|
||||
import com.fjy.spring.service.LogService;
|
||||
import com.fjy.spring.untils.FormatFileSizeUtil;
|
||||
import com.fjy.spring.untils.GetIPAddrUtil;
|
||||
import com.fjy.spring.untils.LogUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -29,6 +30,8 @@ import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static com.fjy.spring.constant.GlobalConstant.USER_SESSION_KEY;
|
||||
|
||||
@Controller
|
||||
@Slf4j
|
||||
public class UpLoadController {
|
||||
@@ -98,6 +101,10 @@ public class UpLoadController {
|
||||
file.setColuserid(user.getColuserid());
|
||||
if (fileService.addFile(file)) {
|
||||
log.info("记录写入数据库成功");
|
||||
|
||||
// 记录上传日志
|
||||
addVisitLog("上传文件" + file.getColrealname() + " " + file.getColfilename());
|
||||
|
||||
}
|
||||
//System.out.println("记录写入数据库成功");
|
||||
else {
|
||||
@@ -248,6 +255,9 @@ public class UpLoadController {
|
||||
|
||||
if (fileService.addFile(tbFile)) {
|
||||
log.info("记录写入数据库成功");
|
||||
|
||||
// 记录上传日志
|
||||
addVisitLog("上传文件" + tbFile.getColrealname() + " " + tbFile.getColfilename());
|
||||
} else {
|
||||
log.error("记录写入数据库失败");
|
||||
}
|
||||
@@ -265,13 +275,21 @@ public class UpLoadController {
|
||||
request.getScheme() + "://" + request.getServerName() + ":"
|
||||
+ serverProperties.getPortNum() + request.getContextPath() + "/upload/"
|
||||
+ file.getOriginalFilename());
|
||||
} catch (IllegalStateException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 登陆后的访问日志记录
|
||||
*
|
||||
* @param content
|
||||
*/
|
||||
private void addVisitLog(String content) {
|
||||
TbUser user = (TbUser) httpServletRequest.getSession().getAttribute(USER_SESSION_KEY);
|
||||
TbLog log = LogUtil.addLog(user, content, httpServletRequest);
|
||||
logService.addLogRec(log);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,17 @@
|
||||
package com.fjy.spring.controller;
|
||||
|
||||
import com.fjy.spring.domain.TbLog;
|
||||
import com.fjy.spring.domain.TbUser;
|
||||
import com.fjy.spring.domain.VUserinfo;
|
||||
import com.fjy.spring.enums.RegisteredEnum;
|
||||
import com.fjy.spring.enums.ResultEnum;
|
||||
import com.fjy.spring.exception.UserException;
|
||||
import com.fjy.spring.properties.ServerProperties;
|
||||
import com.fjy.spring.service.LogService;
|
||||
import com.fjy.spring.service.StudentService;
|
||||
import com.fjy.spring.service.UserService;
|
||||
import com.fjy.spring.untils.CodingUtil;
|
||||
import com.fjy.spring.untils.LogUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
@@ -20,6 +23,8 @@ import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.math.BigInteger;
|
||||
|
||||
import static com.fjy.spring.constant.GlobalConstant.USER_SESSION_KEY;
|
||||
|
||||
@Controller
|
||||
@Slf4j
|
||||
public class UpdateController {
|
||||
@@ -30,7 +35,7 @@ public class UpdateController {
|
||||
private StudentService studentService;
|
||||
|
||||
@Autowired
|
||||
private ServerProperties serverProperties;
|
||||
private LogService logService;
|
||||
|
||||
@Resource
|
||||
HttpServletRequest request;
|
||||
@@ -52,11 +57,20 @@ public class UpdateController {
|
||||
studentService.UpdateStudentListRegistered(tbUser.getColrealname(),tbUser.getColstudentno(),
|
||||
RegisteredEnum.REGISTERED.getCode());
|
||||
log.info(tbUser.getColname()+" 信息更新成功");
|
||||
//写入数据库日志
|
||||
TbUser user =(TbUser)request.getSession().getAttribute(USER_SESSION_KEY);
|
||||
TbLog tbLog = LogUtil.addLog(user,tbUser.getColname()+" 信息更新成功",request);
|
||||
logService.addLogRec(tbLog);
|
||||
|
||||
return true;
|
||||
/*return "redirect:" + request.getScheme() + "://" + request.getServerName() + ":"
|
||||
+ serverProperties.getPortNum() + request.getContextPath() + "/home/user";*/
|
||||
// return "login";
|
||||
}
|
||||
//写入数据库日志
|
||||
TbUser user =(TbUser)request.getSession().getAttribute(USER_SESSION_KEY);
|
||||
TbLog tbLog = LogUtil.addLog(user,tbUser.getColname()+" 信息更新失败",request);
|
||||
logService.addLogRec(tbLog);
|
||||
log.error(tbUser.getColname()+" 信息更新失败");
|
||||
return false;
|
||||
}
|
||||
|
||||
41
src/main/java/com/fjy/spring/untils/LogUtil.java
Normal file
41
src/main/java/com/fjy/spring/untils/LogUtil.java
Normal file
@@ -0,0 +1,41 @@
|
||||
package com.fjy.spring.untils;
|
||||
|
||||
import com.fjy.spring.domain.TbLog;
|
||||
import com.fjy.spring.domain.TbUser;
|
||||
import com.fjy.spring.service.LogService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
import static com.fjy.spring.constant.GlobalConstant.USER_SESSION_KEY;
|
||||
|
||||
/**
|
||||
* @author F嘉阳
|
||||
* @date 2018-04-26 12:45
|
||||
* 日志信息填充工具类
|
||||
*/
|
||||
public class LogUtil {
|
||||
|
||||
/**
|
||||
* 写入日志记录
|
||||
* @param user
|
||||
* @param content
|
||||
*/
|
||||
public static TbLog addLog(TbUser user,String content,HttpServletRequest request){
|
||||
//写入日志信息
|
||||
Date date = new Date();
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
String dateNowStr = sdf.format(date);
|
||||
TbLog log = new TbLog();
|
||||
log.setUserid(user.getColuserid());
|
||||
log.setColtime(dateNowStr);
|
||||
log.setColheader(user.getColname()+" "+content);
|
||||
log.setRequestURL(request.getRequestURL().toString());
|
||||
|
||||
//解决nginx代理后IP地址获取问题
|
||||
log.setColip(GetIPAddrUtil.getIpAddr(request));
|
||||
return log;
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
<!DOCTYPE html>
|
||||
<html xmlns:th="http://www.thymeleaf.org">
|
||||
<head th:fragment="header(title)">
|
||||
<meta name="renderer" content="webkit">
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title th:text="${title}"></title>
|
||||
|
||||
@@ -81,6 +81,7 @@
|
||||
<el-card class="box-card hidden-xs-only" th:fragment="timelimit">
|
||||
<div slot="header" class="clearfix">
|
||||
<span>作业提交倒计时</span>
|
||||
<!--超时后仍为正数-->
|
||||
<span style="color: red"> 功能未完成</span>
|
||||
</div>
|
||||
<div class="text item">
|
||||
|
||||
@@ -48,7 +48,7 @@
|
||||
<el-row>
|
||||
<span>近期登陆日志</span><br>
|
||||
<template>
|
||||
<el-table :data="logData" height="300" :default-sort="{prop: 'time', order: 'descending'}">
|
||||
<el-table :data="logData" height="350" :default-sort="{prop: 'time', order: 'descending'}">
|
||||
<el-table-column prop="colname" label="用户名" sortable>
|
||||
</el-table-column>
|
||||
<el-table-column prop="coltime" label="时间" sortable>
|
||||
|
||||
@@ -103,7 +103,8 @@
|
||||
<el-col :span="23">
|
||||
<div class="grid-content bg-purple-dark">
|
||||
<template>
|
||||
<el-table :data="tableHomeworkData" style="width: 100%">
|
||||
<el-table :data="tableHomeworkData" style="width: 100%"
|
||||
:default-sort="{prop: 'worktime', order: 'descending'}">
|
||||
<el-table-column type="expand">
|
||||
<template slot-scope="props">
|
||||
<el-form label-position="left" inline class="demo-table-expand">
|
||||
|
||||
@@ -26,7 +26,7 @@
|
||||
<el-button @click="togglePost()" type="warning" plain>设为管理员</el-button>
|
||||
<el-button @click="togglePost()" type="danger" plain>删除用户</el-button>
|
||||
</div>
|
||||
<el-table ref="multipleTable" :data="userData" @selection-change="handleSelectionChange"
|
||||
<el-table ref="multipleTable" :data="userData" height="650" @selection-change="handleSelectionChange"
|
||||
tooltip-effect="dark" style="width: 100%" >
|
||||
<el-table-column type="selection" prop='coluserid'>
|
||||
</el-table-column>
|
||||
|
||||
Reference in New Issue
Block a user