springbootspringmvc拦截器拦截POST、PUT、DELETE请求参数和响。。。

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

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.vian.admin.config;
import com.alibaba.fastjson.JSON;
import com.vian.admin.entity.OperationLog;
import com.vian.admin.event.OperationLogEvent;
import com.vian.core.configuration.event.EventPublisher;
import com.vian.microservice.security.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import ng.JoinPoint;
import ng.annotation.AfterReturning;
import ng.annotation.Aspect;
import ng.annotation.Before;
import ng.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import ponent;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.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() {
("拦截请求start");
}
@Before("pointcut()")
public void doBefore(JoinPoint joinPoint) {
ServletRequestAttributes attributes =
(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
String beanName = joinPoint.getSignature().getDeclaringTypeName();
String methodName = joinPoint.getSignature().getName();
String uri = request.getRequestURI();
String userId = SecurityUtils.getCurrentUserId();
//get⽅法不记录⽇志
if ("GET".equals(request.getMethod())) {
return;
}
//请求参数
Object[] paramsArray = joinPoint.getArgs();
(
"请求⽇志拦截:userId={}, uri={}, method={}, request={}",
userId,
uri,
request.getMethod(),
paramsArray);
// 组装⽇志数据
OperationLog optLog = new OperationLog();
optLog.setUserId(userId);
optLog.setResource(uri);
optLog.setRequestMethod(request.getMethod());
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 = logThreadLocal.get();
if (null != optLog) {
optLog.setResponseData(JSON.toJSONString(result));
eventPublisher.publish(new OperationLogEvent(this, optLog)); }
} catch (Exception e) {
log.error("***操作请求⽇志记录失败doAfterReturning()***", e); } finally {
// 清除threadlocal
logThreadLocal.remove();
}
}
/**
* 请求参数拼装
*
* @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.toJSON(paramsArray[i]);
params += jsonObj.toString() + " ";
}
}
return params.trim();
}
}
测试结果:。

相关文档
最新文档