mitao
2025-02-21 31573d6180d15ef65ed0df9c2732495f40b12663
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
package com.panzhihua.auth.manager;
 
import java.util.TimerTask;
 
import com.panzhihua.auth.model.dos.SysLogininforDO;
import com.panzhihua.auth.model.dos.SysOperLogDO;
import com.panzhihua.common.constants.Constants;
import com.panzhihua.common.utlis.AddressUtils;
import com.panzhihua.common.utlis.IpUtils;
import com.panzhihua.common.utlis.LogUtils;
import com.panzhihua.common.utlis.ServletUtils;
 
import eu.bitwalker.useragentutils.UserAgent;
import lombok.extern.slf4j.Slf4j;
 
/**
 * @program: springcloud_k8s_panzhihuazhihuishequ
 * @description: 异步工厂
 * @author: huang.hongfa weixin hhf9596 qq 959656820
 * @create: 2020-11-20 15:28
 **/
@Slf4j
public class AsyncFactoryNew {
 
    /**
     * 记录登录信息
     *
     * @param username
     *            用户名
     * @param status
     *            状态
     * @param message
     *            消息
     * @param args
     *            列表
     * @return 任务task
     */
    public static TimerTask recordLogininfor(final String username, final String status, final String message,
        final Object... args) {
        final UserAgent userAgent = UserAgent.parseUserAgentString(ServletUtils.getRequest().getHeader("User-Agent"));
        final String ip = IpUtils.getIpAddr(ServletUtils.getRequest());
        return new TimerTask() {
            @Override
            public void run() {
                String address = AddressUtils.getRealAddressByIP(ip);
                StringBuilder s = new StringBuilder();
                s.append(LogUtils.getBlock(ip));
                s.append(address);
                s.append(LogUtils.getBlock(username));
                s.append(LogUtils.getBlock(status));
                s.append(LogUtils.getBlock(message));
                // 打印信息到日志
                log.info(s.toString(), args);
                // 获取客户端操作系统
                String os = userAgent.getOperatingSystem().getName();
                // 获取客户端浏览器
                String browser = userAgent.getBrowser().getName();
                // 封装对象
                SysLogininforDO logininfor = new SysLogininforDO();
                logininfor.setUserName(username);
                logininfor.setIpaddr(ip);
                logininfor.setLoginLocation(address);
                logininfor.setBrowser(browser);
                logininfor.setOs(os);
                logininfor.setMsg(message);
                // 日志状态
                if (Constants.LOGIN_SUCCESS.equals(status) || Constants.LOGOUT.equals(status)) {
                    logininfor.setStatus(Constants.SUCCESS + "");
                } else if (Constants.LOGIN_FAIL.equals(status)) {
                    logininfor.setStatus(Constants.FAIL + "");
                }
                // 插入数据
                // SpringUtils.getBean(ISysLogininforService.class).insertLogininfor(logininfor);
            }
        };
    }
 
    /**
     * 操作日志记录
     *
     * @param operLog
     *            操作日志信息
     * @return 任务task
     */
    public static TimerTask recordOper(final SysOperLogDO operLog) {
        return new TimerTask() {
            @Override
            public void run() {
                // 远程查询操作地点
                operLog.setOperLocation(AddressUtils.getRealAddressByIP(operLog.getOperIp()));
                // SpringUtils.getBean(ISysOperLogService.class).insertOperlog(operLog);
            }
        };
    }
}