6 changed files with 306 additions and 211 deletions
@ -0,0 +1,162 @@ |
|||
package com.kms.aspect; |
|||
|
|||
import com.alibaba.fastjson.JSON; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.aspectj.lang.JoinPoint; |
|||
import org.aspectj.lang.ProceedingJoinPoint; |
|||
import org.aspectj.lang.annotation.Around; |
|||
import org.aspectj.lang.annotation.Aspect; |
|||
import org.aspectj.lang.annotation.Pointcut; |
|||
import org.aspectj.lang.reflect.MethodSignature; |
|||
import org.springframework.beans.factory.InitializingBean; |
|||
import org.springframework.context.EnvironmentAware; |
|||
import org.springframework.core.env.Environment; |
|||
import org.springframework.stereotype.Component; |
|||
import org.springframework.web.bind.annotation.RequestBody; |
|||
import org.springframework.web.context.request.RequestContextHolder; |
|||
import org.springframework.web.context.request.ServletRequestAttributes; |
|||
import org.springframework.web.multipart.MultipartFile; |
|||
|
|||
import javax.servlet.http.HttpServletRequest; |
|||
import javax.servlet.http.HttpServletResponse; |
|||
import java.lang.reflect.Method; |
|||
import java.lang.reflect.Parameter; |
|||
import java.util.Collection; |
|||
import java.util.Enumeration; |
|||
import java.util.HashMap; |
|||
import java.util.Map; |
|||
|
|||
|
|||
/** |
|||
* 接口日志界面 |
|||
* |
|||
* @author hxh |
|||
**/ |
|||
@Aspect |
|||
@Component |
|||
@Slf4j |
|||
public class WebLogAspect implements EnvironmentAware, InitializingBean { |
|||
|
|||
private boolean printResponseEnable; |
|||
private Environment environment; |
|||
|
|||
/** |
|||
* 切点 |
|||
*/ |
|||
@Pointcut("@within(org.springframework.web.bind.annotation.RestController) || @within(org.springframework.stereotype.Controller)") |
|||
public void webLog() { |
|||
} |
|||
|
|||
/** |
|||
* 环绕切面 |
|||
* |
|||
* @param joinPoint ProceedingJoinPoint |
|||
* @return 结果 |
|||
* @throws Throwable 异常 |
|||
*/ |
|||
@Around("webLog()") |
|||
public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable { |
|||
long startTime = System.currentTimeMillis(); |
|||
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); |
|||
if (servletRequestAttributes == null) { |
|||
return joinPoint.proceed(); |
|||
} |
|||
HttpServletRequest request = servletRequestAttributes.getRequest(); |
|||
Class<?> targetCls = joinPoint.getTarget().getClass(); |
|||
MethodSignature ms = (MethodSignature) joinPoint.getSignature(); |
|||
//获取目标方法上的注解指定的操作名称
|
|||
Method targetMethod = |
|||
targetCls.getDeclaredMethod( |
|||
ms.getName(), |
|||
ms.getParameterTypes()); |
|||
String uri = request.getRequestURI(); |
|||
String httpMethod = request.getMethod(); |
|||
Object requestParams = getRequestParameter(request, joinPoint); |
|||
log.info("[{}] \"{}\" begin, params:{}", httpMethod, uri, |
|||
JSON.toJSONString(requestParams)); |
|||
Object result; |
|||
try { |
|||
result = joinPoint.proceed(); |
|||
if (printResponseEnable) { |
|||
log.info("[{}] \"{}\" response:{}", httpMethod, uri, JSON.toJSONString(result)); |
|||
} |
|||
return result; |
|||
} catch (Throwable ex) { |
|||
log.error("[{}] \"{}\" error:{}", httpMethod, uri, ex.getMessage()); |
|||
throw ex; |
|||
} finally { |
|||
log.info("[{}] \"{}\" end, use time:{}ms", httpMethod, uri, System.currentTimeMillis() - startTime); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* 获取请求参数 |
|||
* |
|||
* @param request HttpServletRequest |
|||
* @param joinPoint JoinPoint |
|||
* @return 结果 |
|||
*/ |
|||
private Object getRequestParameter(HttpServletRequest request, JoinPoint joinPoint) { |
|||
Object[] args = joinPoint.getArgs(); |
|||
Method method = ((MethodSignature) joinPoint.getSignature()).getMethod(); |
|||
Parameter[] parameters = method.getParameters(); |
|||
Map<String, Object> params = new HashMap<>(parameters.length); |
|||
//json数据
|
|||
for (int i = 0; i < parameters.length; i++) { |
|||
if (isFilterObject(args[i])) { |
|||
continue; |
|||
} |
|||
RequestBody requestBody = parameters[i].getAnnotation(RequestBody.class); |
|||
if (requestBody != null) { |
|||
//一个接口只会有一个RequestBody
|
|||
return args[i]; |
|||
} |
|||
} |
|||
//表单提交或url拼接
|
|||
Enumeration<String> paramNames = request.getParameterNames(); |
|||
while (paramNames.hasMoreElements()) { |
|||
String key = paramNames.nextElement(); |
|||
params.put(key, request.getParameter(key)); |
|||
} |
|||
return !params.isEmpty() ? params : null; |
|||
} |
|||
|
|||
/** |
|||
* 是否需要过滤 |
|||
* |
|||
* @param obj Object |
|||
* @return true or false |
|||
*/ |
|||
private boolean isFilterObject(final Object obj) { |
|||
if (obj == null) { |
|||
return true; |
|||
} |
|||
Class<?> clazz = obj.getClass(); |
|||
if (clazz.isArray()) { |
|||
return clazz.getComponentType().isAssignableFrom(MultipartFile.class); |
|||
} else if (Collection.class.isAssignableFrom(clazz)) { |
|||
Collection collection = (Collection) obj; |
|||
for (Object o : collection) { |
|||
return o instanceof MultipartFile; |
|||
} |
|||
} else if (Map.class.isAssignableFrom(clazz)) { |
|||
Map map = (Map) obj; |
|||
for (Object o : map.entrySet()) { |
|||
Map.Entry entry = (Map.Entry) o; |
|||
return entry.getValue() instanceof MultipartFile; |
|||
} |
|||
} |
|||
return obj instanceof MultipartFile || obj instanceof HttpServletRequest || obj instanceof HttpServletResponse; |
|||
} |
|||
|
|||
@Override |
|||
public void setEnvironment(Environment environment) { |
|||
this.environment = environment; |
|||
} |
|||
|
|||
|
|||
@Override |
|||
public void afterPropertiesSet() { |
|||
printResponseEnable = environment.getProperty("sy.public.log-config.print-response", Boolean.class, false); |
|||
} |
|||
} |
@ -0,0 +1,25 @@ |
|||
package com.kms.web.utils; |
|||
|
|||
import lombok.AllArgsConstructor; |
|||
import lombok.Data; |
|||
import lombok.NoArgsConstructor; |
|||
|
|||
/** |
|||
* @ClassName: WaterResultV2 |
|||
* @Description: TODO |
|||
* @Date: 2024/6/1 上午12:27 |
|||
* * |
|||
* @author: hxh |
|||
* @version: 1.0 |
|||
*/ |
|||
@Data |
|||
@AllArgsConstructor |
|||
@NoArgsConstructor |
|||
public class WaterResultV2<T> { |
|||
private int code; |
|||
private String message; |
|||
private Boolean success; |
|||
private T data; |
|||
private String timestamp; |
|||
|
|||
} |
Loading…
Reference in new issue