| | |
| | | package com.nq.controller; |
| | | |
| | | import com.google.code.kaptcha.Producer; |
| | | import com.nq.utils.captcha.CaptchaUtil; |
| | | import java.awt.image.BufferedImage; |
| | | import java.io.IOException; |
| | | import javax.imageio.ImageIO; |
| | |
| | | } |
| | | |
| | | @RequestMapping({"getCode.do"}) |
| | | public ModelAndView getCode(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "timestamp", required = false) String timestamp) throws IOException { |
| | | public ModelAndView getCode(HttpServletRequest request, HttpServletResponse response, |
| | | @RequestParam(value = "timestamp", required = false) String timestamp, |
| | | @RequestParam(value = "token", required = false) String token) throws IOException { |
| | | ModelAndView mv = new ModelAndView(); |
| | | if (StringUtils.isEmpty(timestamp)) { |
| | | mv.addObject("timestamp", Long.valueOf(System.currentTimeMillis())); |
| | |
| | | response.addHeader("Cache-Control", "post-check=0, pre-check=0"); |
| | | response.setHeader("Pragma", "no-cache"); |
| | | response.setContentType("image/jpeg"); |
| | | String capText = this.captchaProducer.createText(); |
| | | String capText; |
| | | if (StringUtils.isNotBlank(token)) { |
| | | String trimmedToken = token.trim(); |
| | | String existing = CaptchaUtil.get(trimmedToken); |
| | | if (StringUtils.isNotBlank(existing)) { |
| | | capText = existing; |
| | | } else { |
| | | capText = this.captchaProducer.createText(); |
| | | CaptchaUtil.save(trimmedToken, capText); |
| | | log.info("图形验证码已写入Redis token={}", trimmedToken); |
| | | } |
| | | } else { |
| | | capText = this.captchaProducer.createText(); |
| | | HttpSession session = request.getSession(); |
| | | session.removeAttribute("KAPTCHA_SESSION_KEY"); |
| | | session.setAttribute("KAPTCHA_SESSION_KEY", capText); |
| | | } |
| | | BufferedImage bi = this.captchaProducer.createImage(capText); |
| | | ServletOutputStream out = response.getOutputStream(); |
| | | ImageIO.write(bi, "jpg", out); |
| | |
| | | //验证码 |
| | | @RequestMapping({"checkCode.do"}) |
| | | @ResponseBody |
| | | public String checkCode(@RequestParam(value = "timestamp", required = false) String timestamp, @RequestParam(value = "code", required = false) String code, HttpServletRequest request) { |
| | | public String checkCode(@RequestParam(value = "timestamp", required = false) String timestamp, |
| | | @RequestParam(value = "code", required = false) String code, |
| | | @RequestParam(value = "token", required = false) String token, |
| | | HttpServletRequest request) { |
| | | boolean returnStr = false; |
| | | if (StringUtils.isNotBlank(token)) { |
| | | returnStr = CaptchaUtil.verifyAndRemove(token.trim(), code); |
| | | } else { |
| | | HttpSession session = request.getSession(); |
| | | String original = (String) session.getAttribute("KAPTCHA_SESSION_KEY"); |
| | | log.info("======用户输入的验证码:" + code); |
| | |
| | | if (StringUtils.isNotEmpty(code) && code.equalsIgnoreCase(original)) { |
| | | returnStr = true; |
| | | } |
| | | } |
| | | return returnStr + ""; |
| | | } |
| | | } |