|
@@ -1,15 +1,21 @@
|
|
|
package com.sky.ioc.config;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
import com.sky.ioc.constant.Constant;
|
|
|
import com.sky.ioc.entity.domain.log.Log;
|
|
|
import com.sky.ioc.mapper.log.LogMapper;
|
|
|
import com.sky.ioc.tool.JwtUtil;
|
|
|
import com.sky.ioc.tool.RedisUtil;
|
|
|
import com.sky.ioc.tool.ReturnMsg;
|
|
|
+import io.swagger.annotations.Api;
|
|
|
+import io.swagger.annotations.ApiOperation;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.ArrayUtils;
|
|
|
+import org.aspectj.lang.JoinPoint;
|
|
|
import org.aspectj.lang.ProceedingJoinPoint;
|
|
|
import org.aspectj.lang.annotation.Around;
|
|
|
import org.aspectj.lang.annotation.Aspect;
|
|
|
+import org.aspectj.lang.reflect.MethodSignature;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
import org.springframework.util.StringUtils;
|
|
@@ -19,7 +25,9 @@ import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
+import java.util.Arrays;
|
|
|
import java.util.Objects;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
@Slf4j
|
|
|
@Component
|
|
@@ -45,21 +53,65 @@ public class LoginAspect {
|
|
|
String username = JwtUtil.parseJWT(token) ;
|
|
|
Object o = redisUtil.get("login:" + username);
|
|
|
if (!Objects.isNull(o)) {
|
|
|
- return joinPoint.proceed(joinPoint.getArgs());
|
|
|
+ boolean isSuccess = false;
|
|
|
+ Object obj = null;
|
|
|
+ try {
|
|
|
+ joinPoint.proceed(joinPoint.getArgs());
|
|
|
+ isSuccess = true;
|
|
|
+ } catch (Exception e) {
|
|
|
+ isSuccess = false;
|
|
|
+ }
|
|
|
+ // 获取ip
|
|
|
+ String ip = getIp(request);
|
|
|
+ this.insertLog(joinPoint, ip, username, isSuccess);
|
|
|
+ return obj;
|
|
|
}
|
|
|
}
|
|
|
return ReturnMsg.fail(Constant.MSG_CODE.LOGIN_TIMEOUT, "当前未登录");
|
|
|
}
|
|
|
|
|
|
+ private String getIp(HttpServletRequest request) {
|
|
|
+ String ip = request.getHeader("x-forwarded-for");
|
|
|
+ if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
|
|
+ ip = request.getHeader("Proxy-Client-IP");
|
|
|
+ }
|
|
|
+ if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
|
|
+ ip = request.getHeader("WL-Proxy-Client-IP");
|
|
|
+ }
|
|
|
+ if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
|
|
+ ip = request.getHeader("HTTP_CLIENT_IP");
|
|
|
+ }
|
|
|
+ if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
|
|
+ ip = request.getHeader("HTTP_X_FORWARDED_FOR");
|
|
|
+ }
|
|
|
+ if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
|
|
+ ip = request.getRemoteAddr();
|
|
|
+ }
|
|
|
+ if (StringUtils.hasText(ip) && ip.contains(",")) {
|
|
|
+ String[] ipArray = ip.split(",");
|
|
|
+ if (ArrayUtils.isNotEmpty(ipArray)) {
|
|
|
+ ip = ipArray[0];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return ip;
|
|
|
+ }
|
|
|
|
|
|
- private boolean insertLog() {
|
|
|
+ /** 插入日志 */
|
|
|
+ private boolean insertLog(JoinPoint joinPoint, String ip, String userName, boolean isSuccess) {
|
|
|
Log log1 = new Log();
|
|
|
- log1.setModule("");
|
|
|
- log1.setOperation_type("");
|
|
|
- log1.setRequest("");
|
|
|
- log1.setOperation_staff("");
|
|
|
- log1.setOperation_address("");
|
|
|
- log1.setOperation_status("");
|
|
|
+ // 获取模块名
|
|
|
+ String moduleName = Arrays.stream(joinPoint.getTarget().getClass().getAnnotation(Api.class).tags())
|
|
|
+ .collect(Collectors.joining());
|
|
|
+ // 获取方法注释
|
|
|
+ MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
|
|
|
+ String methodName = methodSignature.getMethod().getAnnotation(ApiOperation.class).value();
|
|
|
+
|
|
|
+ log1.setModule(moduleName);
|
|
|
+ log1.setOperation_type(methodName);
|
|
|
+ log1.setRequest(JSON.toJSONString(joinPoint.getArgs()));
|
|
|
+ log1.setOperation_staff(userName);
|
|
|
+ log1.setOperation_address(ip);
|
|
|
+ log1.setOperation_status(isSuccess?"0":"1");
|
|
|
log1.setOperation_time(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
|
|
|
logMapper.insert(log1);
|
|
|
return true;
|