package com.fzzy.web; 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; /** * 登录验证入口 * * @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; } SysUser user = userPR.listById(username); if (null == user) { return "redirect:/login?tag=05&username=" + username; } boolean tag = userPR.checkPassword(password, user); if (!tag){ return "redirect:/login?tag=06&username=" + username; } request.getSession().setAttribute("user", 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 = "您的账号在其他地方登录,被迫下线"; } 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"; } }