feat(mall): 代码优化
parent
db90e1cb65
commit
87300cf3dd
@ -0,0 +1,65 @@
|
||||
package com.wayn.admin.framework.config;
|
||||
|
||||
|
||||
import cn.hutool.extra.servlet.ServletUtil;
|
||||
import com.wayn.common.util.ServletUtils;
|
||||
import jakarta.servlet.ReadListener;
|
||||
import jakarta.servlet.ServletInputStream;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletRequestWrapper;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
public class RequestWrapper extends HttpServletRequestWrapper {
|
||||
|
||||
|
||||
private byte[] body;
|
||||
|
||||
public RequestWrapper(HttpServletRequest request) throws IOException {
|
||||
super(request);
|
||||
body = ServletUtils.getBodyBytes(request);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BufferedReader getReader() throws IOException {
|
||||
return new BufferedReader(new InputStreamReader(getInputStream()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public ServletInputStream getInputStream() {
|
||||
|
||||
final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(body);
|
||||
|
||||
return new ServletInputStream() {
|
||||
|
||||
@Override
|
||||
public int read() {
|
||||
return byteArrayInputStream.read();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isFinished() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isReady() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setReadListener(ReadListener readListener) {
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
public void setInputStream(byte[] body) {
|
||||
this.body = body;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package com.wayn.admin.framework.config;
|
||||
|
||||
import com.wayn.common.util.http.HttpUtil;
|
||||
import jakarta.servlet.*;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class RequestWrapperFilter implements Filter {
|
||||
|
||||
@Override
|
||||
public void init(FilterConfig filterConfig) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException
|
||||
, IOException {
|
||||
HttpServletRequest req = (HttpServletRequest) request;
|
||||
if (HttpUtil.isJsonRequest(req)) {
|
||||
ServletRequest requestWrapper = new RequestWrapper((HttpServletRequest) request);
|
||||
chain.doFilter(requestWrapper, response);
|
||||
} else {
|
||||
chain.doFilter(request, response);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,144 @@
|
||||
package com.wayn.common.aspect;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.wayn.common.util.ServletUtils;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
import org.aspectj.lang.annotation.*;
|
||||
import org.aspectj.lang.reflect.MethodSignature;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.core.task.TaskExecutor;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
@Aspect
|
||||
@Component
|
||||
@Slf4j
|
||||
public class OperLogAspect {
|
||||
|
||||
private static final ThreadLocal<Long> startTimeLocal = ThreadLocal.withInitial(() -> 0L);
|
||||
|
||||
@Autowired
|
||||
private OperMgrLogDao operMgrLogDao;
|
||||
|
||||
@Autowired
|
||||
@Qualifier("operLogTaskExecutor")
|
||||
private TaskExecutor operLogTaskExecutor;
|
||||
|
||||
@Pointcut("@annotation(com.vc.mgr.modules.vc.model.annotation.Log)")
|
||||
public void logPointCut() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 当方法执行前
|
||||
*
|
||||
* @param joinPoint 切点
|
||||
*/
|
||||
@Before(value = "logPointCut()")
|
||||
public void doBefore(JoinPoint joinPoint) {
|
||||
startTimeLocal.set(System.nanoTime());
|
||||
}
|
||||
|
||||
/**
|
||||
* 当方法正常返回时执行
|
||||
*
|
||||
* @param joinPoint 切点
|
||||
*/
|
||||
@AfterReturning(value = "logPointCut()", returning = "response")
|
||||
public void doAfterReturning(JoinPoint joinPoint, Object response) {
|
||||
handlerLog(joinPoint, null, response);
|
||||
}
|
||||
|
||||
/**
|
||||
* 当方法异常返回时执行
|
||||
*
|
||||
* @param joinPoint 切点
|
||||
*/
|
||||
@AfterThrowing(value = "logPointCut()", throwing = "e")
|
||||
public void doAfterThrowing(JoinPoint joinPoint, Exception e) {
|
||||
handlerLog(joinPoint, e, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 日志处理方法
|
||||
*
|
||||
* @param joinPoint 切点
|
||||
* @param e 异常
|
||||
* @param response 切面方法返回
|
||||
*/
|
||||
private void handlerLog(JoinPoint joinPoint, Exception e, Object response) {
|
||||
long executeTime;
|
||||
try {
|
||||
executeTime = System.nanoTime() - startTimeLocal.get();
|
||||
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
|
||||
Method method = methodSignature.getMethod();
|
||||
HttpServletRequest request = ServletUtils.getRequest();
|
||||
// 获取日志注解
|
||||
Log log = method.getAnnotation(Log.class);
|
||||
LoginUserBean loginUserBean = (LoginUserBean) request.getSession().getAttribute(Constants.CURRENT_ADMIN_KEY);
|
||||
if (loginUserBean == null) {
|
||||
return;
|
||||
}
|
||||
if (log != null) {
|
||||
// 创建操作日志对象
|
||||
OperMgrLog operLog = new OperMgrLog();
|
||||
operLog.setCreateTime(new Date());
|
||||
operLog.setModuleName(log.value().getName());
|
||||
operLog.setOperation(log.operator().getCode());
|
||||
operLog.setUserName(loginUserBean.getAname());
|
||||
operLog.setUrl(StringUtils.substring(request.getRequestURI(), 0, 100));
|
||||
// 设置方法名称
|
||||
String className = joinPoint.getTarget().getClass().getName();
|
||||
String methodName = method.getName();
|
||||
operLog.setMethod(className + "." + methodName + "()");
|
||||
operLog.setOperState(Constants.OPERATOR_SUCCESS);
|
||||
operLog.setRequestMethod(request.getMethod());
|
||||
operLog.setExecuteTime(executeTime / 1000000);
|
||||
// 保存请求响应
|
||||
if (Objects.nonNull(response)) {
|
||||
operLog.setRequestResponse(StringUtils.substring(JSON.toJSONString(response), 0, 2000));
|
||||
}
|
||||
|
||||
if (log.isNeedParam()) {
|
||||
String reqParmeter = "";
|
||||
if (ServletUtil.isJsonRequest(request)) {
|
||||
reqParmeter = ServletUtil.getBody(request);
|
||||
} else {
|
||||
// 保存请求参数
|
||||
Map<String, String[]> parameterMap = request.getParameterMap();
|
||||
JSONObject obj = new JSONObject(true);
|
||||
for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
|
||||
String key = entry.getKey();
|
||||
String[] value = entry.getValue();
|
||||
if (value.length == 1 && StringUtils.isNotEmpty(value[0])) {
|
||||
obj.put(key, value[0]);
|
||||
} else {
|
||||
obj.put(key, value);
|
||||
}
|
||||
}
|
||||
reqParmeter = obj.toJSONString();
|
||||
}
|
||||
operLog.setRequestParams(StringUtils.substring(reqParmeter, 0, 2000));
|
||||
}
|
||||
if (e != null) {
|
||||
operLog.setOperState(Constants.OPERATOR_fail);
|
||||
operLog.setErrorMsg(StringUtils.substring(e.getMessage(), 0, 2000));
|
||||
}
|
||||
operLogTaskExecutor.execute(() -> operMgrLogDao.insert(operLog));
|
||||
}
|
||||
} catch (Exception exception) {
|
||||
log.error("handlerLog", exception);
|
||||
} finally {
|
||||
startTimeLocal.remove();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue