MrShi
2023-12-12 9088209cdd7e3158d2a04caae05e6726ba3b3ad6
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
package com.doumee.service.system.impl;
 
import com.doumee.biz.system.SystemDictDataBiz;
import com.doumee.core.constants.Constants;
import com.doumee.core.constants.ResponseStatus;
import com.doumee.core.exception.BusinessException;
import com.doumee.core.model.LoginUserInfo;
import com.doumee.core.utils.DateUtil;
import com.doumee.core.utils.Utils;
import com.doumee.dao.system.dto.LoginDTO;
import com.doumee.dao.system.dto.WebLoginDTO;
import com.doumee.dao.system.model.SystemLoginLog;
import com.doumee.dao.system.model.SystemUser;
import com.doumee.service.common.CaptchaService;
import com.doumee.service.system.SystemLoginLogService;
import com.doumee.service.system.SystemLoginService;
import com.doumee.service.system.SystemUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
 
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
 
@Slf4j
@Service
public class SystemLoginServiceImpl implements SystemLoginService {
 
    @Value("${project.version}")
    private String systemVersion;
 
    @Autowired
    private CaptchaService captchaService;
 
    @Autowired
    private SystemUserService systemUserService;
 
    @Autowired
    private SystemLoginLogService systemLoginLogService;
 
 
    @Lazy
    @Autowired
    private SystemDictDataBiz systemDictDataBiz;
 
 
    @Value("${debug_model}")
    private Boolean debugModel;
 
    @Override
    public String loginByPassword(LoginDTO dto,  HttpServletRequest request) {
        return doLogin(dto,true,request);
    }
    @Override
    public String loginByPasswordWeb(WebLoginDTO dto, HttpServletRequest request) {
        LoginDTO d = new LoginDTO();
        d.setPassword(dto.getPassword());
        d.setUsername(dto.getUsername());
 
        return doLogin(d,false,request);
 
    }
 
    public String doLogin(LoginDTO dto,boolean needCheckCode, HttpServletRequest request) {
        if(StringUtils.isBlank(dto.getUsername())
                ||StringUtils.isBlank(dto.getPassword())
                ||(needCheckCode &&StringUtils.isBlank(dto.getCode()))){
            throw new BusinessException(ResponseStatus.BAD_REQUEST);
        }
 
        SystemLoginLog loginLog = new SystemLoginLog();
        loginLog.setLoginUsername(dto.getUsername());
        loginLog.setLoginTime(new Date());
        loginLog.setSystemVersion(systemVersion);
        loginLog.setIp(Utils.User_Client.getIP(request));
        loginLog.setLocation(Utils.Location.getLocationString(loginLog.getIp()));
        loginLog.setPlatform(Utils.User_Client.getPlatform(request));
        loginLog.setClientInfo(Utils.User_Client.getBrowser(request));
        loginLog.setOsInfo(Utils.User_Client.getOS(request));
        loginLog.setServerIp(Utils.Server.getIP());
        if(needCheckCode){
            // 校验验证码
            if(!debugModel) {
                try {
                    captchaService.check(dto.getUuid(), dto.getCode());
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                    loginLog.setReason(e.getMessage().length() > 200 ? (e.getMessage().substring(0, 190) + "...") : e.getMessage());
                    loginLog.setSuccess(Constants.ZERO);
                    systemLoginLogService.create(loginLog);
                    throw e;
                }
            }
        }
 
        // 校验用户名和密码
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(dto.getUsername(), dto.getPassword());
        try {
            subject.login(token);
            SystemUser u = new SystemUser();
            u.setLastLogin(DateUtil.getCurrentDate());
            u.setId(loginLog.getUserId());
            systemUserService.updateLoginDate(u);
 
            loginLog.setUserId(((LoginUserInfo)subject.getPrincipal()).getId());
            loginLog.setSuccess(Constants.ZERO);
            systemLoginLogService.create(loginLog);
 
            return (String)subject.getSession().getId();
        } catch (AuthenticationException e) {
            log.error(ResponseStatus.ACCOUNT_INCORRECT.getMessage(), e);
            loginLog.setReason(e.getMessage().length() > 200 ? (e.getMessage().substring(0, 190) + "...") : e.getMessage());
            loginLog.setSuccess(Constants.ONE);
            systemLoginLogService.create(loginLog);
            if (e.getCause() instanceof BusinessException) {
                throw new BusinessException(ResponseStatus.ACCOUNT_INCORRECT.getCode(), e.getCause().getMessage());
            }
            throw new BusinessException(ResponseStatus.ACCOUNT_INCORRECT.getCode(), "对不起,账号或密码错误!");
        }
    }
 
 
 
 
 
}