diff --git a/austin-web/src/main/java/com/java3y/austin/web/annotation/AustinAspect.java b/austin-web/src/main/java/com/java3y/austin/web/annotation/AustinAspect.java new file mode 100644 index 0000000..9a7a879 --- /dev/null +++ b/austin-web/src/main/java/com/java3y/austin/web/annotation/AustinAspect.java @@ -0,0 +1,20 @@ +package com.java3y.austin.web.annotation; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * @author kl + * @version 1.0.0 + * @description 接口切面注解 + * @date 2023/2/23 9:01 + */ +@Target({ElementType.TYPE, ElementType.METHOD}) +@Retention(RetentionPolicy.RUNTIME) +@Documented +public @interface AustinAspect { + +} diff --git a/austin-web/src/main/java/com/java3y/austin/web/aspect/AustinAspect.java b/austin-web/src/main/java/com/java3y/austin/web/aspect/AustinAspect.java new file mode 100644 index 0000000..f2bbc24 --- /dev/null +++ b/austin-web/src/main/java/com/java3y/austin/web/aspect/AustinAspect.java @@ -0,0 +1,113 @@ +package com.java3y.austin.web.aspect; + +import cn.hutool.core.util.IdUtil; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.google.common.collect.Lists; +import com.java3y.austin.web.vo.RequestLogDTO; +import lombok.extern.slf4j.Slf4j; +import org.aspectj.lang.JoinPoint; +import org.aspectj.lang.annotation.AfterThrowing; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Before; +import org.aspectj.lang.annotation.Pointcut; +import org.aspectj.lang.reflect.MethodSignature; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import org.springframework.validation.BindingResult; +import org.springframework.web.multipart.MultipartFile; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.util.Arrays; +import java.util.List; + +/** + * @author kl + * @version 1.0.0 + * @description 切面类 + * @date 2023/2/23 9:17 + */ +@Slf4j +@Aspect +@Component +public class AustinAspect { + + @Autowired + private HttpServletRequest request; + + /** + * 同一个请求的KEY + */ + private final String requestIdKey = "request_unique_id"; + + /** + * 只切AustinAspect注解 + */ + @Pointcut("@within(com.java3y.austin.web.annotation.AustinAspect) || @annotation(com.java3y.austin.web.annotation.AustinAspect)") + public void executeService() { + } + + /** + * 前置通知,方法调用前被调用 + * + * @param joinPoint + */ + @Before("executeService()") + public void doBeforeAdvice(JoinPoint joinPoint){ + MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature(); + this.printRequestLog(methodSignature, joinPoint.getArgs()); + } + + /** + * 异常通知 + * + * @param ex + */ + @AfterThrowing(value = "executeService()", throwing = "ex") + public void doAfterThrowingAdvice(Throwable ex) { + printExceptionLog(ex); + } + + /** + * 打印请求日志 + * + * @param methodSignature + * @param argObs + */ + public void printRequestLog(MethodSignature methodSignature, Object[] argObs) { + RequestLogDTO logVo = new RequestLogDTO(); + //设置请求唯一ID + logVo.setId(IdUtil.fastUUID()); + request.setAttribute(requestIdKey, logVo.getId()); + logVo.setUri(request.getRequestURI()); + logVo.setMethod(request.getMethod()); + List args = Lists.newArrayList(); + //过滤掉一些不能转为json字符串的参数 + Arrays.stream(argObs).forEach(e -> { + if (e instanceof MultipartFile || e instanceof HttpServletRequest + || e instanceof HttpServletResponse || e instanceof BindingResult) { + return; + } + args.add(e); + }); + logVo.setArgs(args.toArray()); + logVo.setProduct("austin"); + logVo.setPath(methodSignature.getDeclaringTypeName() + "." + methodSignature.getMethod().getName()); + logVo.setReferer(request.getHeader("referer")); + logVo.setRemoteAddr(request.getRemoteAddr()); + logVo.setUserAgent(request.getHeader("user-agent")); + log.info("austin-aspect-log,request:" + JSON.toJSONString(logVo)); + } + + /** + * 打印异常日志 + * + * @param ex + */ + public void printExceptionLog(Throwable ex) { + JSONObject logVo = new JSONObject(); + logVo.put("id", request.getAttribute(requestIdKey)); + log.error("austin-aspect-log,exception:" + JSON.toJSONString(logVo), ex); + } +} diff --git a/austin-web/src/main/java/com/java3y/austin/web/config/CommonConfiguration.java b/austin-web/src/main/java/com/java3y/austin/web/config/CommonConfiguration.java new file mode 100644 index 0000000..925ea16 --- /dev/null +++ b/austin-web/src/main/java/com/java3y/austin/web/config/CommonConfiguration.java @@ -0,0 +1,34 @@ +package com.java3y.austin.web.config; + +import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; +import com.google.common.collect.Lists; +import org.springframework.boot.autoconfigure.http.HttpMessageConverters; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.MediaType; + +import java.util.List; + +/** + * @author kl + * @version 1.0.0 + * @description 通用配置 + * @date 2023/2/23 10:40 + */ +@Configuration +public class CommonConfiguration { + + /** + * FastJson 消息转换器 格式化输出json + * + * @return + */ + @Bean + public HttpMessageConverters fastJsonHttpMessageConverters() { + FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); + List supportedMediaTypes = Lists.newArrayList(); + supportedMediaTypes.add(MediaType.APPLICATION_JSON); + fastConverter.setSupportedMediaTypes(supportedMediaTypes); + return new HttpMessageConverters(fastConverter); + } +} diff --git a/austin-web/src/main/java/com/java3y/austin/web/controller/ChannelAccountController.java b/austin-web/src/main/java/com/java3y/austin/web/controller/ChannelAccountController.java index 62b06c4..7be7843 100644 --- a/austin-web/src/main/java/com/java3y/austin/web/controller/ChannelAccountController.java +++ b/austin-web/src/main/java/com/java3y/austin/web/controller/ChannelAccountController.java @@ -5,6 +5,7 @@ import cn.hutool.core.util.StrUtil; import com.java3y.austin.common.constant.AustinConstant; import com.java3y.austin.common.enums.RespStatusEnum; import com.java3y.austin.support.domain.ChannelAccount; +import com.java3y.austin.web.annotation.AustinAspect; import com.java3y.austin.web.annotation.AustinResult; import com.java3y.austin.web.exception.CommonException; import com.java3y.austin.web.service.ChannelAccountService; @@ -27,6 +28,7 @@ import java.util.stream.Collectors; * @author 3y */ @Slf4j +@AustinAspect @AustinResult @RestController @RequestMapping("/account") diff --git a/austin-web/src/main/java/com/java3y/austin/web/controller/DataController.java b/austin-web/src/main/java/com/java3y/austin/web/controller/DataController.java index c77c6d1..1ac5f97 100644 --- a/austin-web/src/main/java/com/java3y/austin/web/controller/DataController.java +++ b/austin-web/src/main/java/com/java3y/austin/web/controller/DataController.java @@ -1,6 +1,7 @@ package com.java3y.austin.web.controller; import cn.hutool.core.util.StrUtil; +import com.java3y.austin.web.annotation.AustinAspect; import com.java3y.austin.web.annotation.AustinResult; import com.java3y.austin.web.service.DataService; import com.java3y.austin.web.vo.DataParam; @@ -25,6 +26,7 @@ import java.util.Objects; * @author 3y */ @Slf4j +@AustinAspect @AustinResult @RestController @RequestMapping("/trace") diff --git a/austin-web/src/main/java/com/java3y/austin/web/controller/MaterialController.java b/austin-web/src/main/java/com/java3y/austin/web/controller/MaterialController.java index a7c8310..c0cf255 100644 --- a/austin-web/src/main/java/com/java3y/austin/web/controller/MaterialController.java +++ b/austin-web/src/main/java/com/java3y/austin/web/controller/MaterialController.java @@ -3,6 +3,7 @@ package com.java3y.austin.web.controller; import com.java3y.austin.common.enums.ChannelType; import com.java3y.austin.common.vo.BasicResultVO; +import com.java3y.austin.web.annotation.AustinAspect; import com.java3y.austin.web.service.MaterialService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; @@ -21,6 +22,7 @@ import org.springframework.web.multipart.MultipartFile; * @author 3y */ @Slf4j +@AustinAspect @RestController @RequestMapping("/material") @Api("素材管理接口") diff --git a/austin-web/src/main/java/com/java3y/austin/web/controller/MessageTemplateController.java b/austin-web/src/main/java/com/java3y/austin/web/controller/MessageTemplateController.java index 6d1a144..c5d8a5d 100644 --- a/austin-web/src/main/java/com/java3y/austin/web/controller/MessageTemplateController.java +++ b/austin-web/src/main/java/com/java3y/austin/web/controller/MessageTemplateController.java @@ -14,6 +14,7 @@ import com.java3y.austin.service.api.enums.BusinessCode; import com.java3y.austin.service.api.service.RecallService; import com.java3y.austin.service.api.service.SendService; import com.java3y.austin.support.domain.MessageTemplate; +import com.java3y.austin.web.annotation.AustinAspect; import com.java3y.austin.web.annotation.AustinResult; import com.java3y.austin.web.exception.CommonException; import com.java3y.austin.web.service.MessageTemplateService; @@ -43,6 +44,7 @@ import java.util.stream.Collectors; * @author 3y */ @Slf4j +@AustinAspect @AustinResult @RestController @RequestMapping("/messageTemplate") diff --git a/austin-web/src/main/java/com/java3y/austin/web/controller/MiniProgramController.java b/austin-web/src/main/java/com/java3y/austin/web/controller/MiniProgramController.java index 9b46d47..3d147a0 100644 --- a/austin-web/src/main/java/com/java3y/austin/web/controller/MiniProgramController.java +++ b/austin-web/src/main/java/com/java3y/austin/web/controller/MiniProgramController.java @@ -6,6 +6,7 @@ import cn.hutool.http.HttpUtil; import com.google.common.base.Throwables; import com.java3y.austin.common.enums.RespStatusEnum; import com.java3y.austin.support.utils.AccountUtils; +import com.java3y.austin.web.annotation.AustinAspect; import com.java3y.austin.web.annotation.AustinResult; import com.java3y.austin.web.exception.CommonException; import com.java3y.austin.web.utils.Convert4Amis; @@ -30,6 +31,7 @@ import java.util.Objects; * @author 3y */ @Slf4j +@AustinAspect @AustinResult @RestController @RequestMapping("/miniProgram") diff --git a/austin-web/src/main/java/com/java3y/austin/web/controller/OfficialAccountController.java b/austin-web/src/main/java/com/java3y/austin/web/controller/OfficialAccountController.java index 4a566f7..3e93ac5 100644 --- a/austin-web/src/main/java/com/java3y/austin/web/controller/OfficialAccountController.java +++ b/austin-web/src/main/java/com/java3y/austin/web/controller/OfficialAccountController.java @@ -12,6 +12,7 @@ import com.java3y.austin.common.constant.CommonConstant; import com.java3y.austin.common.constant.OfficialAccountParamConstant; import com.java3y.austin.common.enums.RespStatusEnum; import com.java3y.austin.support.utils.AccountUtils; +import com.java3y.austin.web.annotation.AustinAspect; import com.java3y.austin.web.annotation.AustinResult; import com.java3y.austin.web.config.WeChatLoginConfig; import com.java3y.austin.web.exception.CommonException; @@ -44,6 +45,7 @@ import java.util.*; * @author 3y */ @Slf4j +@AustinAspect @AustinResult @RequestMapping("/officialAccount") @RestController diff --git a/austin-web/src/main/java/com/java3y/austin/web/controller/RefreshTokenController.java b/austin-web/src/main/java/com/java3y/austin/web/controller/RefreshTokenController.java index a558cbb..5f2096a 100644 --- a/austin-web/src/main/java/com/java3y/austin/web/controller/RefreshTokenController.java +++ b/austin-web/src/main/java/com/java3y/austin/web/controller/RefreshTokenController.java @@ -4,6 +4,7 @@ package com.java3y.austin.web.controller; import com.java3y.austin.common.enums.ChannelType; import com.java3y.austin.cron.handler.RefreshDingDingAccessTokenHandler; import com.java3y.austin.cron.handler.RefreshGeTuiAccessTokenHandler; +import com.java3y.austin.web.annotation.AustinAspect; import com.java3y.austin.web.annotation.AustinResult; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; @@ -16,6 +17,7 @@ import org.springframework.web.bind.annotation.RestController; * @Author 3y */ @AustinResult +@AustinAspect @Api(tags = {"手动刷新token的接口"}) @RestController public class RefreshTokenController { diff --git a/austin-web/src/main/java/com/java3y/austin/web/controller/SendController.java b/austin-web/src/main/java/com/java3y/austin/web/controller/SendController.java index dda2f74..16da1af 100644 --- a/austin-web/src/main/java/com/java3y/austin/web/controller/SendController.java +++ b/austin-web/src/main/java/com/java3y/austin/web/controller/SendController.java @@ -4,6 +4,7 @@ package com.java3y.austin.web.controller; import com.java3y.austin.service.api.domain.SendRequest; import com.java3y.austin.service.api.domain.SendResponse; import com.java3y.austin.service.api.service.SendService; +import com.java3y.austin.web.annotation.AustinAspect; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; @@ -17,6 +18,7 @@ import org.springframework.web.bind.annotation.RestController; @Api(tags = {"发送消息"}) @RestController +@AustinAspect public class SendController { @Autowired diff --git a/austin-web/src/main/java/com/java3y/austin/web/vo/RequestLogDTO.java b/austin-web/src/main/java/com/java3y/austin/web/vo/RequestLogDTO.java new file mode 100644 index 0000000..c842b14 --- /dev/null +++ b/austin-web/src/main/java/com/java3y/austin/web/vo/RequestLogDTO.java @@ -0,0 +1,91 @@ +package com.java3y.austin.web.vo; + +import com.alibaba.fastjson.annotation.JSONField; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * @author kl + * @version 1.0.0 + * @description 请求日志Vo + * @date 2023/2/23 9:20 + */ +@Data +@Builder +@AllArgsConstructor +@NoArgsConstructor +public class RequestLogDTO { + + /** + * 请求ID(UUID)与 ResponseLogVo id 一致 + */ + private String id; + + /** + * 接口URI + */ + @JSONField(ordinal = 1) + private String uri; + + /** + * 请求方法 + */ + @JSONField(ordinal = 2) + private String method; + + /** + * 参数数组 + */ + @JSONField(ordinal = 3) + private Object[] args; + + /** + * 是否需要认证 + */ + @JSONField(ordinal = 4) + private Boolean auth; + + /** + * 认证令牌 + */ + @JSONField(ordinal = 5) + private String token; + + /** + * 登录账号信息 + */ + @JSONField(ordinal = 6) + private Object loginAccount; + + /** + * 产品 + */ + @JSONField(ordinal = 7) + private String product; + + /** + * 类名+方法名 + */ + @JSONField(ordinal = 8) + private String path; + + /** + * 页面引用 + */ + @JSONField(ordinal = 9) + private String referer; + + /** + * 请求地址 + */ + @JSONField(ordinal = 10) + private String remoteAddr; + + /** + * 用户代理(浏览器) + */ + @JSONField(ordinal = 11) + private String userAgent; +}