jyy
2022-04-15 f57554f7da5e4d05b4b4bab99bf49ac9ca8c2038
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
package com.matrix.system.common.authority.strategy;
 
import com.matrix.core.constance.SystemErrorCode;
import com.matrix.core.exception.GlobleException;
import com.matrix.core.tools.EncrypUtil;
import com.matrix.core.tools.LogUtil;
import com.matrix.core.tools.StringUtils;
import com.matrix.system.common.bean.SysUsers;
import com.matrix.system.common.constance.AppConstance;
import com.matrix.system.common.constance.AppMessageCode;
import com.matrix.system.common.service.SysUsersService;
 
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
 
/**
 * 通过认证中心登录
 * 
 * @author JIANGYOUYAO
 * @email 935090232@qq.com
 * @date 2017年12月9日
 */
public class SsoAccountPwdLoginStrategy implements LoginStrategy {
 
    private SysUsersService sysUsersService;
 
    private SysUsers user;
 
    public SsoAccountPwdLoginStrategy(SysUsers user, SysUsersService sysUsersService) {
        this.user = user;
        this.sysUsersService = sysUsersService;
    }
 
    @Override
    public Object login() {
 
        // 是否同时有账号和密码
        if (StringUtils.isBlank(user.getSuAccount()) || StringUtils.isBlank(user.getSuPassword())) {
            throw new GlobleException(AppMessageCode.User.ACCOUNT_PASSWORD_MUST_REQUEST);
        }
        // 根据账号查询用户
        SysUsers userQuery = new SysUsers();
        userQuery.setSuAccount(user.getSuAccount());
        List<SysUsers> users = sysUsersService.findByModel(userQuery);
        if (users.size() != 1) {
            LogUtil.error("{}账号不唯一,登录失败", null, user.getSuAccount());
            throw new GlobleException(SystemErrorCode.SYSTEM_RUNNING_ERROR);
        }
        userQuery = users.get(0);
        String pwdSource = user.getSuPassword() + userQuery.getSuRegisterTime().getTime();
        try {
            if (!userQuery.getSuPassword().equals(EncrypUtil.getSha1(pwdSource))) {
                // 账号密码错误
                throw new GlobleException(AppMessageCode.User.ACCOUNT_PASSWORD_ERROR);
            }
        } catch (UnsupportedEncodingException | NoSuchAlgorithmException e) {
            LogUtil.error("密码加密方法出错", e, user.getSuAccount());
            throw new GlobleException(SystemErrorCode.SYSTEM_RUNNING_ERROR);
        }
        // 后台已经删除账号
        if (userQuery.getSuValid().equals(AppConstance.RECORD_INVALID)) {
            throw new GlobleException(AppMessageCode.User.ACCOUNT_IS_DELETED);
        }
 
        if (StringUtils.isBlank(userQuery.getSuAccountStatus())) {
            LogUtil.info("用户状态为空登录失败");
            throw new GlobleException(AppMessageCode.User.ACCOUNT_NOT_ACTIVE);
        }
 
        // 判断用户的状态
        switch (userQuery.getSuAccountStatus()) {
 
        case AppConstance.ACCOUNT_STATUS_INACTIVATED:
            throw new GlobleException(AppMessageCode.User.ACCOUNT_NOT_ACTIVE);
        case AppConstance.ACCOUNT_STATUS_LOCKED:
            throw new GlobleException(AppMessageCode.User.ACCOUNT_IS_LOCK);
        default:
            // 登录成功
            return userQuery;
        }
 
    }
 
}