基于SSM的日志类
数据库
domain
package com.itheima.ssm.domain;
import com.itheima.ssm.utils.DateUtils;
import java.util.Date;
/**
* 日之类
*/
public class SysLog {
private int id; //主键
private Date visitTime; //访问时间
private String visitTimeStr;
private String username; //用户名
private String ip; //ip
private String url; //访问资源url
private Long executionTime;//执行时间
private String method; //访问方法
public String getVisitTimeStr() {
if(visitTime!=null){
visitTimeStr= DateUtils.date2String(visitTime,"yyyy-MM-dd HH:mm");
}
return visitTimeStr;
}
public void setVisitTimeStr(String visitTimeStr) {
this.visitTimeStr = visitTimeStr;
}
@Override
public String toString() {
return "SysLog{" +
"id=" + id +
", visitTime=" + visitTime +
", visitTimeStr='" + visitTimeStr + '\'' +
", username='" + username + '\'' +
", ip='" + ip + '\'' +
", url='" + url + '\'' +
", executionTime=" + executionTime +
", method='" + method + '\'' +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Date getVisitTime() {
return visitTime;
}
public void setVisitTime(Date visitTime) {
this.visitTime = visitTime;
}
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;
}
}
dao
package com.itheima.ssm.dao;
import com.itheima.ssm.domain.SysLog;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository("sysLogDao")
public interface ISysLogDao {
@Insert("insert into sysLog (visitTime,username,ip,url,executionTime,method) values(#{visitTime},#{username},#{ip},#{url},#{executionTime},#{method})")
void save(SysLog sysLog) throws Exception;
@Select("select * from sysLog")
List<SysLog> findAll() throws Exception;
}
IService
package com.itheima.ssm.service;
import com.itheima.ssm.domain.SysLog;
import java.util.List;
public interface ISysLogService {
//添加进日志
void save(SysLog sysLog) throws Exception;
//查询所有日志
List<SysLog> findAll() throws Exception;
}
ServiceImpl
package com.itheima.ssm.service.impl;
import com.itheima.ssm.dao.ISysLogDao;
import com.itheima.ssm.domain.SysLog;
import com.itheima.ssm.service.ISysLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service("sysLogService")
public class SysLogServiceImpl implements ISysLogService {
@Autowired
private ISysLogDao sysLogDao;
@Override
public void save(SysLog sysLog) throws Exception {
sysLogDao.save(sysLog);
}
@Override
public List<SysLog> findAll() throws Exception {
return sysLogDao.findAll();
}
}
AOP
package com.itheima.ssm.controller;
import com.itheima.ssm.domain.SysLog;
import com.itheima.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.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.reflect.Method;
import java.util.Date;
/**
* 日志类
*/
@Component
@Aspect
public class LogAop {
/*在web.xml中配置了RequestContextListener的监听器之后就可以在这里注入reqeust对象来获取访问者的ip*/
@Autowired
private HttpServletRequest request;
@Autowired
private ISysLogService sysLogService;
private Date visitTime;//开始访问时间
private Class clazz; //访问的类
private Method method; //访问的方法
private String url; //访问的资源路径url
//前置通知
@Before("execution(* com.itheima.ssm.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();
}
clazz.getMethod(methodName,classArgs);
}
}
//后置通知
@After("execution(* com.itheima.ssm.controller.*.*(..))")
public void doAfter(JoinPoint jp) throws Exception {
long time = new Date().getTime()-visitTime.getTime();//获取访问时长
//获取url
if(clazz!=null&&method!=null&&clazz!=LogAop.class){
//1.获取类上的的@RequestMapping
RequestMapping classAnnotation = (RequestMapping) clazz.getAnnotation(RequestMapping.class);
if(classAnnotation!=null){
String[] classValue = classAnnotation.value();
//2.获取方法上的@RequestMapping
RequestMapping methodAnnotation = method.getAnnotation(RequestMapping.class);
if (methodAnnotation!=null){
String[] methodValue = methodAnnotation.value();
url=classValue[0]+methodValue[0];
//获取ip
String ip = request.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将数据保存进数据库
sysLogService.save(sysLog);
}
}
}
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!-- 配置加载类路径的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml,classpath*:spring-security.xml</param-value>
</context-param>
<!-- 配置监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<!-- 前端控制器(加载classpath:springmvc.xml 服务器启动创建servlet) -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置初始化参数,创建完DispatcherServlet对象,加载springmvc.xml配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!-- 服务器启动的时候,让DispatcherServlet对象创建 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- 解决中文乱码过滤器 -->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--用户登录权限认证-->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<error-page>
<error-code>403</error-code>
<location>/403.jsp</location>
</error-page>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
Controller
package com.itheima.ssm.controller;
import com.itheima.ssm.domain.SysLog;
import com.itheima.ssm.service.ISysLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
@Controller
@RequestMapping("/sysLog")
public class SysLogController {
@Autowired
private ISysLogService sysLogService;
@RequestMapping("/findAll.do")
public ModelAndView findAll() throws Exception{
ModelAndView mv = new ModelAndView();
List<SysLog> sysLogs = sysLogService.findAll();
mv.addObject("sysLogs",sysLogs);
mv.setViewName("syslog-list");
return mv;
}
}