数据后台管理(五)AOP日志 为了增加数据的安全性,在数据管理的
为了增加数据的安全性,在数据管理的过程中,我们需要将操作者访问时间,操作者的名称,访问的IP,访问资源的URL,执行时长,访问方法记录下来存储到数据库中,并可以通过页面查看。
1.将日志信息存储到数据库中
1.1根据需要记录的日志内容在数据库中创建表syslog和对应的实体类SysLog
日志表syslog
SysLog类
SysLog
1.2在controller包下新建一个切面类LogAop来获取需要记录日志内容
注意该类作为切面类需要注解@Aspect
在该切面类内创建一个前置通知@Before("execution(* club.nipengfei.ssm.controller.*.*(..))"),一个后置通知@After("execution(* club.nipengfei.ssm.controller.*.*(..))"),注解内的属性表示切入点表达式,在这里表示controller包下的所有类。
1.2.1获取操作者的访问时间
直接在前置通知内new一个Date()
1.2.2获取操作者名称
复制代码
1 // 获取当前操作的用户
2 SecurityContext context = SecurityContextHolder.getContext();
3 User user =(User) context.getAuthentication().getPrincipal();
4 String username = user.getUsername();
复制代码
1.2.3获取访问IP
先在web.xml中新增一个监听器
复制代码
1
2 org.springframework.web.context.request.RequestContextListener
3
复制代码
通过getRemoteAddr()方法获取IP
1 // 获取IP地址
2 String ip = request.getRemoteAddr();
1.2.4获取访问资源的URL
思路:获取类上注解的@RequestMapping的属性值和方法上注解@RequestMapping的属性值,并将两者拼接。
获取类上注解属性值:通过反射获取操作的类,使用getAnnotation(RequestMapping.class)方法获取@RequestMapping注解,使用value()获取属性
获取方法上注解属性值:通过反射获取操作的类,使用getMethod()方法获取方法,使用getAnnotation(RequestMapping.class)方法获取@RequestMapping注解,使用value()获取属性
具体代码放下面。
1.2.5获取执行时长
在后置通知内new一个Date()减去前置通知的Date()。
1 // 获取访问时长
2 long time = new Date().getTime()-visitTime.getTime();
1.2.6获取访问方法
通过类的getMethod()方法获取方法。
注意:有些方法含参,有些不含参需要分开处理。
复制代码
1 @Before("execution(* club.nipengfei.ssm.controller.*.*(..))")
2 public void doBefore(JoinPoint jp) throws NoSuchMethodException {
3 visitTime = new Date(); // 当前时间就是开始访问的类
4 clazz = jp.getTarget().getClass(); // 具体访问的类
5 String methodName = jp.getSignature().getName(); // 获取访问方法名称
6 Object[] args = jp.getArgs(); // 获取访问方法参数
7
8 // 获取具体执行方法Method对象
9 if (args==null || args.length==0){
10 method = clazz.getMethod(methodName);
11 } else {
12 Class[] classArgs = new Class[args.length];
13 for (int i=0;i findAll() throws Exception;
16 }
复制代码
2.2在SysLogServiceImpl类内调用上面方法
复制代码
1 package club.nipengfei.ssm.service.impl;
2
3 import club.nipengfei.ssm.dao.ISysLogDao;
4 import club.nipengfei.ssm.domain.SysLog;
5 import club.nipengfei.ssm.service.ISysLogService;
6 import org.springframework.beans.factory.annotation.Autowired;
7 import org.springframework.stereotype.Service;
8 import org.springframework.transaction.annotation.Transactional;
9
10 import java.util.List;
11
12 @Service
13 @Transactional
14 public class SysLogServiceImpl implements ISysLogService {
15
16 @Autowired
17 private ISysLogDao sysLogDao;
18
19 @Override
20 public void save(SysLog sysLog) throws Exception {
21 sysLogDao.save(sysLog);
22 }
23
24 @Override
25 public List findAll() throws Exception {
26 return sysLogDao.findAll();
27 }
28 }
复制代码
2.3在controller包下新建一个SysLogController类
复制代码
1 package club.nipengfei.ssm.controller;
2
3 import club.nipengfei.ssm.domain.SysLog;
4 import club.nipengfei.ssm.service.ISysLogService;
5 import org.springframework.beans.factory.annotation.Autowired;
6 import org.springframework.stereotype.Controller;
7 import org.springframework.web.bind.annotation.RequestMapping;
8 import org.springframework.web.servlet.ModelAndView;
9
10 import java.util.List;
11
12 @Controller
13 @RequestMapping("/sysLog")
14 public class SysLogController {
15
16 @Autowired
17 private ISysLogService sysLogService;
18
19 @RequestMapping("/findAll.do")
20 public ModelAndView findAll() throws Exception {
21 ModelAndView mv = new ModelAndView();
22 List sysLogList = sysLogService.findAll();
23 mv.addObject("sysLogs",sysLogList);
24 mv.setViewName("syslog-list");
25 return mv;
2https://www.cnblogs.com/qzwl/p/11792588.html