mirror of
https://gitee.com/youlaitech/youlai-mall.git
synced 2024-12-23 05:00:25 +08:00
feat:AOP实现操作记录日志并上传至Redis
This commit is contained in:
parent
ea66b899bd
commit
1f75d96637
@ -43,7 +43,7 @@ public class AuthController {
|
||||
|
||||
private TokenEndpoint tokenEndpoint;
|
||||
|
||||
@ApiOperation("OAuth2认证生成token,兼容从请求头获取加密的客户端信息和从请求路径获取客户端信息")
|
||||
@ApiOperation("OAuth2认证生成token")
|
||||
@ApiImplicitParams({
|
||||
@ApiImplicitParam(name = "grant_type", defaultValue = "password", value = "授权模式", required = true),
|
||||
@ApiImplicitParam(name = "client_id", defaultValue = "client", value = "Oauth2客户端ID", required = true),
|
||||
|
@ -12,12 +12,6 @@
|
||||
<artifactId>common-core</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<!-- swagger2-annotations -->
|
||||
<dependency>
|
||||
<groupId>io.swagger</groupId>
|
||||
<artifactId>swagger-annotations</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- spring boot web -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
@ -40,6 +40,17 @@
|
||||
<version>6.2.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-aop</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.swagger</groupId>
|
||||
<artifactId>swagger-annotations</artifactId>
|
||||
<version>${swagger-annotations.version}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
@ -0,0 +1,29 @@
|
||||
package com.youlai.common.web.log;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @Author haoxr
|
||||
* @Date 2021-03-01 16:45
|
||||
* @Version 1.0.0
|
||||
*/
|
||||
@Data
|
||||
public class Log {
|
||||
|
||||
private String description;
|
||||
|
||||
private String username;
|
||||
|
||||
private String ip;
|
||||
|
||||
private String url;
|
||||
|
||||
private String method;
|
||||
|
||||
private String gmtStart;
|
||||
|
||||
private Integer intervalTime;
|
||||
|
||||
private String result;
|
||||
|
||||
}
|
@ -0,0 +1,67 @@
|
||||
package com.youlai.common.web.log;
|
||||
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.Signature;
|
||||
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.springframework.data.redis.core.RedisTemplate;
|
||||
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;
|
||||
|
||||
/**
|
||||
* @Author haoxr
|
||||
* @Date 2021-03-01 16:47
|
||||
* @Version 1.0.0
|
||||
*/
|
||||
@Aspect
|
||||
@Component
|
||||
@AllArgsConstructor
|
||||
@Slf4j
|
||||
public class LogAspect {
|
||||
|
||||
private RedisTemplate redisTemplate;
|
||||
|
||||
@Pointcut("execution(public * com.youlai..*.controller.*.*(..))")
|
||||
public void Log() {
|
||||
}
|
||||
|
||||
@Before("Log()")
|
||||
public void doBefore(JoinPoint point) {
|
||||
}
|
||||
|
||||
@Around("Log()")
|
||||
public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
|
||||
log.info("日志记录");
|
||||
long startTime = System.nanoTime();
|
||||
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
||||
HttpServletRequest request = attributes.getRequest();
|
||||
Log log = new Log();
|
||||
Signature signature = joinPoint.getSignature();
|
||||
MethodSignature methodSignature = (MethodSignature) signature;
|
||||
Method method = methodSignature.getMethod();
|
||||
if (method.isAnnotationPresent(ApiOperation.class)) {
|
||||
ApiOperation apiOperation = method.getAnnotation(ApiOperation.class);
|
||||
log.setDescription(apiOperation.value());
|
||||
}
|
||||
long endTime = System.nanoTime();
|
||||
log.setIntervalTime((int) (endTime - startTime));
|
||||
log.setIp(request.getRemoteUser());
|
||||
log.setUrl(request.getRequestURL().toString());
|
||||
log.setMethod(request.getMethod());
|
||||
redisTemplate.opsForSet().add("sys:log", JSONUtil.toJsonStr(log));
|
||||
Object result = joinPoint.proceed();
|
||||
return result;
|
||||
}
|
||||
}
|
@ -77,7 +77,6 @@
|
||||
<version>${youlai.version}</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>com.youlai</groupId>
|
||||
<artifactId>common-redis</artifactId>
|
||||
|
Loading…
Reference in New Issue
Block a user