| | |
| | | import com.dy.pmsGlobal.pojoBa.BaUser; |
| | | import com.mysql.cj.util.StringUtils; |
| | | import jakarta.servlet.http.HttpServletRequest; |
| | | import jakarta.servlet.http.HttpServletResponse; |
| | | import jakarta.servlet.http.HttpSession; |
| | | import jakarta.validation.Valid; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | |
| | | import org.springframework.validation.BindingResult; |
| | | import org.springframework.web.bind.annotation.*; |
| | | |
| | | import javax.imageio.ImageIO; |
| | | import java.awt.*; |
| | | import java.awt.image.BufferedImage; |
| | | import java.io.IOException; |
| | | import java.util.Objects; |
| | | import java.util.Random; |
| | | import java.util.UUID; |
| | | |
| | | /** |
| | |
| | | */ |
| | | @PostMapping(path = "login", consumes = MediaType.APPLICATION_JSON_VALUE)//前端提交json数据 |
| | | @Log("用户登录(json)") |
| | | public BaseResponse<UserVo> login(@RequestBody @Valid LoginVo vo, BindingResult bindingResult) { |
| | | public BaseResponse<UserVo> login(@RequestBody @Valid LoginVo vo,BindingResult bindingResult, |
| | | HttpSession session) { |
| | | try { |
| | | if(bindingResult != null && bindingResult.hasErrors()){ |
| | | return BaseResponseUtils.buildFail(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage()); |
| | | } |
| | | return this.doLogin(vo) ; |
| | | |
| | | // 从Session中获取保存的验证码 |
| | | String sessionCaptcha = (String) session.getAttribute("captcha"); |
| | | // 首先验证验证码 |
| | | if (vo.captcha != null && vo.captcha.equalsIgnoreCase(sessionCaptcha)) { |
| | | session.removeAttribute("captcha"); |
| | | return this.doLogin(vo) ; |
| | | } else { |
| | | // 验证码错误,返回登录页面并显示错误信息 |
| | | return BaseResponseUtils.buildFail("验证码错误"); |
| | | } |
| | | } catch (Exception e) { |
| | | log.error("查询一个用户数据异常", e); |
| | | log.error("用户登录异常", e); |
| | | return BaseResponseUtils.buildException(e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 客户端请求用户登录,客户端提交form表单 |
| | | * @param vo 登录用户form表单对象 |
| | | * @param loginVo 登录用户form表单对象 |
| | | * @return 登录用户值对象 |
| | | */ |
| | | @PostMapping(path = "loginForm", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)//前端提交form表单数据 |
| | | @Log("用户登录(form)") |
| | | public BaseResponse<UserVo> loginForm(@RequestBody @Valid LoginVo vo, BindingResult bindingResult){ |
| | | public BaseResponse<UserVo> loginForm(@Valid LoginVo loginVo, BindingResult bindingResult,HttpSession session){ |
| | | try{ |
| | | if(bindingResult != null && bindingResult.hasErrors()){ |
| | | return BaseResponseUtils.buildFail(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage()); |
| | | } |
| | | return this.doLogin(vo) ; |
| | | // 从Session中获取保存的验证码 |
| | | String sessionCaptcha = (String) session.getAttribute("captcha"); |
| | | // 首先验证验证码 |
| | | if (loginVo.captcha != null && loginVo.captcha.equalsIgnoreCase(sessionCaptcha)) { |
| | | session.removeAttribute("captcha"); |
| | | return this.doLogin(loginVo) ; |
| | | } else { |
| | | // 验证码错误,返回登录页面并显示错误信息 |
| | | return BaseResponseUtils.buildFail("验证码错误"); |
| | | } |
| | | } catch (Exception e) { |
| | | log.error("查询一个用户数据异常", e); |
| | | log.error("用户登录异常", e); |
| | | return BaseResponseUtils.buildException(e.getMessage()); |
| | | } |
| | | } |
| | |
| | | return BaseResponseUtils.buildException(e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * 此方法供子模块系统调用,所以不公开在API接口中 |
| | |
| | | } |
| | | return vo ; |
| | | } |
| | | |
| | | /** |
| | | * 生成登录验证码 |
| | | * @param response |
| | | * @param session |
| | | * @throws IOException |
| | | */ |
| | | @GetMapping("/captcha") |
| | | public void captcha(HttpServletResponse response, HttpSession session) throws IOException { |
| | | // 设置响应的类型格式为图片格式 |
| | | response.setContentType("image/jpeg"); |
| | | // 禁止图像缓存 |
| | | response.setHeader("Pragma", "no-cache"); |
| | | response.setHeader("Cache-Control", "no-cache"); |
| | | response.setDateHeader("Expires", 0); |
| | | |
| | | int width = 100, height = 50; |
| | | BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); |
| | | Graphics g = image.getGraphics(); |
| | | // 设定背景色 |
| | | g.setColor(Color.WHITE); |
| | | g.fillRect(0, 0, width, height); |
| | | // 设定字体 |
| | | g.setFont(new Font("Arial", Font.BOLD, 30)); |
| | | // 随机生成验证码 |
| | | String captcha = generateCaptcha(); |
| | | // 将验证码存入Session |
| | | session.setAttribute("captcha", captcha); |
| | | // 在图片上绘制验证码 |
| | | g.setColor(Color.BLACK); |
| | | g.drawString(captcha, 15, 35); |
| | | g.dispose(); |
| | | // 输出图片 |
| | | ImageIO.write(image, "JPEG", response.getOutputStream()); |
| | | } |
| | | |
| | | |
| | | ///////////////////////////////////////////////////////////////// |
| | | // |
| | | // 以下私有方法 |
| | | // |
| | | ///////////////////////////////////////////////////////////////// |
| | | |
| | | /** |
| | | * 生成四位随机数 |
| | | * @return |
| | | */ |
| | | private String generateCaptcha() { |
| | | Random r = new Random(); |
| | | return r.nextInt(9000) + 1000 + ""; |
| | | } |
| | | /** |
| | | * 用户登录 |
| | | * @param vo 登录用户form表单对象 |