CZT
2023-09-06 71c4fa1e27f75ae4b765c95c67a3069c84dc72ba
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package com.ld.igds.web;
 
import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.bdf2.core.model.DefaultUser;
import com.bstek.dorado.core.Configure;
import com.google.code.kaptcha.Constants;
import com.ld.igds.constant.BizType;
import com.ld.igds.io.constant.OrderRespEnum;
import com.ld.igds.io.notify.NotifyWebInvoker;
import com.ld.igds.log.service.HLogLoginService;
import com.ld.igds.sys.service.SysUserService;
import com.ld.igds.util.ContextUtil;
import com.ld.igds.util.FilesUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.web.WebAttributes;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
 
/**
 * 登录接口
 *
 * @author Andy
 */
@Slf4j
@Controller
@RequestMapping
public class LoginController {
 
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private NotifyWebInvoker notifyWebInvoker;
    @Resource
    private HLogLoginService loginService;
 
    /**
     * 登录验证入口
     *
     * @param username
     * @param password
     * @param captcha
     * @return
     */
    @PostMapping("/login-check")
    public String login(HttpServletRequest request,
                        @RequestParam(name = "username_") String username,
                        @RequestParam(name = "password_") String password,
                        @RequestParam(name = "captcha_") String captcha) {
 
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
            return "redirect:/login?tag=01&username=" + username;
        }
 
        boolean tag = Configure.getBoolean("bdf2.useCaptchaForLogin");
        if (tag) {
            if (StringUtils.isEmpty(captcha)) {
                return "redirect:/login?tag=02&username=" + username;
            }
            String sessionCaptcha = (String) ContextHolder.getHttpSession().getAttribute(Constants.KAPTCHA_SESSION_KEY);
            if (StringUtils.isEmpty(sessionCaptcha)) {
                return "redirect:/login?tag=03&username=" + username;
            }
            if (!captcha.equals(sessionCaptcha)) {
                return "redirect:/login?tag=04&username=" + username;
            }
        }
 
 
        //判断限制登陆
        int num = loginService.checkLoginLimit(request, username);
        if (num >= 3) {
            return "redirect:/login?tag=11&username=" + username;
        }
 
        DefaultUser user = sysUserService.loadUser(username);
        if (null == user) {
            num = loginService.addNoUser(request, username);
            if (num >= 3) {
                return "redirect:/login?tag=12&username=" + username;
            }
            return "redirect:/login?tag=05&username=" + username;
        }
 
        if (!user.isEnabled()) {//用户离职
            return "redirect:/login?tag=07&username=" + username;
        }
 
        tag = sysUserService.checkPassword(username, password, user);
        if (!tag) {
            num = loginService.addPwdError(request, user);
            if (num >= 3) {
                return "redirect:/login?tag=11&username=" + username;
            }
            return "redirect:/login?tag=06&username=" + username;
        }
 
        String subDeptId = ContextUtil.subDeptId(user);
 
        //添加通知原登录人员被踢
        notifyWebInvoker.notifyWeb(user.getCompanyId(), subDeptId, user.getUsername(), OrderRespEnum.LOGOUT, BizType.SYS, "您的账号在其他地方登录,被迫下线");
 
        loginService.addLoginInfo(request, user);
 
        //直接定向到系统验证方法
        return "forward:/security_check_?username_=" + username + "&password_=" + password + "&captcha_=" + captcha;
    }
 
    /**
     * 登录页面
     *
     * @return
     */
    @RequestMapping("/login")
    public ModelAndView login(
            @RequestParam(name = "tag", required = false) String tag,
            @RequestParam(name = "username", required = false) String username) {
 
        ModelAndView view = new ModelAndView();
 
        // 设置logo-title
        String logName = FilesUtil.getLogoTitleByCompanyId(ContextUtil.getDefaultCompanyId());
        view.addObject("logoTitle", "./static/img/" + logName);
 
        if (StringUtils.isNotEmpty(tag)) {
            if ("01".equals(tag)) {
                tag = "用户名和密码不能为空";
            }
            if ("02".equals(tag)) {
                tag = "验证码不能为空";
            }
            if ("03".equals(tag)) {
                tag = "验证码过期,请重新输入";
            }
            if ("04".equals(tag)) {
                tag = "验证码不正确";
            }
            if ("05".equals(tag)) {
                tag = "系统不存在当前用户";
            }
            if ("06".equals(tag)) {
                tag = "用户密码错误";
            }
            if ("07".equals(tag)) {
                tag = "当前用户已离职";
            }
            if ("10".equals(tag)) {
                tag = "您的账号在其他地方登录,被迫下线";
            }
            if ("11".equals(tag)) {
                tag = "连续3次错误,限制登陆10分钟";
            }
            if ("12".equals(tag)) {
                tag = "连续3次输入不存在账号,限制登陆10分钟";
            }
            view.addObject("TAG", tag);
            view.addObject("USERNAME", username);
        }
 
        // 调整使用系统异常信息
        Exception exp = (Exception) ContextHolder.getHttpSession()
                .getAttribute(WebAttributes.AUTHENTICATION_EXCEPTION);
 
        if (null == exp) {
            exp = (Exception) ContextHolder.getRequest().getAttribute(
                    WebAttributes.AUTHENTICATION_EXCEPTION);
        }
        if (null != exp) {
 
            log.error("===登陆异常原因==={}", exp);
 
            if ("Bad credentials".equals(exp.getMessage())) {
                view.addObject("TAG", "用户名和密码错误!!");
            } else if ("The password is invalid".equals(exp.getMessage())) {
                view.addObject("TAG", "密码错误!!");
            } else {
                view.addObject("TAG", "登陆验证异常,请重新尝试");
            }
        }
 
        view.setViewName("admin/login/login");
        return view;
    }
 
    /**
     * 登出
     *
     * @return
     */
    @RequestMapping("/log-out")
    public String logOut() {
        log.info("======用户退出系统========{}", ContextUtil.getLoginUserCName());
        ContextHolder.getHttpSession().removeAttribute(ContextHolder.LOGIN_USER_SESSION_KEY);
        return "redirect:/login";
    }
 
}