From adf3a1c6591b8db2c47f45c2ac2370e66d505edb Mon Sep 17 00:00:00 2001 From: luxiaotao1123 <t1341870251@163.com> Date: 星期五, 22 四月 2022 23:01:55 +0800 Subject: [PATCH] #三方接口日志 --- src/main/java/com/zy/asrs/controller/OpenController.java | 22 ++--------- src/main/java/com/zy/common/config/AdminInterceptor.java | 12 ++++++ src/main/java/com/zy/common/config/ControllerResAdvice.java | 63 +++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/zy/asrs/controller/OpenController.java b/src/main/java/com/zy/asrs/controller/OpenController.java index 7c29e2a..19883d4 100644 --- a/src/main/java/com/zy/asrs/controller/OpenController.java +++ b/src/main/java/com/zy/asrs/controller/OpenController.java @@ -1,21 +1,19 @@ package com.zy.asrs.controller; import com.alibaba.fastjson.JSON; +import com.core.annotations.AppAuth; import com.core.common.*; import com.core.exception.CoolException; import com.zy.asrs.entity.param.OpenOrderCompleteParam; import com.zy.asrs.entity.param.OpenOrderPakinParam; import com.zy.asrs.entity.param.OpenOrderPakoutParam; -import com.zy.asrs.service.ApiLogService; import com.zy.asrs.service.OpenService; import com.zy.common.model.DetlDto; -import com.zy.common.utils.IpTools; import com.zy.common.web.BaseController; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; -import javax.servlet.http.HttpServletRequest; import java.util.ArrayList; import java.util.Date; import java.util.List; @@ -35,8 +33,6 @@ @Autowired private OpenService openService; - @Autowired - private ApiLogService apiLogService; @@ -46,24 +42,14 @@ @PostMapping("/order/pakin/new/package/v1") + @AppAuth(memo = "鎵撳寘涓婄嚎鎺ュ彛") public synchronized R pakinOrderPackage(@RequestHeader(required = false) String appkey, - @RequestParam(required = false) String barcode, - HttpServletRequest request) { + @RequestParam(required = false) String barcode) { auth(appkey, barcode); if (Cools.isEmpty(barcode)) { return R.parse(BaseRes.PARAM); } openService.packageUp(barcode); - // 淇濆瓨鎺ュ彛鏃ュ織 - apiLogService.save( - "鎵撳寘涓婄嚎鎺ュ彛", - request.getRequestURI(), - appkey, - IpTools.gainRealIp(request), - barcode, - JSON.toJSONString(R.ok()), - Boolean.TRUE - ); return R.ok(); } @@ -147,7 +133,7 @@ private void auth(String appkey, Object obj) { - log.info("{}鎺ュ彛琚闂紒appkey:{},璇锋眰鏁版嵁锛歿}", "open/sensorType/list/auth/v1", appkey, JSON.toJSONString(obj)); + log.info("{}鎺ュ彛琚闂紱appkey:{}锛涜姹傛暟鎹細{}", "open/sensorType/list/auth/v1", appkey, JSON.toJSONString(obj)); if (!auth) { return; } diff --git a/src/main/java/com/zy/common/config/AdminInterceptor.java b/src/main/java/com/zy/common/config/AdminInterceptor.java index b42a596..3932e07 100644 --- a/src/main/java/com/zy/common/config/AdminInterceptor.java +++ b/src/main/java/com/zy/common/config/AdminInterceptor.java @@ -2,6 +2,7 @@ import com.alibaba.fastjson.JSON; import com.baomidou.mybatisplus.mapper.EntityWrapper; +import com.core.annotations.AppAuth; import com.core.annotations.ManagerAuth; import com.core.common.BaseRes; import com.core.common.Cools; @@ -73,6 +74,12 @@ // response.setHeader("Access-Control-Allow-Origin", "*"); HandlerMethod handlerMethod = (HandlerMethod) handler; Method method = handlerMethod.getMethod(); + if (method.isAnnotationPresent(AppAuth.class)){ + AppAuth annotation = method.getAnnotation(AppAuth.class); + if (annotation.value().equals(AppAuth.Auth.CHECK)){ + request.setAttribute("appAuth", annotation.memo()); + } + } if (method.isAnnotationPresent(ManagerAuth.class)){ ManagerAuth annotation = method.getAnnotation(ManagerAuth.class); if (annotation.value().equals(ManagerAuth.Auth.CHECK)){ @@ -92,6 +99,11 @@ } } + @Override + public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception { +// Object r = request.getAttribute("cool-response"); + } + private boolean check(HttpServletRequest request, HttpServletResponse response, String memo) { try { String token = request.getHeader("token"); diff --git a/src/main/java/com/zy/common/config/ControllerResAdvice.java b/src/main/java/com/zy/common/config/ControllerResAdvice.java new file mode 100644 index 0000000..08b22d1 --- /dev/null +++ b/src/main/java/com/zy/common/config/ControllerResAdvice.java @@ -0,0 +1,63 @@ +package com.zy.common.config; + +import com.alibaba.fastjson.JSON; +import com.core.common.Cools; +import com.core.common.R; +import com.zy.asrs.service.ApiLogService; +import com.zy.common.utils.IpTools; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.MethodParameter; +import org.springframework.http.MediaType; +import org.springframework.http.converter.HttpMessageConverter; +import org.springframework.http.server.ServerHttpRequest; +import org.springframework.http.server.ServerHttpResponse; +import org.springframework.http.server.ServletServerHttpRequest; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice; + +import javax.servlet.http.HttpServletRequest; + +/** + * 杩斿洖鍊煎鐞� + * Created by vincent on 2022/4/22 + */ +@Slf4j +@ControllerAdvice +public class ControllerResAdvice implements ResponseBodyAdvice<Object> { + + @Autowired + private ApiLogService apiLogService; + + @Override + public boolean supports(MethodParameter methodParameter, Class<? extends HttpMessageConverter<?>> aClass) { + return true; + } + + @Override + public Object beforeBodyWrite(Object o, MethodParameter methodParameter, MediaType mediaType, Class<? extends HttpMessageConverter<?>> aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) { + if (serverHttpRequest instanceof ServletServerHttpRequest) { + HttpServletRequest request = ((ServletServerHttpRequest) serverHttpRequest).getServletRequest(); + Object appAuth = request.getAttribute("appAuth"); + if (appAuth != null) { + if (o instanceof R) { + String appkey = request.getHeader("appkey"); + if (!Cools.isEmpty(appkey)) { + // 淇濆瓨鎺ュ彛鏃ュ織 + apiLogService.save( + String.valueOf(appAuth), + request.getRequestURI(), + appkey, + IpTools.gainRealIp(request), + JSON.toJSONString(request.getParameterMap()), + JSON.toJSONString(o), + Boolean.TRUE + ); + } + } + } + } + return o; + } + +} -- Gitblit v1.9.1