feat(mall): 代码优化

master
waynaqua 1 year ago
parent db90e1cb65
commit 87300cf3dd

@ -97,7 +97,11 @@
<artifactId>sshj</artifactId>
<version>0.35.0</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>${hutool.version}</version>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>

@ -1,6 +1,8 @@
package com.wayn.admin.framework.config;
import com.wayn.common.config.WaynConfig;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@ -13,4 +15,13 @@ public class MvcConfig implements WebMvcConfigurer {
// 本地文件上传路径
registry.addResourceHandler("/upload/**").addResourceLocations("file:" + WaynConfig.getUploadDir() + "/");
}
@Bean
public FilterRegistrationBean<RequestWrapperFilter> firstFilter() {
FilterRegistrationBean<RequestWrapperFilter> registrationBean = new FilterRegistrationBean<>();
registrationBean.setFilter(new RequestWrapperFilter());
registrationBean.addUrlPatterns("/*");
registrationBean.setOrder(20);
return registrationBean;
}
}

@ -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();
}
}
}

@ -1,6 +1,5 @@
package com.wayn.common.config;
import com.alipay.api.domain.BidDetailVO;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ -24,12 +24,8 @@ public class BusinessException extends RuntimeException {
this.msg = returnCodeEnum.getMsg();
}
public BusinessException(String message, Throwable cause) {
super(message, cause);
public BusinessException(String msg) {
this.msg = msg;
this.code = ReturnCodeEnum.CUSTOM_ERROR.getCode();
}
public BusinessException(String message) {
super(message);
}
}

@ -2,8 +2,10 @@ package com.wayn.common.util;
import com.wayn.common.constant.Constants;
import com.wayn.common.util.http.HttpUtil;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
@ -112,4 +114,8 @@ public class ServletUtils {
response.setHeader("Content-Disposition", HttpUtil.safeHttpHeader("attachment;filename=" + URLEncoder.encode(fileName, Constants.UTF_ENCODING)));
response.setContentType("application/octet-stream");
}
public static byte[] getBodyBytes(ServletRequest request) throws IOException {
return IOUtils.readFully(request.getInputStream(), 1024);
}
}

@ -322,4 +322,8 @@ public class HttpUtil {
return temp;
}
public static boolean isJsonRequest(HttpServletRequest request) {
return request.getHeader("Content-Type") != null
&& request.getHeader("Content-Type").contains("application/json");
}
}

Loading…
Cancel
Save