From c7725095b7dbe31df833877759393ae9405ae534 Mon Sep 17 00:00:00 2001 From: chendengyao <1171077218@qq.com> Date: Wed, 16 Jun 2021 17:19:56 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=93=8D=E4=BD=9C=E6=97=A5?= =?UTF-8?q?=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/yipin/liuwanr/config/AuthConfig.java | 102 +++++++++--------- .../com/yipin/liuwanr/config/LogAspect.java | 98 +++++++++++++++++ .../com/yipin/liuwanr/entity/Response.java | 9 ++ .../java/com/yipin/liuwanr/entity/SysLog.java | 31 ++++++ .../yipin/liuwanr/entity/annotation/Log.java | 10 ++ .../java/com/yipin/liuwanr/util/IPUtils.java | 27 +++++ src/main/resources/logback.xml | 44 ++++++++ 7 files changed, 270 insertions(+), 51 deletions(-) create mode 100644 src/main/java/com/yipin/liuwanr/config/LogAspect.java create mode 100644 src/main/java/com/yipin/liuwanr/entity/SysLog.java create mode 100644 src/main/java/com/yipin/liuwanr/entity/annotation/Log.java create mode 100644 src/main/java/com/yipin/liuwanr/util/IPUtils.java create mode 100644 src/main/resources/logback.xml diff --git a/src/main/java/com/yipin/liuwanr/config/AuthConfig.java b/src/main/java/com/yipin/liuwanr/config/AuthConfig.java index a037081..ba8b02f 100644 --- a/src/main/java/com/yipin/liuwanr/config/AuthConfig.java +++ b/src/main/java/com/yipin/liuwanr/config/AuthConfig.java @@ -1,51 +1,51 @@ -package com.yipin.liuwanr.config; - - - -import com.yipin.liuwanr.filter.AuthInterceptor; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.web.servlet.config.annotation.InterceptorRegistry; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; - -import java.util.List; - -/** - * @description - * @author: Mr.JK - * @create: 2021-06-03 16:24 - **/ -@Configuration -@ConfigurationProperties(prefix = "auth") -public class AuthConfig implements WebMvcConfigurer { - - //开启拦截字符串 - public static String openInterceptStr; - - //放行url - public static List permitUrl; - - @Value("${auth.openInterceptStr}") - public void setOpenInterceptStr(String openInterceptStr) { - AuthConfig.openInterceptStr = openInterceptStr; - } - - @Value("${auth.permitUrl}") - public void setPermitUrl(List permitUrl) { - AuthConfig.permitUrl = permitUrl; - } - - @Bean - public AuthInterceptor initAuthInterceptor(){ - return new AuthInterceptor(); - } - - @Override - public void addInterceptors(InterceptorRegistry registry) { - registry.addInterceptor(initAuthInterceptor()).addPathPatterns(openInterceptStr) - .excludePathPatterns(permitUrl); - } - -} \ No newline at end of file +//package com.yipin.liuwanr.config; +// +// +// +//import com.yipin.liuwanr.filter.AuthInterceptor; +//import org.springframework.beans.factory.annotation.Value; +//import org.springframework.boot.context.properties.ConfigurationProperties; +//import org.springframework.context.annotation.Bean; +//import org.springframework.context.annotation.Configuration; +//import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +//import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; +// +//import java.util.List; +// +///** +// * @description +// * @author: Mr.JK +// * @create: 2021-06-03 16:24 +// **/ +////@Configuration +////@ConfigurationProperties(prefix = "auth") +//public class AuthConfig implements WebMvcConfigurer { +// +// //开启拦截字符串 +// public static String openInterceptStr; +// +// //放行url +// public static List permitUrl; +// +// @Value("${auth.openInterceptStr}") +// public void setOpenInterceptStr(String openInterceptStr) { +// AuthConfig.openInterceptStr = openInterceptStr; +// } +// +// @Value("${auth.permitUrl}") +// public void setPermitUrl(List permitUrl) { +// AuthConfig.permitUrl = permitUrl; +// } +// +// @Bean +// public AuthInterceptor initAuthInterceptor(){ +// return new AuthInterceptor(); +// } +// +// @Override +// public void addInterceptors(InterceptorRegistry registry) { +// registry.addInterceptor(initAuthInterceptor()).addPathPatterns(openInterceptStr) +// .excludePathPatterns(permitUrl); +// } +// +//} \ No newline at end of file diff --git a/src/main/java/com/yipin/liuwanr/config/LogAspect.java b/src/main/java/com/yipin/liuwanr/config/LogAspect.java new file mode 100644 index 0000000..bdd188e --- /dev/null +++ b/src/main/java/com/yipin/liuwanr/config/LogAspect.java @@ -0,0 +1,98 @@ +package com.yipin.liuwanr.config; + + +import com.yipin.liuwanr.entity.SysLog; +import com.yipin.liuwanr.entity.annotation.Log; +import com.yipin.liuwanr.util.IPUtils; +import com.yipin.liuwanr.util.UserIdUtils; +import lombok.extern.slf4j.Slf4j; +import org.aspectj.lang.JoinPoint; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Before; +import org.aspectj.lang.annotation.Pointcut; +import org.aspectj.lang.reflect.MethodSignature; +import org.jboss.logging.Logger; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.LocalVariableTableParameterNameDiscoverer; +import org.springframework.stereotype.Component; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; + +import javax.servlet.http.HttpServletRequest; +import java.lang.reflect.Method; +import java.text.SimpleDateFormat; +import java.util.Date; + +/** + * + * @author cdy + */ + +@Aspect +@Component +@Slf4j +public class LogAspect { + + @Autowired + UserIdUtils userIdUtils; + @Autowired + HttpServletRequest request; + + @Pointcut("@annotation(com.yipin.liuwanr.entity.annotation.Log)") + public void pointcut() { } + + @Around("pointcut()") + public Object around(ProceedingJoinPoint point) { + Object result = null; + long beginTime = System.currentTimeMillis(); + try { + // 执行方法 + result = point.proceed(); + } catch (Throwable e) { + e.printStackTrace(); + } + // 执行时长(毫秒) + long time = System.currentTimeMillis() - beginTime; + + MethodSignature signature = (MethodSignature) point.getSignature(); + Method method = signature.getMethod(); + SysLog sysLog = new SysLog(); + Log logAnnotation = method.getAnnotation(Log.class); + if (logAnnotation != null) { + // 注解上的描述 + sysLog.setOperation(logAnnotation.value()); + } + // 请求的方法名 + String className = point.getTarget().getClass().getName(); + String methodName = signature.getName(); + sysLog.setMethod(className + "." + methodName + "()"); + // 请求的方法参数值 + Object[] args = point.getArgs(); + // 请求的方法参数名称 + LocalVariableTableParameterNameDiscoverer u = new LocalVariableTableParameterNameDiscoverer(); + String[] paramNames = u.getParameterNames(method); + if (args != null && paramNames != null) { + String params = ""; + for (int i = 0; i < args.length; i++) { + params += " " + paramNames[i] + ": " + args[i]; + } + sysLog.setParams(params); + } + // 设置IP地址 + sysLog.setIp(IPUtils.getIpAddress(request)); + // 设置用户Id + sysLog.setUsername(userIdUtils.getUserIdByToken().toString()); + sysLog.setTime((int) time); + + SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + sysLog.setCreateTime(simpleDateFormat.format(new Date())); + + sysLog.setResult(result.toString()); + // 输出日志 + log.info(sysLog.toString()); + + return result; + } +} diff --git a/src/main/java/com/yipin/liuwanr/entity/Response.java b/src/main/java/com/yipin/liuwanr/entity/Response.java index 59c7570..8162fd1 100644 --- a/src/main/java/com/yipin/liuwanr/entity/Response.java +++ b/src/main/java/com/yipin/liuwanr/entity/Response.java @@ -31,4 +31,13 @@ public class Response { public void setErrmessage(String errmessage) { this.errmessage = errmessage; } + + @Override + public String toString() { + return "Response{" + + "status=" + status + + ", message=" + message + + ", errmessage='" + errmessage + '\'' + + '}'; + } } diff --git a/src/main/java/com/yipin/liuwanr/entity/SysLog.java b/src/main/java/com/yipin/liuwanr/entity/SysLog.java new file mode 100644 index 0000000..ab84887 --- /dev/null +++ b/src/main/java/com/yipin/liuwanr/entity/SysLog.java @@ -0,0 +1,31 @@ +package com.yipin.liuwanr.entity; + +import com.fasterxml.jackson.annotation.JsonFormat; +import lombok.Data; +import lombok.ToString; +import org.springframework.format.annotation.DateTimeFormat; + +import java.util.Date; + +@Data +@ToString +public class SysLog { + + //用户名 + private String username; + //操作 + private String operation; + //执行时间 + private Integer time; + //请求方法名 + private String method; + //请求参数 + private String params; + //Ip地址 + private String ip; + //创建时间 + private String createTime; + //返回结果 + private String result; + +} diff --git a/src/main/java/com/yipin/liuwanr/entity/annotation/Log.java b/src/main/java/com/yipin/liuwanr/entity/annotation/Log.java new file mode 100644 index 0000000..d20acd0 --- /dev/null +++ b/src/main/java/com/yipin/liuwanr/entity/annotation/Log.java @@ -0,0 +1,10 @@ +package com.yipin.liuwanr.entity.annotation; + +import java.lang.annotation.*; + +@Documented +@Target(ElementType.METHOD) +@Retention(RetentionPolicy.RUNTIME) +public @interface Log { + String value() default ""; +} diff --git a/src/main/java/com/yipin/liuwanr/util/IPUtils.java b/src/main/java/com/yipin/liuwanr/util/IPUtils.java new file mode 100644 index 0000000..aeb18d2 --- /dev/null +++ b/src/main/java/com/yipin/liuwanr/util/IPUtils.java @@ -0,0 +1,27 @@ +package com.yipin.liuwanr.util; + +import javax.servlet.http.HttpServletRequest; + +public class IPUtils { + + + public static String getIpAddress(HttpServletRequest request) { + String ip = request.getHeader("x-forwarded-for"); + 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.getRemoteAddr(); + } + return ip; + } +} \ No newline at end of file diff --git a/src/main/resources/logback.xml b/src/main/resources/logback.xml new file mode 100644 index 0000000..00f9bce --- /dev/null +++ b/src/main/resources/logback.xml @@ -0,0 +1,44 @@ + + + + + + + + + + + %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n + + + + + + + + ${LOG_HOME}/%d{yyyy-MM-dd}.log + + + + + + %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n + + + + + + + + + + + + + + + + + + + \ No newline at end of file