CZT
2024-03-05 e2a064410d4ea573bae6e8bf96da378c8ad0e809
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
package com.fzzy.web;
 
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.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
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 {
 
    /**
     * 登录验证入口
     *
     * @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 validateCode = CaptchaUtil.ver(captcha, request);
        log.info("-----------验证码-----------{}", validateCode);
 
        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 = "您的账号在其他地方登录,被迫下线";
            }
            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 {
            //生成普通验证码
            // SpecCaptcha specCaptcha = new SpecCaptcha();
            //生成算数验证码
            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() {
        return "redirect:/login";
    }
 
}