|
@@ -0,0 +1,285 @@
|
|
|
+package com.diagbot.aop;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.auth0.jwt.JWT;
|
|
|
+import com.auth0.jwt.exceptions.JWTDecodeException;
|
|
|
+import com.auth0.jwt.interfaces.Claim;
|
|
|
+import com.auth0.jwt.interfaces.DecodedJWT;
|
|
|
+import com.diagbot.dto.QcCasesDTO;
|
|
|
+import com.diagbot.dto.SysLoginLogDTO;
|
|
|
+import com.diagbot.dto.SysOperationLogDTO;
|
|
|
+import com.diagbot.entity.SysHospitalSet;
|
|
|
+import com.diagbot.exception.CommonErrorCode;
|
|
|
+import com.diagbot.exception.CommonException;
|
|
|
+import com.diagbot.facade.LoginLogFacade;
|
|
|
+import com.diagbot.facade.OperationLogFacade;
|
|
|
+import com.diagbot.facade.SysHospitalSetFacade;
|
|
|
+import com.diagbot.util.AddressUtils;
|
|
|
+import com.diagbot.util.HttpUtils;
|
|
|
+import com.diagbot.util.IpUtils;
|
|
|
+import com.diagbot.util.ListUtil;
|
|
|
+import com.diagbot.util.StringUtil;
|
|
|
+import com.diagbot.util.SysUserUtils;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import eu.bitwalker.useragentutils.UserAgent;
|
|
|
+import org.apache.commons.collections4.MapUtils;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.aspectj.lang.JoinPoint;
|
|
|
+import org.aspectj.lang.annotation.AfterReturning;
|
|
|
+import org.aspectj.lang.annotation.Aspect;
|
|
|
+import org.aspectj.lang.annotation.Before;
|
|
|
+import org.aspectj.lang.annotation.Pointcut;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import sun.plugin.cache.OldCacheEntry;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @Description:日志记录处理
|
|
|
+ * @author: songxl
|
|
|
+ * @time: 2022/5/09 14:28
|
|
|
+ */
|
|
|
+
|
|
|
+@Aspect
|
|
|
+@Component
|
|
|
+public class LogAspect {
|
|
|
+ @Autowired
|
|
|
+ LoginLogFacade loginLogFacade;
|
|
|
+ @Autowired
|
|
|
+ SysHospitalSetFacade sysHospitalSetFacade;
|
|
|
+ @Autowired
|
|
|
+ OperationLogFacade operationLogFacade;
|
|
|
+
|
|
|
+ // 操作配置织入点
|
|
|
+ @Pointcut("execution(public * com.diagbot.web.*.*(..))" +
|
|
|
+ "&& !execution(public * com.diagbot.web.SysUserController.getJwt(..))"+
|
|
|
+ "&& !execution(public * com.diagbot.web.SysUserController.getCaptcha(..))"+
|
|
|
+ "&& !execution(public * com.diagbot.web.SysUserController.getJwtNoPass(..))"+
|
|
|
+ "&& !execution(public * com.diagbot.web.SysUserController.getHospitalMark(..))"+
|
|
|
+ "&& !execution(public * com.diagbot.web.SysUserController.midifyPassword(..))"
|
|
|
+ )
|
|
|
+ public void operPointCut() {
|
|
|
+ }
|
|
|
+
|
|
|
+ // 登录织入点
|
|
|
+ @Pointcut("execution(public * com.diagbot.web.SysUserController.getJwt(..))")
|
|
|
+ public void loginAfterReturning() {
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 登录成功之后执行
|
|
|
+ *
|
|
|
+ * @param
|
|
|
+ */
|
|
|
+ @AfterReturning(pointcut = "loginAfterReturning()", returning = "jsonResult")
|
|
|
+ public void loginAfterReturning(JoinPoint joinPoint, Object jsonResult) {
|
|
|
+ loginLogHandle(joinPoint, jsonResult);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 操作之前执行
|
|
|
+ *
|
|
|
+ * @param
|
|
|
+ */
|
|
|
+ @Before(value = "operPointCut()")
|
|
|
+ public void operPointCut(JoinPoint joinPoint) {
|
|
|
+ operationLogHandler(joinPoint);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void operationLogHandler(JoinPoint joinPoint) {
|
|
|
+ try {
|
|
|
+ //1.去sys_hospital_set表中拿所有需要进行操作日志记录的url
|
|
|
+ List<SysHospitalSet> hospitalSets = sysHospitalSetFacade
|
|
|
+ .getHospitalSetByRemark(Long.parseLong(SysUserUtils.getCurrentHospitalID()),"operationLog");
|
|
|
+ if(ListUtil.isNotEmpty(hospitalSets)){
|
|
|
+ havingOperation(hospitalSets,HttpUtils.getHttpServletRequest(),joinPoint);
|
|
|
+ }
|
|
|
+ }catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void havingOperation(List<SysHospitalSet> hospitalSets, HttpServletRequest httpServletRequest,JoinPoint joinPoint) {
|
|
|
+ //1.判断该请求是否需要操作日志记录
|
|
|
+ hospitalSets.stream().forEach(hospitalSet->{
|
|
|
+ if(matchers(hospitalSet.getCode(),httpServletRequest)){
|
|
|
+ String params = getControllerMethodDescription(joinPoint);
|
|
|
+ try {
|
|
|
+ String old = getOldOperation(HttpUtils.getHttpServletRequest().getRequestURI(),params);
|
|
|
+ //对比
|
|
|
+ comparePara(old,params);
|
|
|
+ }catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ private void comparePara(String old, String params) {
|
|
|
+ //对比新旧数据变化
|
|
|
+
|
|
|
+ //拼接操作结果
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getOldOperation(String requestURI,String params) {
|
|
|
+ String out = "";
|
|
|
+ switch (requestURI){
|
|
|
+ case "/qc/cases/saveQcCases"://修改模块分值
|
|
|
+ if(StringUtil.isNotEmpty(params)){
|
|
|
+ //通过id获取模块名称以及目前分值
|
|
|
+ JSONArray paramsArr = JSONArray.parseArray(params);
|
|
|
+ QcCasesDTO old = operationLogFacade.getQcCasesById(paramsArr.getJSONObject(0).getString("id"));
|
|
|
+ out = old.toString();
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ return out;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 返回信息相同日志代码复用
|
|
|
+ *
|
|
|
+ * @param joinPoint
|
|
|
+ * @Return com.diagbot.dto.SysOperationLogDTO
|
|
|
+ */
|
|
|
+ public SysOperationLogDTO multiplexing(JoinPoint joinPoint) {
|
|
|
+
|
|
|
+ SysOperationLogDTO operationLog = new SysOperationLogDTO();
|
|
|
+ Date date = new Date();
|
|
|
+ operationLog.setOperationDate(date);
|
|
|
+ operationLog.setGmtCreate(date);
|
|
|
+ operationLog.setOperationId(SysUserUtils.getCurrentPrincipleID() == null ? 0l : Long.parseLong(SysUserUtils.getCurrentPrincipleID()));
|
|
|
+ operationLog.setOperationName(SysUserUtils.getCurrentPrinciple());
|
|
|
+ String ip = HttpUtils.getIpAddress();
|
|
|
+ if (IpUtils.isIPv4LiteralAddress(ip)) {
|
|
|
+ operationLog.setOperationIp(ip);
|
|
|
+ } else {
|
|
|
+ operationLog.setRemark("[非ipv4地址]:" + ip);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 设置方法名称
|
|
|
+ String className = joinPoint.getTarget().getClass().getName();
|
|
|
+ String methodName = joinPoint.getSignature().getName();
|
|
|
+ operationLog.setOperationMethod(className + "." + methodName + "()");
|
|
|
+ // 设置请求方式
|
|
|
+ operationLog.setOperationWay(HttpUtils.getHttpServletRequest().getMethod());
|
|
|
+ operationLog.setOperationUrl(HttpUtils.getHttpServletRequest().getRequestURI());
|
|
|
+ // 处理设置注解上的参数
|
|
|
+ try {
|
|
|
+ String params = getControllerMethodDescription(joinPoint);
|
|
|
+ if (StringUtils.isNotBlank(params)) {
|
|
|
+ operationLog.setOperationParam(params);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return operationLog;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Description:获取请求的参数
|
|
|
+ * @Param: [joinPoint, logInformation]
|
|
|
+ * @return: void
|
|
|
+ * @Author: cy
|
|
|
+ * @Date: 2021/9/2
|
|
|
+ */
|
|
|
+ private String getControllerMethodDescription(JoinPoint joinPoint) {
|
|
|
+ Map<String, String[]> map = HttpUtils.getHttpServletRequest().getParameterMap();
|
|
|
+ if (MapUtils.isNotEmpty(map)) {
|
|
|
+ String params = JSONObject.toJSONString(map);
|
|
|
+ return params;
|
|
|
+ } else {
|
|
|
+ Object args = joinPoint.getArgs();
|
|
|
+ if (null != args) {
|
|
|
+ String params = argsArrayToString(joinPoint.getArgs());
|
|
|
+ return params;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 参数拼装
|
|
|
+ *
|
|
|
+ * @Description:
|
|
|
+ * @Param: [paramsArray]
|
|
|
+ * @return: java.lang.String
|
|
|
+ * @Author: cy
|
|
|
+ * @Date: 2021/9/2
|
|
|
+ */
|
|
|
+ private String argsArrayToString(Object[] paramsArray) {
|
|
|
+ String params = "";
|
|
|
+ if (paramsArray != null && paramsArray.length > 0) {
|
|
|
+ for (int i = 0; i < paramsArray.length; i++) {
|
|
|
+ if (null != (paramsArray[i])) {
|
|
|
+ Object jsonObj = JSONObject.toJSONString(paramsArray[i]);
|
|
|
+ params += jsonObj.toString() + " ";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return params.trim();
|
|
|
+ }
|
|
|
+
|
|
|
+ public void loginLogHandle(JoinPoint joinPoint, Object jsonResult) {
|
|
|
+ // 返回参数
|
|
|
+ if (null != jsonResult) {
|
|
|
+ ObjectMapper objectWriter = new ObjectMapper();
|
|
|
+ try {
|
|
|
+ String strResult = objectWriter.writeValueAsString(jsonResult);
|
|
|
+ JSONObject JSONResult = JSONObject.parseObject(strResult);
|
|
|
+ SysLoginLogDTO loginLog = new SysLoginLogDTO();
|
|
|
+ Date date = new Date();
|
|
|
+ loginLog.setLoginDate(date);
|
|
|
+ loginLog.setGmtCreate(date);
|
|
|
+ String token = JSONResult.getJSONObject("data").get("accessToken").toString();
|
|
|
+ DecodedJWT jwt = decodedJWT(token);
|
|
|
+ Map<String, Claim> claims = jwt.getClaims();
|
|
|
+ Claim claimUID = claims.get("user_id");
|
|
|
+ Claim claimUName = claims.get("user_name");
|
|
|
+ loginLog.setLoginId(claimUID.asLong());
|
|
|
+ loginLog.setLoginName(claimUName.asString());
|
|
|
+ loginLog.setLoginIp(HttpUtils.getIpAddress() == null ? "" : HttpUtils.getIpAddress());
|
|
|
+ UserAgent userAgent = UserAgent.parseUserAgentString(HttpUtils.getHttpServletRequest().getHeader("User-Agent"));
|
|
|
+ loginLog.setLoginAddress(AddressUtils.getRealAddressByIP(loginLog.getLoginIp()));
|
|
|
+ // 获取客户端浏览器
|
|
|
+ String browser = userAgent.getBrowser().getName();
|
|
|
+ loginLog.setLoginBrowser(browser);
|
|
|
+
|
|
|
+ //插入登录日志
|
|
|
+ loginLogFacade.getBaseMapper().addLoginLog(loginLog);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static DecodedJWT decodedJWT(String token) {
|
|
|
+ try {
|
|
|
+ DecodedJWT jwt = JWT.decode(token);
|
|
|
+ return jwt;
|
|
|
+ } catch (JWTDecodeException var2) {
|
|
|
+ var2.printStackTrace();
|
|
|
+ throw new CommonException(CommonErrorCode.ANALYZER_TOKEN_FAIL);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private boolean matchers(String url,HttpServletRequest request) {
|
|
|
+ AntPathRequestMatcher matcher = new AntPathRequestMatcher(url);
|
|
|
+ if (matcher.matches(request)) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|