UserAspect.java
4.33 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
package cn.com.glsx.entmicro.config;
import cn.com.glsx.entmicro.request.DefaultRequest;
import cn.com.glsx.entmicro.utils.IpUtils;
import cn.com.glsx.framework.core.enums.DefaultErrorEnum;
import cn.com.glsx.framework.core.exception.RpcServiceException;
import cn.com.glsx.scrm.system.dto.LoginUserDTO;
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.oreframework.boot.autoconfigure.rop.util.RopThreadLocal;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Field;
/**
* @author zhouhaibao
* @version 1.0
* @description: 用户信息统一处理
* @date 2021/7/30 10:00
*/
@Slf4j
@Aspect
@Component
public class UserAspect {
public void logPointCut() {
}
@Before("execution(* cn.com.glsx.entmicro.service..*(..))")
public void webpre(JoinPoint joinPoint) {
// 处理用户信息
try {
setUser(joinPoint);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 保存用户信息
*
* @param joinPoint
*/
public void setUser(JoinPoint joinPoint) throws Exception {
Object[] args = joinPoint.getArgs();
if (args != null && args.length != 0) {
Object object = args[0];
try {
if (object instanceof DefaultRequest) {
AbstractRopRequest accessToken = (AbstractRopRequest) object;
LoginUserDTO user = getUser();
HttpServletRequest httpServletRequest = (HttpServletRequest) accessToken.getRopRequestContext().getRawRequestObject();
String ip = IpUtils.getIpAddr(httpServletRequest);
log.info("当前登录的ip{}", ip);
log.info("商户号{}", user.getMerchantId());
log.info("createdBy{}", user.getId());
log.info("用户id{}", user.getUuid());
log.info("用户类型{}", user.getUserType());
Class userCla = (Class) object.getClass();
Class superclass = userCla.getSuperclass();
Field field = superclass.getDeclaredField("createdBy");
Field field1 = superclass.getDeclaredField("createdAccount");
Field field5 = superclass.getDeclaredField("ip");
Field field7 = superclass.getDeclaredField("merchantId");
Field field6 = superclass.getDeclaredField("datalist");
Field field8 = superclass.getDeclaredField("entmicroId");
Field field9 = superclass.getDeclaredField("uuid");
Field field10 = superclass.getDeclaredField("userType");
field.setAccessible(true);
field.set(object, user.getId());
field1.setAccessible(true);
field1.set(object, user.getAccountName());
field5.setAccessible(true);
field5.set(object, ip);
field6.setAccessible(true);
field6.set(object, user.getPermissionMerchantIds());
field7.setAccessible(true);
field7.set(object, user.getMerchantId());
field8.setAccessible(true);
field8.set(object, user.getEntmicroId());
field9.setAccessible(true);
field9.set(object, user.getUuid());
field10.setAccessible(true);
field10.set(object, user.getUserType());
}
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
throw new RpcServiceException(DefaultErrorEnum.SERVER_ERROR);
}
}
}
/**
* 获取用户信息
*
* @return
*/
public LoginUserDTO getUser() {
LoginUserDTO loginUser = RopThreadLocal.getLoginUser(LoginUserDTO.class);
if (loginUser == null) {
throw new RpcServiceException(DefaultErrorEnum.SERVER_ERROR);
}
return loginUser;
}
}