前后端分离
hutool工具生成验证码
@RestController
@RequestMapping("/hutool")
public class HutoolIdentifyCodeController {
private final Logger _logger = LoggerFactory.getLogger(PvpaUserController.class);
/**
* 生成图片验证码
* @param response
*/
@GetMapping("/getImage")
public void ImgCode(HttpSession session, HttpServletResponse response){
LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100,4,20);
//图片长宽,验证码个数,以及干扰线
try {
session.setAttribute("CAPTCHA_KEY", lineCaptcha.getCode());
//通过session保存验证码到服务器
response.setContentType("image/png");
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expire", 0);
lineCaptcha.write(response.getOutputStream());
}catch (Exception e){
_logger.error(e.getMessage());
}
}
/**
* 对验证码进行输入验证
*/
@RequestMapping(value = "/getverfy",method = RequestMethod.POST)
public PfHttpRespone verfy(String vername,HttpSession session){
Object captcha_key = session.getAttribute("CAPTCHA_KEY");
用session保存的验证码和输入的验证码对比
Boolean b=captcha_key.equals(vername);
try {
if ((b)){
return ResultUtil.success("验证成功");
}else {
return ResultUtil.error("验证失败");
}
} catch (Exception e) {
_logger.error(e.getMessage());
return ResultUtil.error("验证异常");
}
}
}