spring boot 自定义Log注解简单版

发布时间:2023-05-14 18:00

**

需求是:简单记录用户的操作日志,注意:只看方法不要copy 手打无法执行

**
1.在yml中将spring-aop-auto:true;注意:操作日志类(optertorLog)和opterLogServiceHandler的类都已经创建好 ;本文章已省略;
2,创建Log类

package com.common.annotation;
 
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
 
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Log {
    String value() default "";
    String logType() default "";

3,创建LogAspect 日志切面类

import java.lang.reflect.Method;
import java.util.Date;
 
import javax.servlet.http.HttpServletRequest;
 
import org.apache.shiro.SecurityUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
import org.springframework.stereotype.Component;
 
import com.fasterxml.jackson.databind.ObjectMapper;
i
 
@Aspect
@Component
public class LogAspect {
 
	@Autowired
	private OpterLogServiceHandler opterLogServiceHandler;
 
 	@Pointcut("@annotation(com.common.annotation.Log)")
	public void Logpointcut() {
	}
 
	@Around("Logpointcut()")
	public Object around(ProceedingJoinPoint point) {
		Object result = null;
		long beginTime = System.currentTimeMillis();
		try {
		//获取注解中的value值
			result = point.proceed();
			MethodSignature signature=(MethodSignature )point.getSignature();
			Method method=signature.getMethod();
			Log aoplog=method,getAnnotation(Log.class);
			//获取request
			RequestAttributes requestAttributes=RequestContextHolder.getReqiestAttrinites();
			HttpServletRequest request=requestAttributes.getReqiest();
			//获取用户 自定义set的 
			TicketUser ticket=request.getSession().getAttribute(TicketUser.session_tick_key);
			//保存业务类 optertorLog
			OptertorLog optertorLog=new OptertorLog();
			....optertorLog.set..
			....optertorLog.set..
			...optertorLog.set..
			opterLogServiceHandler.save(optertorLog);

		} catch (Throwable e) {
			e.printStackTrace();
			LogAspect .log.info("日志切面报错!!"+e.getMessage());
			
		}
				return result;
	}
 //看业务中是否需要记录操作人的ip地址 业务逻辑需求
 //获取Ip
 private String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
            // 多次反向代理后会有多个ip值,第一个ip才是真实ip
            if( ip.indexOf(",")!=-1 ){
                ip = ip.split(",")[0];
            }
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("X-Real-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

ItVuer - 免责声明 - 关于我们 - 联系我们

本网站信息来源于互联网,如有侵权请联系:561261067@qq.com

桂ICP备16001015号