无关风月
2025-02-28 2f8e70ad2884d2b6b7443dfae0af11ae9cfc8b99
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
package com.jilongda.manage.log;
 
import com.jilongda.manage.service.LoginLogService;
import com.jilongda.common.log.OperLoginLog;
import com.jilongda.common.model.LoginLog;
import com.jilongda.common.utils.IPUtil;
import com.jilongda.manage.security.SecurityUserDetails;
import com.jilongda.manage.security.SysUserDetailsService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
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.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
 
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
 
 
/**
 * 切面处理类,登录日志记录处理
 *
 * @author xiaochen
 * @date 2021/12/21
 */
@Aspect
@Component
public class LoginLogAspect {
 
    @Autowired
    private LoginLogService loginLogService;
    @Autowired
    private SysUserDetailsService loadUserDetailsService;
 
    /**
     * 设置操作日志切入点 记录登录日志 在注解的位置切入代码
     */
    @Pointcut("@annotation(com.jilongda.common.log.OperLoginLog)")
    public void operLoginLogPoinCut() {
    }
 
    /**
     * 正常返回通知,拦截用户登录日志,连接点正常执行完成后执行, 如果连接点抛出异常,则不会执行
     *
     * @param joinPoint 切入点
     * @param keys      返回结果
     */
    @AfterReturning(value = "operLoginLogPoinCut()", returning = "keys")
    public void saveOperLog(JoinPoint joinPoint, Object keys) {
        // 获取RequestAttributes
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        // 从获取RequestAttributes中获取HttpServletRequest的信息
        HttpServletRequest request = (HttpServletRequest) requestAttributes
                .resolveReference(RequestAttributes.REFERENCE_REQUEST);
 
        LoginLog operLoginLog = new LoginLog();
        try {
            // 从切面织入点处通过反射机制获取织入点处的方法
            MethodSignature signature = (MethodSignature) joinPoint.getSignature();
            // 获取切入点所在的方法
            Method method = signature.getMethod();
            // 获取操作
            OperLoginLog opLog = method.getAnnotation(OperLoginLog.class);
            // 获取当前登录用户信息
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            String userName = null;
            if (!(authentication instanceof AnonymousAuthenticationToken)) {
                userName = authentication.getName();
            }
            if (StringUtils.hasLength(userName)) {
                // 通过用户名查询该账号信息
                SecurityUserDetails userDetails = loadUserDetailsService.loadUserByUsername(userName);
                operLoginLog.setUserName(userDetails.getNickName()); // 请求用户名称
                operLoginLog.setAccount(userDetails.getAccount()); // 请求账号
            }
            String ip = IPUtil.getIpAddress(request);
            operLoginLog.setLoginIp(ip); // 请求IP
//            operLoginLog.setLoginAddress(IPAddressUtil.getRealAddress(ip)); // 登录地址,目前服务器带不动
            String address = IPUtil.getUserLocationByIp();
            operLoginLog.setLoginAddress(address);
            operLoginLog.setWorkAddress(address); // 工作地
            operLoginLog.setLoginTime(LocalDateTime.now()); // 登录时间
            operLoginLog.setAppletOrManage(2);
            loginLogService.save(operLoginLog);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
 
    /**
     * 转换request 请求参数
     *
     * @param paramMap request获取的参数数组
     */
    public Map<String, String> converMap(Map<String, String[]> paramMap) {
        Map<String, String> rtnMap = new HashMap<String, String>();
        for (String key : paramMap.keySet()) {
            rtnMap.put(key, paramMap.get(key)[0]);
        }
        return rtnMap;
    }
 
}