首页>代码>基于SSM的技术博客系统>/Art_Blog/src/main/java/com/java214/aop/SystemLogAspect.java
package com.java214.aop;

import java.lang.reflect.Method;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.java214.annotation.SystemLog;
import com.java214.model.Log;
import com.java214.service.LogService;
import com.java214.util.UserIpUtil;

@Aspect
@Component
public class SystemLogAspect {
	// 注入Service用于把日志保存数据库
	@Resource
	private LogService logService;

	// Service层切点
	@Pointcut("@annotation(com.java214.annotation.SystemLog)")
	public void serviceAspect() {
	}

	@After("serviceAspect()")
	public void doServiceLog(JoinPoint joinPoint) {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes()).getRequest();
		try {
			// 数据库日志
			Log log = new Log();
			// 获取日志描述信息
			
			log.setUserType(getUserType(joinPoint));
			log.setIp(UserIpUtil.getIp(request));
			log.setParam(joinPoint.getArgs()[0].toString());
			log.setDescription(getDescription(joinPoint));
			log.setAddtime(new Date());
			if(!log.getParam().equals("none")){
				logService.insert(log);
			}
			
		} catch (Exception e) {
		}
	}

	/**
	 * 获取注解中对方法的描述信息 用于service层注解
	 * 
	 * @param joinPoint
	 *            切点
	 * @return 方法描述
	 * @throws Exception
	 */
	public static String getDescription(JoinPoint joinPoint)
			throws Exception {
		String targetName = joinPoint.getTarget().getClass().getName();
		String methodName = joinPoint.getSignature().getName();
		Object[] arguments = joinPoint.getArgs();
		Class<?> targetClass = Class.forName(targetName);
		Method[] methods = targetClass.getMethods();
		String description = "";
		for (Method method : methods) {
			if (method.getName().equals(methodName)) {
				Class[] clazzs = method.getParameterTypes();
				if (clazzs.length == arguments.length) {
					description = method.getAnnotation(SystemLog.class)
							.description();
					break;
				}
			}
		}
		return description;
	}
	
	/**
	 * 获取注解中的用户信息 用于service层注解
	 * 
	 * @param joinPoint
	 *            切点
	 * @return 方法描述
	 * @throws Exception
	 */
	public static String getUserType(JoinPoint joinPoint)
			throws Exception {
		String targetName = joinPoint.getTarget().getClass().getName();
		String methodName = joinPoint.getSignature().getName();
		Object[] arguments = joinPoint.getArgs();
		Class<?> targetClass = Class.forName(targetName);
		Method[] methods = targetClass.getMethods();
		String userType = "";
		for (Method method : methods) {
			if (method.getName().equals(methodName)) {
				Class[] clazzs = method.getParameterTypes();
				if (clazzs.length == arguments.length) {
					userType = method.getAnnotation(SystemLog.class)
							.userType();
					break;
				}
			}
		}
		return userType;
	}
	
}
最近下载更多
TY0165  LV20 2024年6月21日
lpwgzy  LV5 2023年10月9日
全栈小白  LV35 2023年9月11日
yunYUN123  LV1 2023年5月18日
tyyeng  LV18 2022年12月27日
利民提提  LV3 2022年11月20日
lilong007  LV23 2022年10月21日
cyx123456  LV6 2022年8月13日
getset  LV8 2022年6月26日
zhangjilu  LV18 2022年6月11日
最近浏览更多
xiaoaitx  LV8 1月2日
萌了个乖乖  LV12 2024年12月26日
三秋桂子  LV1 2024年12月22日
ma406805131  LV19 2024年12月18日
krispeng  LV14 2024年11月20日
897258976  LV8 2024年8月29日
TY0165  LV20 2024年6月21日
mc152921 2024年5月29日
暂无贡献等级
yellow_flowers  LV8 2024年4月26日
yqyqyqyqy  LV4 2024年4月24日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友