hi,你好!欢迎访问本站!登录
本站由网站地图腾讯云宝塔系统阿里云强势驱动
当前位置:首页 - 教程 - 杂谈 - 正文 君子好学,自强不息!

数据背景治理(五)AOP日记

2019-11-18杂谈搜奇网36°c
A+ A-

为了增添数据的安全性,在数据治理的过程当中,我们须要将操纵者接见时候,操纵者的称号,接见的IP,接见资本的URL,实行时长,接见要领纪录下来存储到数据库中,并可以经由过程页面检察。

1.将日记信息存储到数据库中

1.1根据须要纪录的日记内容在数据库中建立表syslog和对应的实体类SysLog

日记表syslog

SysLog类

 1 package club.nipengfei.ssm.domain;
 2 
 3 import java.util.Date;
 4 
 5 public class SysLog {
 6     private String id;
 7     private Date visitTime;
 8     private String visitTimeStr;
 9     private String username;
10     private String ip;
11     private String url;
12     private Long executionTime;
13     private String method;
14 
15     public String getId() {
16         return id;
17     }
18 
19     public void setId(String id) {
20         this.id = id;
21     }
22 
23     public Date getVisitTime() {
24         return visitTime;
25     }
26 
27     public void setVisitTime(Date visitTime) {
28         this.visitTime = visitTime;
29     }
30 
31     public String getVisitTimeStr() {
32         return visitTimeStr;
33     }
34 
35     public void setVisitTimeStr(String visitTimeStr) {
36         this.visitTimeStr = visitTimeStr;
37     }
38 
39     public String getUsername() {
40         return username;
41     }
42 
43     public void setUsername(String username) {
44         this.username = username;
45     }
46 
47     public String getIp() {
48         return ip;
49     }
50 
51     public void setIp(String ip) {
52         this.ip = ip;
53     }
54 
55     public String getUrl() {
56         return url;
57     }
58 
59     public void setUrl(String url) {
60         this.url = url;
61     }
62 
63     public Long getExecutionTime() {
64         return executionTime;
65     }
66 
67     public void setExecutionTime(Long executionTime) {
68         this.executionTime = executionTime;
69     }
70 
71     public String getMethod() {
72         return method;
73     }
74 
75     public void setMethod(String method) {
76         this.method = method;
77     }
78 }
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   <listener>
2     <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
3   </listener>

经由过程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<args.length;i++){
14                 classArgs[i] = args[i].getClass();
15             }
16             method = clazz.getMethod(methodName,classArgs);
17         }
18     }

 LogAop类的代码:

 1 package club.nipengfei.ssm.controller;
 2 
 3 import club.nipengfei.ssm.domain.SysLog;
 4 import club.nipengfei.ssm.service.ISysLogService;
 5 import org.aspectj.lang.JoinPoint;
 6 import org.aspectj.lang.annotation.After;
 7 import org.aspectj.lang.annotation.Aspect;
 8 import org.aspectj.lang.annotation.Before;
 9 import org.springframework.beans.factory.annotation.Autowired;
10 import org.springframework.security.core.context.SecurityContext;
11 import org.springframework.security.core.context.SecurityContextHolder;
12 import org.springframework.security.core.userdetails.User;
13 import org.springframework.stereotype.Component;
14 import org.springframework.web.bind.annotation.RequestMapping;
15 
16 import javax.servlet.http.HttpServletRequest;
17 import java.lang.reflect.Method;
18 import java.util.Date;
19 
20 @Component
21 @Aspect
22 public class LogAop {
23 
24     @Autowired
25     private HttpServletRequest request;
26 
27     @Autowired
28     private ISysLogService sysLogService;
29 
30     private Date visitTime; // 最先时候
31     private Class clazz;  // 接见的类
32     private Method method; // 接见的要领
33 
34     // 前置关照 重要猎取最先时候,实行的类哪个,实行的哪个要领
35     @Before("execution(* club.nipengfei.ssm.controller.*.*(..))")
36     public void doBefore(JoinPoint jp) throws NoSuchMethodException {
37         visitTime = new Date();  // 当前时候就是最先接见的类
38         clazz = jp.getTarget().getClass(); // 详细接见的类
39         String methodName = jp.getSignature().getName();  // 猎取接见要领称号
40         Object[] args = jp.getArgs();  // 猎取接见要领参数
41 
42         // 猎取详细实行要领Method对象
43         if (args==null || args.length==0){
44             method =  clazz.getMethod(methodName);
45         } else {
46             Class[] classArgs = new Class[args.length];
47             for (int i=0;i<args.length;i++){
48                 classArgs[i] = args[i].getClass();
49             }
50             method = clazz.getMethod(methodName,classArgs);
51         }
52     }
53 
54     // 后置关照
55     @After("execution(* club.nipengfei.ssm.controller.*.*(..))")
56     public void doAfter(JoinPoint jp) throws Exception {
57 
58         // 猎取接见时长
59         long time = new Date().getTime()-visitTime.getTime();
60 
61         String url = "";
62         // 猎取url
63         if (clazz != null && method !=null && clazz!=LogAop.class){
64             // 猎取类的@RequestMapping("/orders")
65             RequestMapping clazzAnnotation =(RequestMapping) clazz.getAnnotation(RequestMapping.class);
66             if (clazzAnnotation != null){
67                 String[] classValue = clazzAnnotation.value();
68                 // 猎取要领上的@RequestMapping("xxx")
69                 RequestMapping methodAnnotation = method.getAnnotation(RequestMapping.class);
70                 if (methodAnnotation != null){
71                     String[] methodValue = methodAnnotation.value();
72                     url=classValue[0]+methodValue[0];
73 
74                     // 猎取IP地点
75                     String ip = request.getRemoteAddr();
76 
77                     // 猎取当前操纵的用户
78                     SecurityContext context = SecurityContextHolder.getContext();
79                     User user =(User) context.getAuthentication().getPrincipal();
80                     String username = user.getUsername();
81 
82                     // 将日记相干信息封装到SysLog对象
83                     SysLog sysLog = new SysLog();
84                     sysLog.setExecutionTime(time);
85                     sysLog.setIp(ip);
86                     sysLog.setMethod("[类名] "+clazz.getName()+"[要领名] "+method.getName());
87                     sysLog.setUrl(url);
88                     sysLog.setUsername(username);
89                     sysLog.setVisitTime(visitTime);
90 
91                     // 挪用service完成操纵
92                     sysLogService.save(sysLog);
93                 }
94             }
95 
96         }
97     }
98 }

1.3在service.impl包下新建一个SysLogServiceImpl类,生成一个save要领将SysLog类对象放到数据表中

 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 }

1.4在dao包下新建ISysLogDao接口

 1 package club.nipengfei.ssm.dao;
 2 
 3 import club.nipengfei.ssm.domain.SysLog;
 4 import org.apache.ibatis.annotations.Insert;
 5 import org.apache.ibatis.annotations.Select;
 6 
 7 import java.util.List;
 8 
 9 public interface ISysLogDao {
10 
11     @Insert("insert into syslog(visitTime,username,ip,url,executionTime,method) values(#{visitTime},#{username},#{ip},#{url},#{executionTime},#{method})")
12     public void save(SysLog sysLog) throws Exception;
13 
14 }

1.5存在的题目

当点击定单治理时发明,不能一般接见

检察材料发明由于该findAll要领传入的形参是int范例,而我们的切面类经由过程反射猎取该类的要领时传入的参数Integer范例。将findAll要领的int改成Integer,发明能一般接见了。

2.将日记信息展现到页面上

流程剖析图:

 

2.1在ISysLogDao接口中生成一个findAll要领

 1 package club.nipengfei.ssm.dao;
 2 
 3 import club.nipengfei.ssm.domain.SysLog;
 4 import org.apache.ibatis.annotations.Insert;
 5 import org.apache.ibatis.annotations.Select;
 6 
 7 import java.util.List;
 8 
 9 public interface ISysLogDao {
10 
11     @Insert("insert into syslog(visitTime,username,ip,url,executionTime,method) values(#{visitTime},#{username},#{ip},#{url},#{executionTime},#{method})")
12     public void save(SysLog sysLog) throws Exception;
13 
14     @Select("select * from sysLog")
15     List<SysLog> 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<SysLog> 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<SysLog> sysLogList = sysLogService.findAll();
23         mv.addObject("sysLogs",sysLogList);
24         mv.setViewName("syslog-list");
25         return mv;
26     }
27 }

 

  选择打赏方式
微信赞助

打赏

QQ钱包

打赏

支付宝赞助

打赏

  移步手机端
数据背景治理(五)AOP日记

1、打开你手机的二维码扫描APP
2、扫描左则的二维码
3、点击扫描获得的网址
4、可以在手机端阅读此文章
未定义标签

本文来源:搜奇网

本文地址:https://www.sou7.cn/282264.html

关注我们:微信搜索“搜奇网”添加我为好友

版权声明: 本文仅代表作者个人观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。请记住本站网址https://www.sou7.cn/搜奇网。

发表评论

选填

必填

必填

选填

请拖动滑块解锁
>>