Java SSM 项目实战 day09 SSMAOP日志
Posted 蓝盒子bluebox
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java SSM 项目实战 day09 SSMAOP日志相关的知识,希望对你有一定的参考价值。
一、数据库与表结构
1、日志表信息描述sysLog
2、sql语句
CREATE TABLE sysLog(
id VARCHAR2(32) default SYS_GUID() PRIMARY KEY,
visitTime timestamp,
username VARCHAR2(50),
ip VARCHAR2(30),
url VARCHAR2(50),
executionTime int,
method VARCHAR2(200)
)
3、创建对应的实体类
package com.itzheng.ssm.domain;
import java.util.Date;
public class SysLog {
private String id;
private Date visitTime;
private String visitTimeStr;
private String username;
private String ip;
private String url;
private Long executionTime;
private String method;
public SysLog() {
}
public SysLog(String id, Date visitTime, String visitTimeStr, String username, String ip, String url, Long executionTime, String method) {
this.id = id;
this.visitTime = visitTime;
this.visitTimeStr = visitTimeStr;
this.username = username;
this.ip = ip;
this.url = url;
this.executionTime = executionTime;
this.method = method;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Date getVisitTime() {
return visitTime;
}
public void setVisitTime(Date visitTime) {
this.visitTime = visitTime;
}
public String getVisitTimeStr() {
return visitTimeStr;
}
public void setVisitTimeStr(String visitTimeStr) {
this.visitTimeStr = visitTimeStr;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public Long getExecutionTime() {
return executionTime;
}
public void setExecutionTime(Long executionTime) {
this.executionTime = executionTime;
}
public String getMethod() {
return method;
}
public void setMethod(String method) {
this.method = method;
}
@Override
public String toString() {
return "SysLog{" +
"id='" + id + '\\'' +
", visitTime=" + visitTime +
", visitTimeStr='" + visitTimeStr + '\\'' +
", username='" + username + '\\'' +
", ip='" + ip + '\\'' +
", url='" + url + '\\'' +
", executionTime=" + executionTime +
", method='" + method + '\\'' +
'}';
}
}
二、基于AOP日志处理(记录日志信息)
1、在控制层创建LogAop类
修改web.xml,添加一下内容
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
LogAop类
package com.itzheng.ssm.controller;
import com.itzheng.ssm.domain.SysLog;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Date;
@Component
@Aspect
public class LogAop {
@Autowired
private HttpServletRequest requset;
private Date visitTime;//开始的时间
private Class clazz;//访问的类
private Method method;//访问的方法
//前置通知 主要是获取我们的开始的时间,执行类的哪一个,执行的是哪一个方法
@Before("execution(* com.itzheng.ssm.controller.*.*(..))")//拦截controller类当中的所有的方法
public void doBefore(JoinPoint jp) throws NoSuchMethodException {
visitTime = new Date();//当前时间就是开始访问的时间
clazz = jp.getTarget().getClass();//获取当前具体要访问的类
String methodName = jp.getSignature().getName();//获取当前访问具体方法的名称
Object[] args = jp.getArgs();//获取访问方法的参数
//以下代码获取到了具体执行的方法的method对象
if(args == null || args.length == 0){
method = clazz.getMethod(methodName);//只能获取无参数的方法
}else {
Class[] classArgs = new Class[args.length];
for (int i = 0; i < args.length;i++){
classArgs[i] = args[i].getClass();//获取每一个方法参数,放到classArgs数组当中
}
method = clazz.getMethod(methodName,classArgs);//只能获取无参数的方法
}
}
//后置通知
@After("execution(* com.itzheng.ssm.controller.*.*(..))")//拦截controller类当中的所有的方法
public void doAfter(JoinPoint jp){
long time = new Date().getTime()-visitTime.getTime();//获取了访问的时长
String url = "";
//获取url
if( clazz != null && method != null && clazz != LogAop.class){
//1.获取类上的这个注解@RequestMapping("/orders")的值
RequestMapping classAnnotation = (RequestMapping)clazz.getAnnotation(RequestMapping.class);
if(classAnnotation != null){
String[] classValue = classAnnotation.value();//获取到了/orders
//2、获取方法上的@RequestMapping(xxx)
RequestMapping methodAnnotation = method.getAnnotation(RequestMapping.class);
if(methodAnnotation != null){
String [] methodValue = methodAnnotation.value();
url = classValue[0] + methodValue[0];
}
}
}
//获取访问的IP地址
String ip = requset.getRemoteAddr();
//如何获取当前操作的用户
SecurityContext context = SecurityContextHolder.getContext();//从上下文当中获取当前登录的用户
User user = (User)context.getAuthentication().getPrincipal();
String username = user.getUsername();
//将日志相关信息分装到SysLog对象当中
SysLog sysLog = new SysLog();
sysLog.setExecutionTime(time);//执行时长
sysLog.setIp(ip);
sysLog.setMethod("[类名] "+clazz.getName()+"[方法名] " + method.getName());
sysLog.setUrl(url);
sysLog.setUsername(username);
sysLog.setVisitTime(visitTime);
//调用Service完成数据库的插入操作(即日志的记录操作)
}
}
2、创建对应的Service接口和对应的实现类
(1)ISysLogService接口
package com.itzheng.ssm.service;
import com.itzheng.ssm.domain.SysLog;
public interface ISysLogService {
public void save(SysLog sysLog) throws Exception;
}
(2)SysLogServiceImpl
package com.itzheng.ssm.service.impl;
import com.itzheng.ssm.domain.SysLog;
import com.itzheng.ssm.service.ISysLogService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional
public class SysLogServiceImpl implements ISysLogService {
@Override
public void save(SysLog sysLog) throws Exception {
}
}
3、创建对应的DAO
package com.itzheng.ssm.dao;
import com.itzheng.ssm.domain.SysLog;
import org.apache.ibatis.annotations.Insert;
public interface ISysLogDao {
@Insert("")
public void save(SysLog sysLog) throws Exception ;
}
4、完善上述所有的业务以及dao
(1)SysLogServiceImpl
package com.itzheng.ssm.service.impl;
import com.itzheng.ssm.dao.ISysLogDao;
import com.itzheng.ssm.domain.SysLog;
import com.itzheng.ssm.service.ISysLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional
public class SysLogServiceImpl implements ISysLogService {
@Autowired
private ISysLogDao sysLogDao;
@Override
public void save(SysLog sysLog) throws Exception {
sysLogDao.save(sysLog);
}
}
(2)LogAop
@Autowired
private ISysLogService sysLogService;
//调用Service完成数据库的插入操作(即日志的记录操作)
sysLogService.save(sysLog);
(3)ISysLogDao
package com.itzheng.ssm.dao;
import com.itzheng.ssm.domain.SysLog;
import org.apache.ibatis.annotations.Insert;
public interface ISysLogDao {
@Insert("insert into syslog(visitTime,username,ip,url,executionTime,method) values(#{visitTime},#{username},#{ip},#{url},#{executionTime},#{method})")
public void save(SysLog sysLog) throws Exception ;
}
(4)完善LogAop类
package com.itzheng.ssm.controller;
import com.itzheng.ssm.domain.SysLog;
import com.itzheng.ssm.service.ISysLogService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMap以上是关于Java SSM 项目实战 day09 SSMAOP日志的主要内容,如果未能解决你的问题,请参考以下文章
Java SSM 项目实战 day07 SpringSecurity源码分析
Java SSM 项目实战 day06 角色操作,资源权限操作