luohairen
2024-10-28 e6585fbca31f7f2997ba8558a18a769a204c9ba7
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
package com.ycl.jxkg.config.spring.security;
 
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.ycl.jxkg.base.SystemCode;
import com.ycl.jxkg.constants.CaffeineConstant;
import com.ycl.jxkg.domain.entity.SysConfig;
import com.ycl.jxkg.domain.entity.UserEventLog;
import com.ycl.jxkg.domain.vo.student.user.UserLoginVO;
import com.ycl.jxkg.enums.general.YesOrNoEnum;
import com.ycl.jxkg.event.UserEvent;
import com.ycl.jxkg.mapper.ClassesUserMapper;
import com.ycl.jxkg.mapper.SysConfigMapper;
import com.ycl.jxkg.service.UserService;
import com.ycl.jxkg.utils.CaffeineUtil;
import com.ycl.jxkg.utils.DateTimeUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;
 
 
/**
 * @version 3.5.0
 * @description: 登录成功返回
 * Copyright (C), 2020-2024, 武汉思维跳跃科技有限公司
 * @date 2021/12/25 9:45
 */
@Component
public class RestAuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {
 
    private final ApplicationEventPublisher eventPublisher;
    private final UserService userService;
    private final CaffeineUtil caffeineUtil;
    private final SysConfigMapper sysConfigMapper;
    @Autowired
    private ClassesUserMapper classesUserMapper;
 
    /**
     * Instantiates a new Rest authentication success handler.
     *
     * @param eventPublisher the event publisher
     * @param userService    the user service
     */
    @Autowired
    public RestAuthenticationSuccessHandler(ApplicationEventPublisher eventPublisher, UserService userService, CaffeineUtil caffeineUtil, SysConfigMapper sysConfigMapper) {
        this.eventPublisher = eventPublisher;
        this.userService = userService;
        this.caffeineUtil = caffeineUtil;
        this.sysConfigMapper = sysConfigMapper;
    }
 
    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        Object object = authentication.getPrincipal();
        if (null != object) {
            User springUser = (User) object;
            // 登录之后保存或更新 用户名与session的关系
            String sessionId = request.getSession().getId();
            caffeineUtil.put(CaffeineConstant.AUTH, springUser.getUsername(), sessionId);
            com.ycl.jxkg.domain.entity.User user = userService.getUserByUserName(springUser.getUsername());
            if (null != user) {
                List<SysConfig> configList = new LambdaQueryChainWrapper<>(sysConfigMapper)
                        .list();
                Integer passwordExpireDays = 30;
                if (configList.size() == 1) {
                    passwordExpireDays = configList.get(0).getPasswordExpireTime();
                }
                Date now = new Date();
                // 密码过期返回强制修改密码标识
                if (DateTimeUtil.getTwoTimeDiffDay(now, user.getLastUpdatePasswordTime()) > passwordExpireDays) {
                    RestUtil.response(response, 205, "密码过期,请修改密码", user.getId());
                    return;
                }
 
                UserEventLog userEventLog = new UserEventLog(user.getId(), user.getUserName(), user.getRealName(), new Date());
                userEventLog.setContent(user.getUserName() + " 登录了江西语音视频培训系统");
                eventPublisher.publishEvent(new UserEvent(userEventLog));
                // 修改用户的最后登录时间
                user.setLastActiveTime(new Date());
                userService.updateUser(user);
                UserLoginVO userLoginVO = new UserLoginVO();
                BeanUtils.copyProperties(user,userLoginVO);
                List<String> classesNames = classesUserMapper.getClassesNameByUserId(user.getId());
                userLoginVO.setClassName(classesNames);
                RestUtil.response(response, SystemCode.OK.getCode(), SystemCode.OK.getMessage(), userLoginVO);
            }
        } else {
            RestUtil.response(response, SystemCode.UNAUTHORIZED.getCode(), SystemCode.UNAUTHORIZED.getMessage());
        }
    }
}