|
@@ -0,0 +1,122 @@
|
|
|
+package com.css.simulation.resource.server.infrastructure.common.configuration;
|
|
|
+
|
|
|
+import api.common.pojo.common.ResponseBodyVO;
|
|
|
+import api.common.pojo.po.log.LogOperationPO;
|
|
|
+import api.common.util.LogUtil;
|
|
|
+import api.common.util.ObjectUtil;
|
|
|
+import com.css.simulation.resource.server.infrastructure.common.util.AuthUtil;
|
|
|
+import com.css.simulation.resource.server.application.service.LogService;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.core.MethodParameter;
|
|
|
+import org.springframework.http.HttpHeaders;
|
|
|
+import org.springframework.http.MediaType;
|
|
|
+import org.springframework.http.server.ServerHttpRequest;
|
|
|
+import org.springframework.http.server.ServerHttpResponse;
|
|
|
+import org.springframework.web.bind.annotation.ControllerAdvice;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
|
|
|
+
|
|
|
+import java.util.regex.Matcher;
|
|
|
+import java.util.regex.Pattern;
|
|
|
+
|
|
|
+
|
|
|
+@ControllerAdvice
|
|
|
+public class LogAdvice implements ResponseBodyAdvice {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ LogService logService;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean supports(MethodParameter methodParameter, Class aClass) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Object beforeBodyWrite(Object o, MethodParameter methodParameter, MediaType mediaType, Class aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
|
|
|
+ //没有操作标记则返回
|
|
|
+ String operationType = LogUtil.getOperationType();
|
|
|
+ LogUtil.remove();//线程标记清空
|
|
|
+ if(ObjectUtil.isNull(operationType)){
|
|
|
+ return o;
|
|
|
+ }
|
|
|
+ //异常后返回
|
|
|
+ if(o instanceof ResponseBodyVO){
|
|
|
+ boolean status = ((ResponseBodyVO) o).isStatus();
|
|
|
+ if(!status){
|
|
|
+ return o;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //类注解的模块名
|
|
|
+ Class<?> containingClass = methodParameter.getContainingClass();
|
|
|
+ RequestMapping classAnnotation = containingClass.getAnnotation(RequestMapping.class);
|
|
|
+ if(ObjectUtil.isNull(classAnnotation)){
|
|
|
+ return o;
|
|
|
+ }
|
|
|
+ String modelName = classAnnotation.name();
|
|
|
+ if(ObjectUtil.isNull(modelName)){
|
|
|
+ modelName = "未命名模块";
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ //获取ip
|
|
|
+ String ip = getRemoteAddress(serverHttpRequest);
|
|
|
+ LogOperationPO po = new LogOperationPO();
|
|
|
+ po.setIp(ip);
|
|
|
+ po.setModule(modelName);
|
|
|
+ po.setOperationType(operationType);
|
|
|
+ po.setUserId(AuthUtil.getCurrentUserId());
|
|
|
+ po.setUsername(AuthUtil.getCurrentUsername());
|
|
|
+ logService.saveOperationLog(po);
|
|
|
+ }catch (Exception e){
|
|
|
+ //e.printStackTrace();
|
|
|
+ }
|
|
|
+ return o;
|
|
|
+ }
|
|
|
+
|
|
|
+ //IP地址获取
|
|
|
+ private String getRemoteAddress(ServerHttpRequest serverHttpRequest) {
|
|
|
+ HttpHeaders headers = serverHttpRequest.getHeaders();
|
|
|
+ String ip = headers.getFirst("x-forwarded-for");
|
|
|
+ if(ip != null && ip.length() != 0){
|
|
|
+ if(ip.indexOf(",") != -1){
|
|
|
+ ip = ip.split(",")[0];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(ip == null || ip.length() == 0){
|
|
|
+ ip = headers.getFirst("Proxy-Client-IP");
|
|
|
+ }
|
|
|
+ if(ip == null || ip.length() == 0){
|
|
|
+ ip = headers.getFirst("WL-Proxy-Client-IP");
|
|
|
+ }
|
|
|
+ if(ip == null || ip.length() == 0){
|
|
|
+ ip = headers.getFirst("HTTP_CLIENT_IP");
|
|
|
+ }
|
|
|
+ if(ip == null || ip.length() == 0){
|
|
|
+ ip = headers.getFirst("HTTP_X_FORWARDED_FOR");
|
|
|
+ }
|
|
|
+ if(ip == null || ip.length() == 0){
|
|
|
+ ip = headers.getFirst("X-Real-IP");
|
|
|
+ }
|
|
|
+ if(ip == null || ip.length() == 0){
|
|
|
+ ip = serverHttpRequest.getRemoteAddress().getAddress().getHostAddress();
|
|
|
+ }
|
|
|
+ if(isIpv4(ip)){
|
|
|
+ return ip;
|
|
|
+ }else{
|
|
|
+ return "Illegal IP address";
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ //IP地址验证,防止非法地址
|
|
|
+ private boolean isIpv4(String ipAddress) {
|
|
|
+ String ip = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."
|
|
|
+ +"(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
|
|
|
+ +"(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
|
|
|
+ +"(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";
|
|
|
+
|
|
|
+ Pattern pattern = Pattern.compile(ip);
|
|
|
+ Matcher matcher = pattern.matcher(ipAddress);
|
|
|
+ return matcher.matches();
|
|
|
+ }
|
|
|
+
|
|
|
+}
|