parent
8bb39a6431
commit
c7725095b7
7 changed files with 270 additions and 51 deletions
@ -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<String> permitUrl; |
||||
|
||||
@Value("${auth.openInterceptStr}") |
||||
public void setOpenInterceptStr(String openInterceptStr) { |
||||
AuthConfig.openInterceptStr = openInterceptStr; |
||||
} |
||||
|
||||
@Value("${auth.permitUrl}") |
||||
public void setPermitUrl(List<String> permitUrl) { |
||||
AuthConfig.permitUrl = permitUrl; |
||||
} |
||||
|
||||
@Bean |
||||
public AuthInterceptor initAuthInterceptor(){ |
||||
return new AuthInterceptor(); |
||||
} |
||||
|
||||
@Override |
||||
public void addInterceptors(InterceptorRegistry registry) { |
||||
registry.addInterceptor(initAuthInterceptor()).addPathPatterns(openInterceptStr) |
||||
.excludePathPatterns(permitUrl); |
||||
} |
||||
|
||||
} |
||||
//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<String> permitUrl;
|
||||
//
|
||||
// @Value("${auth.openInterceptStr}")
|
||||
// public void setOpenInterceptStr(String openInterceptStr) {
|
||||
// AuthConfig.openInterceptStr = openInterceptStr;
|
||||
// }
|
||||
//
|
||||
// @Value("${auth.permitUrl}")
|
||||
// public void setPermitUrl(List<String> permitUrl) {
|
||||
// AuthConfig.permitUrl = permitUrl;
|
||||
// }
|
||||
//
|
||||
// @Bean
|
||||
// public AuthInterceptor initAuthInterceptor(){
|
||||
// return new AuthInterceptor();
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public void addInterceptors(InterceptorRegistry registry) {
|
||||
// registry.addInterceptor(initAuthInterceptor()).addPathPatterns(openInterceptStr)
|
||||
// .excludePathPatterns(permitUrl);
|
||||
// }
|
||||
//
|
||||
//}
|
@ -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; |
||||
} |
||||
} |
@ -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; |
||||
|
||||
} |
@ -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 ""; |
||||
} |
@ -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; |
||||
} |
||||
} |
@ -0,0 +1,44 @@ |
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<configuration debug="false"> |
||||
|
||||
<!--定义日志文件的存储地址 勿在 LogBack 的配置中使用相对路径--> |
||||
<property name="LOG_HOME" value="../log" /> |
||||
|
||||
<!--控制台日志, 控制台输出 --> |
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> |
||||
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> |
||||
<!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度,%msg:日志消息,%n是换行符--> |
||||
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern> |
||||
</encoder> |
||||
</appender> |
||||
|
||||
<!--文件日志, 按照每天生成日志文件 --> |
||||
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> |
||||
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> |
||||
<!--日志文件输出的文件名--> |
||||
<FileNamePattern>${LOG_HOME}/%d{yyyy-MM-dd}.log</FileNamePattern> |
||||
<!--日志文件保留天数--> |
||||
<!-- <MaxHistory>30</MaxHistory>--> |
||||
</rollingPolicy> |
||||
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> |
||||
<!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符--> |
||||
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern> |
||||
</encoder> |
||||
<!--日志文件最大的大小--> |
||||
<!-- <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">--> |
||||
<!-- <MaxFileSize>10MB</MaxFileSize>--> |
||||
<!-- </triggeringPolicy>--> |
||||
</appender> |
||||
|
||||
<!--myibatis log configure--> |
||||
<logger name="com.apache.ibatis" level="TRACE"/> |
||||
<logger name="java.sql.Connection" level="DEBUG"/> |
||||
<logger name="java.sql.Statement" level="DEBUG"/> |
||||
<logger name="java.sql.PreparedStatement" level="DEBUG"/> |
||||
|
||||
<!-- 日志输出级别 --> |
||||
<root level="INFO"> |
||||
<appender-ref ref="STDOUT" /> |
||||
<appender-ref ref="FILE"/> |
||||
</root> |
||||
</configuration> |
Loading…
Reference in new issue