k94314517
2024-05-29 c36020f0ed52cf80081b134fc908254f8fe78ffc
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package com.doumee.service.system.impl;
 
import com.doumee.core.exception.BusinessException;
import com.doumee.core.model.LoginUserInfo;
import com.doumee.core.constants.ResponseStatus;
import com.doumee.core.utils.Utils;
import com.doumee.dao.business.dao.CompanyMapper;
import com.doumee.dao.business.model.Company;
import com.doumee.dao.system.dto.LoginDTO;
import com.doumee.dao.system.model.*;
import com.doumee.service.common.CaptchaService;
import com.doumee.service.system.*;
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.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Service;
 
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Objects;
 
@Slf4j
@Service
public class SystemLoginServiceImpl implements SystemLoginService {
 
    @Value("${project.version}")
    private String systemVersion;
    @Value("${debug_model}")
    private Boolean isDebug;
    @Lazy
    @Autowired
    private SystemUserService systemUserService;
 
    @Lazy
    @Autowired
    private SystemDataPermissionService systemDataPermissionService;
    @Lazy
    @Autowired
    private SystemRoleService systemRoleService;
 
    @Lazy
    @Autowired
    private SystemPermissionService systemPermissionService;
 
    @Autowired
    private CaptchaService captchaService;
 
    @Autowired
    private SystemLoginLogService systemLoginLogService;
 
    @Autowired
    private CompanyMapper companyMapper;
 
    @Override
    public String loginByPassword(LoginDTO dto, HttpServletRequest 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((isDebug == null  || !isDebug) && (Objects.isNull(dto.getCheckCode()) || dto.getCheckCode()) ){
            // 校验验证码
            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(Boolean.FALSE);
                systemLoginLogService.create(loginLog);
                throw e;
            }
        }
        // 校验用户名和密码
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(dto.getUsername(), dto.getPassword());
        try {
            subject.login(token);
            loginLog.setUserId(((LoginUserInfo)subject.getPrincipal()).getId());
            loginLog.setSuccess(Boolean.TRUE);
            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(Boolean.FALSE);
            systemLoginLogService.create(loginLog);
            throw new BusinessException(ResponseStatus.ACCOUNT_INCORRECT);
        }
    }
 
    @Override
    public LoginUserInfo loginByPasswordNew(LoginDTO dto, ServerHttpRequest request) {
        SystemLoginLog loginLog = new SystemLoginLog();
        loginLog.setLoginUsername(dto.getUsername());
        loginLog.setLoginTime(new Date());
        loginLog.setSystemVersion(systemVersion);
        loginLog.setLocation(Utils.Location.getLocationString(loginLog.getIp()));
        if(request!=null&&request.getHeaders()!=null && request.getHeaders().size()>0){
            loginLog.setIp(Utils.User_Client.getIP(request));
            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(isDebug == null  || !isDebug){
            // 校验验证码
            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(Boolean.FALSE);
                systemLoginLogService.create(loginLog);
                throw new BusinessException(ResponseStatus.ACCOUNT_INCORRECT.getCode(),"对不起,验证码不正确!");
            }
        }
 
        // 根据用户名查询用户对象
        SystemUser queryDto = new SystemUser();
        queryDto.setUsername(dto.getUsername());
        queryDto.setDeleted(Boolean.FALSE);
        SystemUser user = systemUserService.findOne(queryDto);
        if (user == null) {
            throw new BusinessException(ResponseStatus.ACCOUNT_INCORRECT);
        }
        String pwd = Utils.Secure.encryptPassword(new String(dto.getPassword()), user.getSalt());
        // 比较密码
        if( !StringUtils.equals(pwd, user.getPassword())){
            throw new BusinessException(ResponseStatus.ACCOUNT_INCORRECT);
        }
        Company company = new Company();
        if(Objects.nonNull(user.getCompanyId())){
            company = companyMapper.selectById(user.getCompanyId());
        }
        // 获取登录用户信息
        List<SystemRole> roles = systemRoleService.findByUserId(user.getId());
        List<SystemPermission> permissions = systemPermissionService.findByUserId(user.getId());
 
        SystemRole rt = new SystemRole();
        rt.setDeleted(Boolean.FALSE);
        //数据部门权限集合
        user.setCompanyIdList(systemDataPermissionService.selectHighRole(new SystemDataPermission(),rt,user));
 
        LoginUserInfo userInfo = LoginUserInfo.from(user, roles, permissions,company,null);
        return  userInfo;
    }
 
}