3
13693261870
2022-09-16 63ba114e70e380442fcbed4a5157ee52c9491216
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
136
137
138
139
140
141
142
package com.landtool.lanbase.common.aspect;
 
import java.lang.reflect.Method;
 
import javax.servlet.http.HttpServletRequest;
import org.apache.shiro.SecurityUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import com.landtool.lanbase.common.utils.HttpContextUtils;
import com.landtool.lanbase.common.utils.IPUtils;
import com.landtool.lanbase.config.LoginConfigProperties;
import com.landtool.lanbase.modules.log.entity.LogLogininfo;
import com.landtool.lanbase.modules.log.service.LogLogininfoService;
import com.landtool.lanbase.modules.org.entity.OrgUser;
import com.landtool.lanbase.modules.org.redis.OrgUserRedis;
import com.landtool.lanbase.modules.org.service.OrgUserService;
 
import eu.bitwalker.useragentutils.Browser;
import eu.bitwalker.useragentutils.UserAgent;
import eu.bitwalker.useragentutils.Version;
 
 
@Aspect
@Component
public class LogLogininfoAspect {
    @Autowired
    private LogLogininfoService logLogininfoService;
    
    @Autowired
    private OrgUserService orgUserService;
    
    @Autowired
    private LoginConfigProperties  loginConfig;
 
    @Autowired
    private OrgUserRedis orgUserRedis;
 
    
    @Pointcut("@annotation(com.landtool.lanbase.common.annotation.LogLogininfo)")
    public void logPointCut() { 
        
    }
 
    @Around("logPointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        long beginTime = System.currentTimeMillis();
        //执行方法
        Object result = point.proceed();
        //执行时长(毫秒)
        long time = System.currentTimeMillis() - beginTime;
//        System.out.println("time"+time);
        //保存日志
        saveLogLogininfo(point, time);
 
        return result;
    }
 
    private void saveLogLogininfo(ProceedingJoinPoint joinPoint, long time) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        LogLogininfo logLogininfo = new LogLogininfo();
        com.landtool.lanbase.common.annotation.LogLogininfo log = method.getAnnotation(com.landtool.lanbase.common.annotation.LogLogininfo.class);
        if(log != null){
            //注解上的描述
            if(log.value().equals("登录")){
                logLogininfo.setLogintype((long) 1);
            }else if(log.value().equals("登出")){
                logLogininfo.setLogintype((long) 2);
                logLogininfo.setRstatus((long) 0);
            }else{
                logLogininfo.setLogintype((long) 3);
            }
        }
        logLogininfo.setAppfullname(loginConfig.getAppFullName());
 
        logLogininfo.setAppid(loginConfig.getAppId());
        
       
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        String username = null;
        String methodName = method.getName();
        //登录入口并不能通过SecurityUtils获取用户名,需要特殊处理
        if(methodName.indexOf("login")>-1){
            //    通过cas登陆
            if(methodName.indexOf("caslogin")>-1){
                if(request.getRemoteUser() != null) {
                    username = request.getRemoteUser();
                }
            }
            // 普通登陆
            if(methodName.equalsIgnoreCase("login")){
                Object[] args=joinPoint.getArgs();
                if(args[0].toString() != null) {
                    username = args[0].toString();
                }
            }
            if(username !=null && username !=""){
                OrgUser user = orgUserService.queryByUserName(username);
                logLogininfo.setLoginname(username);
                if(user !=null){
                    //登陆成功
                    logLogininfo.setUserid(user.getUserid());
                    logLogininfo.setRstatus((long) 0);
                }else{
                    //未登陆或或用戶不存在系統時不寫日志
                     return ;
                }
            }else{
                //登陆失败
                logLogininfo.setLoginname("未知");
                logLogininfo.setUserid((long) -1);
                logLogininfo.setRstatus((long) 1);
            }            
        }else{
            //非登陆入口的情况,比如logout
            username = ((OrgUser) SecurityUtils.getSubject().getPrincipal()).getLoginname();
            logLogininfo.setLoginname(username);
 
            Long userid = ((OrgUser) SecurityUtils.getSubject().getPrincipal()).getUserid();
            logLogininfo.setUserid(userid);
            logLogininfo.setRstatus((long) 0);
        }
 
 
        //获取浏览器版本
        Browser bro=UserAgent.parseUserAgentString(request.getHeader("User-Agent")).getBrowser();
        Version version=bro.getVersion(request.getHeader("User-Agent"));
        String info=bro.getName()+"/"+version.getVersion();
        logLogininfo.setBroserversion(info);
        //设置IP地址
        logLogininfo.setLoginip(IPUtils.getIpAddr(request));
        
        //保存系统日志
        logLogininfoService.save(logLogininfo); 
    }
}