UserAspect.java
5.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package cn.com.glsx.stoassistant.config;
import cn.com.glsx.framework.core.enums.DefaultErrorEnum;
import cn.com.glsx.framework.core.exception.RpcServiceException;
import cn.com.glsx.scrm.system.model.SystemOperationLog;
import cn.com.glsx.scrm.system.model.wechatapplets.LoginUserInfoResponse;
import cn.com.glsx.scrm.system.remote.SystemLoginLogRemote;
import cn.com.glsx.stoassistant.annotation.LogSupport;
import cn.com.glsx.stoassistant.request.DefaultRopRequest;
import cn.com.glsx.stoassistant.utils.IpUtils;
import com.rop.AbstractRopRequest;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.oreframework.boot.autoconfigure.rop.util.RopThreadLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
/**
* @author zhouhaibao
* @version 1.0
* @description: 用户信息统一处理
* @date 2021/7/30 10:00
*/
@Slf4j
@Aspect
@Component
public class UserAspect {
@Autowired
private SystemLoginLogRemote systemLoginLogRemote;
public void logPointCut() {
}
@Before("execution(* cn.com.glsx.stoassistant.service..*(..))")
public void webpre(JoinPoint joinPoint) {
// 处理用户信息
try {
setUser(joinPoint);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 保存用户信息
*
* @param joinPoint
*/
public void setUser(JoinPoint joinPoint) throws Exception {
joinPoint.getTarget().getClass().getAnnotations();
Object[] args = joinPoint.getArgs();
if (args != null && args.length != 0) {
Object object = args[0];
try {
if (object instanceof DefaultRopRequest) {
AbstractRopRequest accessToken = (AbstractRopRequest) object;
LoginUserInfoResponse user = getUser();
Object target = joinPoint.getTarget();
// 第一种
HttpServletRequest httpServletRequest = (HttpServletRequest) accessToken.getRopRequestContext().getRawRequestObject();
String ip = IpUtils.getIpAddr(httpServletRequest);
log.info("当前登录的ip{}", ip);
log.info("商户号{}", user.getMerchantId());
log.info("用户id{}", user.getUuid());
log.info("用户类型{}", user.getUserType());
Class userCla = (Class) object.getClass();
Class superclass = userCla.getSuperclass();
Field field1 = superclass.getDeclaredField("createdAccount");
Field field5 = superclass.getDeclaredField("ip");
Field field7 = superclass.getDeclaredField("merchantId");
Field field9 = superclass.getDeclaredField("uuid");
field1.setAccessible(true);
field1.set(object, user.getAccountName());
field5.setAccessible(true);
field5.set(object, ip);
field7.setAccessible(true);
field7.set(object, user.getMerchantId());
field9.setAccessible(true);
field9.set(object, user.getUuid());
setLog(user, joinPoint, ip);
}
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
throw new RpcServiceException(DefaultErrorEnum.SERVER_ERROR);
}
}
}
private void setLog(LoginUserInfoResponse user, JoinPoint joinPoint, String ip) {
try {
Class<?> clazz = joinPoint.getTarget().getClass();
String methodName = joinPoint.getSignature().getName();
Class<?>[] parameterTypes = ((MethodSignature) joinPoint.getSignature()).getMethod().getParameterTypes();
Method method = clazz.getMethod(methodName, parameterTypes);
LogSupport postMapping = method.getAnnotation(LogSupport.class);
if(postMapping!=null) {
SystemOperationLog systemOperationLog = new SystemOperationLog();
systemOperationLog.setLoginIp(ip);
systemOperationLog.setMerchantId(user.getMerchantId());
systemOperationLog.setMerchantName(user.getMerchantName());
systemOperationLog.setOperateMethod(postMapping.method());
systemOperationLog.setOperateModule(postMapping.module());
systemOperationLog.setLoginName(user.getPhone());
systemOperationLog.setUserType(2);
systemLoginLogRemote.insertOperationLog(systemOperationLog);
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
/**
* 获取用户信息
*
* @return
*/
public LoginUserInfoResponse getUser() {
LoginUserInfoResponse loginUser = RopThreadLocal.getLoginUser(LoginUserInfoResponse.class);
if (loginUser == null) {
log.info("系统异常,未查到登录信息");
throw new RpcServiceException(DefaultErrorEnum.SERVER_ERROR);
}
return loginUser;
}
}