Commit 015c9803 authored by huangjiamin's avatar huangjiamin

全局日志处理

parent 6e2265ea
...@@ -183,6 +183,12 @@ ...@@ -183,6 +183,12 @@
<version>4.6.0</version> <version>4.6.0</version>
</dependency> </dependency>
<dependency>
<groupId> org.aspectj</groupId >
<artifactId> aspectjweaver</artifactId >
<version> 1.8.7</version >
</dependency>
</dependencies> </dependencies>
......
...@@ -35,7 +35,7 @@ public class IpController { ...@@ -35,7 +35,7 @@ public class IpController {
* 购买IP资源 * 购买IP资源
* *
* @param principal principal * @param principal principal
* @param ipResourceRequestDto todo * @param ipResourceRequestDto ipResourceRequestDto
* @return ResultDto * @return ResultDto
*/ */
@RequestMapping(value = "/buy", method = RequestMethod.POST) @RequestMapping(value = "/buy", method = RequestMethod.POST)
...@@ -57,7 +57,7 @@ public class IpController { ...@@ -57,7 +57,7 @@ public class IpController {
* 续费IP资源 * 续费IP资源
* *
* @param principal principal * @param principal principal
* @param ipResourceRequestDto todo * @param ipResourceRequestDto ipResourceRequestDto
* @return ResultDto * @return ResultDto
*/ */
@RequestMapping(value = "/renew", method = RequestMethod.POST) @RequestMapping(value = "/renew", method = RequestMethod.POST)
...@@ -79,7 +79,7 @@ public class IpController { ...@@ -79,7 +79,7 @@ public class IpController {
* 删除指定IP资源 * 删除指定IP资源
* *
* @param principal principal * @param principal principal
* @param ipResourceRequestDto todo--前端只传了 addr * @param ipResourceRequestDto ipResourceRequestDto
* @return ResultDto * @return ResultDto
*/ */
@RequestMapping(value = "/del", method = RequestMethod.POST) @RequestMapping(value = "/del", method = RequestMethod.POST)
...@@ -101,7 +101,7 @@ public class IpController { ...@@ -101,7 +101,7 @@ public class IpController {
* 获取IP列表 * 获取IP列表
* *
* @param principal principal * @param principal principal
* @param ipListRequestDto todo * @param ipListRequestDto ipListRequestDto
* @return ResultDto * @return ResultDto
*/ */
@RequestMapping(value = "/list", method = RequestMethod.POST) @RequestMapping(value = "/list", method = RequestMethod.POST)
...@@ -128,7 +128,7 @@ public class IpController { ...@@ -128,7 +128,7 @@ public class IpController {
* (暂不知) * (暂不知)
* *
* @param principal principal * @param principal principal
* @param ipResourceUpdateDto todo * @param ipResourceUpdateDto ipResourceUpdateDto
* @return ResultDto * @return ResultDto
*/ */
@RequestMapping(value = "/update", method = RequestMethod.POST) @RequestMapping(value = "/update", method = RequestMethod.POST)
...@@ -152,7 +152,7 @@ public class IpController { ...@@ -152,7 +152,7 @@ public class IpController {
* 添加自有ip的检测 * 添加自有ip的检测
* *
* @param principal principal * @param principal principal
* @param ipResourceUpdateDto todo * @param ipResourceUpdateDto ipResourceUpdateDto
* @return ResultDto * @return ResultDto
*/ */
@RequestMapping(value = "/query", method = RequestMethod.POST) @RequestMapping(value = "/query", method = RequestMethod.POST)
...@@ -174,7 +174,7 @@ public class IpController { ...@@ -174,7 +174,7 @@ public class IpController {
/** /**
* @param principal principal * @param principal principal
* @param ipResourceRequestDto todo * @param ipResourceRequestDto ipResourceRequestDto
* @return ResultDto * @return ResultDto
*/ */
@RequestMapping(value = "/detail", method = RequestMethod.POST) @RequestMapping(value = "/detail", method = RequestMethod.POST)
......
package com.edgec.browserbackend.common.log;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.*;
/**
* @author JMW
*/
@Component
@Aspect
public class MvcMethodLogAdvice {
private static Logger LOG = LoggerFactory.getLogger(MvcMethodLogAdvice.class);
@Around("@annotation(org.springframework.web.bind.annotation.RequestMapping)")
public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
//获取当前请求对象
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = Objects.requireNonNull(attributes).getRequest();
Signature signature = joinPoint.getSignature();
MethodSignature methodSignature = (MethodSignature) signature;
Method method = methodSignature.getMethod();
String sb = "url : " + request.getRequestURL().toString() + " ; " +
"Ip : " + request.getRemoteAddr() + " ; " +
"Method : " + request.getMethod() + "; " +
"Parameter : " + getParameter(method, joinPoint.getArgs());
LOG.info(sb);
return joinPoint.proceed();
}
/**
* 根据方法和传入的参数获取请求参数
*/
private Object getParameter(Method method, Object[] args) {
List<Object> argList = new ArrayList<>();
Parameter[] parameters = method.getParameters();
for (int i = 0; i < parameters.length; i++) {
//将RequestBody注解修饰的参数作为请求参数
RequestBody requestBody = parameters[i].getAnnotation(RequestBody.class);
if (requestBody != null) {
argList.add(args[i]);
}
//将RequestParam注解修饰的参数作为请求参数
RequestParam requestParam = parameters[i].getAnnotation(RequestParam.class);
if (requestParam != null) {
Map<String, Object> map = new HashMap<>();
String key = parameters[i].getName();
if (!org.springframework.util.StringUtils.isEmpty(requestParam.value())) {
key = requestParam.value();
}
map.put(key, args[i]);
argList.add(map);
}
}
if (argList.size() == 0) {
return null;
} else if (argList.size() == 1) {
return argList.get(0);
} else {
return argList;
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment