private String _generateRegKey(HttpServletRequest request, HttpServletResponse response) { String REG_VALUE = null; String code = RandomStringUtils.randomAlphanumeric(LENGTH).toUpperCase(); code.replace('0', 'W'); code.replace('o', 'R'); code.replace('I', 'E'); code.replace('1', 'T'); REG_VALUE = CryptUtils.encrypt(code, key); RequestUtils.setCookie(request, response, COOKIE_NAME, REG_VALUE, 1440, true); // 保存1天 return code; }
/** * 检查验证码是否正确 * * @param req * @return */ public boolean validate(HttpServletRequest req) { Cookie cke = RequestUtils.getCookie(req, COOKIE_NAME); if (cke == null || StringUtils.isNotBlank(cke.getValue())) { return false; } String value = cke.getValue(); String code1 = null; try { code1 = CryptUtils.decrypt(value, key); } catch (Exception e) { return false; } String code2 = req.getParameter("verifyCode"); return StringUtils.equalsIgnoreCase(code1, code2); }