Commit f85f39f3 by shenjunjie

Merge branch 'feature' into 'dev'

权限部分调整6

See merge request !285
parents 05e95b48 8f7d2c14
......@@ -16,6 +16,8 @@ import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
......@@ -31,7 +33,6 @@ import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.*;
import static java.util.Objects.nonNull;
......@@ -45,6 +46,8 @@ import static java.util.Objects.nonNull;
@Aspect
@Component
public class AopLogRecord {
public static final Logger log = LogManager.getLogger(AopLogRecord.class);
@Value("${jwt.key}")
private String jwtKey;
@Resource(name = "userServiceImpl")
......@@ -58,57 +61,65 @@ public class AopLogRecord {
private static final List<String> URL_PATTERNS = Arrays.asList("/getNewAll", "/getNew", "/schedule");
@AfterReturning(value = "execution(public * com..controller..admin..*Controller.*(..)) || execution(* com..controller..app..*Controller.*(..)) || execution(* com..controller..LoginController.checkBind())", returning = "ResponseResult")
private void beforeLog(JoinPoint joinPoint, ResponseResult ResponseResult) throws UnknownHostException, ClassNotFoundException{
long now = System.currentTimeMillis();
// RequestContextHolder 顾名思义 持有 request 上下文的容器
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
// 获取request对象
HttpServletRequest request = Objects.requireNonNull(requestAttributes).getRequest();
// webRequest拿路径的uri和request拿的uri有区别
NativeWebRequest webRequest = new ServletWebRequest(request);
Object webRequestAttribute = webRequest.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST);
if (filterUrl(webRequestAttribute)) {
// controller接口信息
String httpMethod = request.getMethod();
String uri = request.getRequestURI();
String methodName = joinPoint.getSignature().getName();
// 访问页面
String className = joinPoint.getTarget().getClass().getName();
Class<?> clazz = Class.forName(className);
String controller = clazz.getAnnotation(Api.class).tags()[0];
// 操作模块
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
String method = methodSignature.getMethod().getAnnotation(ApiOperation.class).value();
// 前后台,false:前台,true:后台
boolean backstage = uri.contains("admin");
if (!(Tools.isNullOrUndefined(request.getHeader(jwtKey)) || Tools.isNullOrUndefined(request.getHeader("pid")))) {
Map<String, Object> map = JwtUtil.unsign(request.getHeader(jwtKey), Map.class);
if (null == map) {
return;
}
Object userIdObj = map.get(GenericAttribute.USER_ID);
// 无用户信息不做记录
if (null == userIdObj) {
return;
}
String userId = userIdObj.toString();
String projectId = request.getHeader("pid");
UserInfo userInfo = userService.queryUserInfo(userId, projectId);
String nickName = userInfo.getNickname();
// 请求地址和服务器地址
String severAddress = InetAddress.getLocalHost().getHostAddress();
String ipAddress = Tools.getIpAddress(request);
// 接口传参信息
List<Map<String, Object>> arguments = getRequestArguments(joinPoint);
Behavior behavior = new Behavior(userId, projectId, nickName, ipAddress, now, controller, method, backstage,
uri, methodName, httpMethod, arguments, now, severAddress, null);
UserLogRecord userLogRecord = userLogRecord(projectId, userInfo, joinPoint, methodSignature, ResponseResult);
if (Objects.nonNull(userLogRecord)) {
userLogRecordDao.insertOne(userLogRecord);
private void beforeLog(JoinPoint joinPoint, ResponseResult ResponseResult){
try {
long now = System.currentTimeMillis();
// RequestContextHolder 顾名思义 持有 request 上下文的容器
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
// 获取request对象
HttpServletRequest request = Objects.requireNonNull(requestAttributes).getRequest();
// webRequest拿路径的uri和request拿的uri有区别
NativeWebRequest webRequest = new ServletWebRequest(request);
Object webRequestAttribute = webRequest.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST);
if (filterUrl(webRequestAttribute)) {
// controller接口信息
String httpMethod = request.getMethod();
String uri = request.getRequestURI();
String methodName = joinPoint.getSignature().getName();
// 访问页面
String className = joinPoint.getTarget().getClass().getName();
Class<?> clazz = Class.forName(className);
String controller = clazz.getAnnotation(Api.class).tags()[0];
// 操作模块
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
String method = methodSignature.getMethod().getAnnotation(ApiOperation.class).value();
// 前后台,false:前台,true:后台
boolean backstage = uri.contains("admin");
if (!(Tools.isNullOrUndefined(request.getHeader(jwtKey)) || Tools.isNullOrUndefined(request.getHeader("pid")))) {
Map<String, Object> map = JwtUtil.unsign(request.getHeader(jwtKey), Map.class);
if (null == map) {
return;
}
Object userIdObj = map.get(GenericAttribute.USER_ID);
// 无用户信息不做记录
if (null == userIdObj) {
return;
}
String userId = userIdObj.toString();
String projectId = request.getHeader("pid");
UserInfo userInfo = userService.queryUserInfo(userId, projectId);
if (null == userInfo) {
log.info("userInfo为null,map:{},userId:{},projectId:{}", JSONObject.toJSONString(map), request.getHeader(jwtKey), request.getHeader("pid"));
return;
}
String nickName = userInfo.getNickname();
// 请求地址和服务器地址
String severAddress = InetAddress.getLocalHost().getHostAddress();
String ipAddress = Tools.getIpAddress(request);
// 接口传参信息
List<Map<String, Object>> arguments = getRequestArguments(joinPoint);
Behavior behavior = new Behavior(userId, projectId, nickName, ipAddress, now, controller, method, backstage,
uri, methodName, httpMethod, arguments, now, severAddress, null);
UserLogRecord userLogRecord = userLogRecord(projectId, userInfo, joinPoint, methodSignature, ResponseResult);
if (Objects.nonNull(userLogRecord)) {
userLogRecordDao.insertOne(userLogRecord);
}
String collectionName = behaviorDao.generateCollectionName();
behaviorDao.insertOneWithoutId(behavior, collectionName);
}
String collectionName = behaviorDao.generateCollectionName();
behaviorDao.insertOneWithoutId(behavior, collectionName);
}
} catch (Exception e) {
log.error("beforeLog", e);
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment