发布时间:2023-05-14 18:00
**
**
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;
}
dropout层_【深度学习理论】一文搞透Dropout、L1L2正则化/权重衰减
初入微前端---qiankun学习这一篇就够了,并在vite中使用qiankun
Halcon实例转OpenCvSharp(C# OpenCV)实现--瓶口缺陷检测(附源码)
互联网晚报 | 5月13日 星期五 | 罗永浩回应被叫行业冥灯;新一轮汽车下乡政策最快将于本月出台;字节跳动鲸鲮操作系统获批...
matlab求两个离散数据的相关性,matlab求两个序列的相关性_matlab求相关性_两个序列的相关性...
[Hacker News 周报] 高性能 JS 运行时 Bun;Electron 轻量级替代品;自动探测 Web 应用内存泄漏
void* data 数据类型参数以及void *data[ ]解释
scare机器人如何手眼标定_SCARA机器人手眼标定之目标抓取