springbootspringmvc拦截POST、PUT、DELETE请求参
数和响。。。
1.操作⽇志实体类
@Document(collection = "operation_log")
@Getter
@Setter
@ToString
public class OperationLog extends BaseEntityWithId {
private String userId; // 操作⼈
private String resource; // 操作的资源
private String requestMethod; // 请求⽅式
private String beanName; // 操作的类
private String methodName; // 操作的模块
private String requestParams; // 请求的参数
private String responseData; // 返回数据
}
2.
package com.fig;
import com.alibaba.fastjson.JSON;
import com.ity.OperationLog;
import com.vian.admin.event.OperationLogEvent;
import onfiguration.event.EventPublisher;
import com.vian.microservice.security.SecurityUtils;
slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.t.request.RequestContextHolder;
import org.t.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
@Aspect
@Component
@Slf4j
public class RequestLogAspect {
@Autowired private EventPublisher eventPublisher;
private ThreadLocal<OperationLog> logThreadLocal = new ThreadLocal<>();
//拦截web下所有⽅法
@Pointcut("execution(* com.vian.admin.web..*.*(..))")
public void pointcut() {
log.info("拦截请求start");
}
@Before("pointcut()")
public void doBefore(JoinPoint joinPoint) {
ServletRequestAttributes attributes =
(ServletRequestAttributes) RequestAttributes();
HttpServletRequest request = Request();
String beanName = Signature().getDeclaringTypeName();
String methodName = Signature().getName();
String uri = RequestURI();
String userId = CurrentUserId();
//get⽅法不记录⽇志
if ("GET".Method())) {
return;
}
//请求参数
Object[] paramsArray = Args();
log.info(
"请求⽇志拦截:userId={}, uri={}, method={}, request={}",
userId,
uri,
paramsArray);
/
/ 组装⽇志数据
OperationLog optLog = new OperationLog();
optLog.setUserId(userId);
optLog.setResource(uri);
optLog.Method());
optLog.setBeanName(beanName);
optLog.setMethodName(methodName);
optLog.setRequestParams(argsArrayToString(paramsArray));
logThreadLocal.set(optLog);
}
@AfterReturning(returning = "result", pointcut = "pointcut()") public void doAfterReturning(Object result) {
try {
// 处理完请求,从线程变量中获取⽇志数据,并记录到db
OperationLog optLog = ();
if (null != optLog) {
optLog.JSONString(result));
eventPublisher.publish(new OperationLogEvent(this, optLog));      }
} catch (Exception e) {
<("***操作请求⽇志记录失败doAfterReturning()***", e);    } finally {
// 清除threadlocal
}
spring mvc和boot区别}
/**
* 请求参数拼装
*
* @param paramsArray
* @return
*/
private String argsArrayToString(Object[] paramsArray) {
String params = "";
if (paramsArray != null && paramsArray.length > 0) {
for (int i = 0; i < paramsArray.length; i++) {
Object jsonObj = JSON(paramsArray[i]);
params += String() + " ";
}
}
im();
}
}
测试结果:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。