czt
2025-08-20 12c99c99ed89d1d11318d9b4c6c295d35e21b1e7
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
package com.fzzy.web;
 
import com.fzzy.sys.LogLoginService;
import com.fzzy.sys.UserPR;
import com.fzzy.sys.entity.SysUser;
import com.wf.captcha.ArithmeticCaptcha;
import com.wf.captcha.utils.CaptchaUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
 
/**
 * 登录入口
 *
 * @author Andy
 */
@Slf4j
@Controller
@RequestMapping
public class LoginController {
 
    @Autowired
    private UserPR userPR;
    @Autowired
    private LogLoginService logLoginService;
 
    /**
     * 登录验证入口
     *
     * @param username
     * @param password
     * @param captcha
     * @return
     */
    @GetMapping("/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;
        }
        if (StringUtils.isEmpty(captcha)) {
            return "redirect:/login?tag=02&username=" + username;
        }
 
        //校验验证码
        boolean validateCode = CaptchaUtil.ver(captcha, request);
        if (!validateCode) {
            return "redirect:/login?tag=04&username=" + username;
        }
 
        //判断限制登陆
        int num = logLoginService.checkLoginLimit(request, username);
        if (num >= 3) {
            return "redirect:/login?tag=11&username=" + username;
        }
 
        SysUser user = userPR.listById(username);
        if (null == user) {
            num = logLoginService.addNoUser(request, username);
            if (num >= 3) {
                return "redirect:/login?tag=12&username=" + username;
            }
            return "redirect:/login?tag=05&username=" + username;
        }
 
        boolean tag = userPR.checkPassword(password, user);
        if (!tag){
            num = logLoginService.addPwdError(request, user);
            if (num >= 3) {
                return "redirect:/login?tag=11&username=" + username;
            }
            return "redirect:/login?tag=06&username=" + username;
        }
        request.getSession().setAttribute("user", user);
        //增加登录日志
        logLoginService.addLoginInfo(request, user);
 
        return "redirect:/home";
    }
 
    /**
     * 登录页面
     *
     * @return
     */
    @RequestMapping("/login")
    public ModelAndView login(
            @RequestParam(name = "tag", required = false) String tag,
            @RequestParam(name = "username", required = false) String username) {
        ModelAndView view = new ModelAndView();
        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次错误,限制登陆60分钟";
            }
            if ("12".equals(tag)) {
                tag = "连续3次输入不存在账号,限制登陆60分钟";
            }
            view.addObject("TAG", tag);
            view.addObject("USERNAME", username);
        }
        view.setViewName("login/login");
        return view;
    }
 
    /**
     * 验证码
     *
     * @param response
     * @param request
     */
    @RequestMapping(value = "captcha", method = RequestMethod.GET)
    public void captcha(HttpServletResponse response, HttpServletRequest request) {
        //设置长宽
        try {
            //生成算数验证码
            ArithmeticCaptcha arithmeticCaptcha = new ArithmeticCaptcha();
            //设置2为算数
            arithmeticCaptcha.setLen(2);
            //验证码结果
            String content = arithmeticCaptcha.text();
 
            log.info("-----------系统生成验证码-----------{}", content);
 
            CaptchaUtil.out(arithmeticCaptcha, request, response);
 
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
    /**
     * 登出
     *
     * @return
     */
    @RequestMapping("/log-out")
    public String logOut(HttpServletRequest request) {
        request.getSession().removeAttribute("user");
        return "redirect:/login";
    }
 
}