這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)使用kaptcha怎么實(shí)現(xiàn)一個(gè)驗(yàn)證碼功能,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
成都創(chuàng)新互聯(lián)公司網(wǎng)站建設(shè)公司是一家服務(wù)多年做網(wǎng)站建設(shè)策劃設(shè)計(jì)制作的公司,為廣大用戶提供了成都做網(wǎng)站、網(wǎng)站建設(shè),成都網(wǎng)站設(shè)計(jì),1元廣告,成都做網(wǎng)站選成都創(chuàng)新互聯(lián)公司,貼合企業(yè)需求,高性價(jià)比,滿足客戶不同層次的需求一站式服務(wù)歡迎致電。
1.首先在pom.xml文件中導(dǎo)入Maven依賴
com.github.penggle kaptcha 2.3.2
2.在web.xml文件中配置一個(gè)servlet
Kaptcha com.google.code.kaptcha.servlet.KaptchaServlet kaptcha.border no kaptcha.textproducer.font.color red kaptcha.image.width 135 kaptcha.textproducer.char.string ACDEFHKPRSTWX345679 kaptcha.image.height 50 kaptcha.textproducer.font.size 43 kaptcha.noise.color black kaptcha.textproducer.char.length 4 kaptcha.textproducer.font.names Arial Kaptcha /Kaptcha
3.頁(yè)面代碼如下
向后端提交數(shù)據(jù)
var verifyCodeActual = $('#j_captcha').val(); if (!verifyCodeActual) { $.toast('請(qǐng)輸入驗(yàn)證碼!'); return; } formData.append("verifyCodeActual", verifyCodeActual);
4.js交互
function changeVerifyCode(img) { img.src = "../Kaptcha?" + Math.floor(Math.random() * 100); }
5.后端驗(yàn)證
import javax.servlet.http.HttpServletRequest; public class CodeUtil { public static boolean checkVerifyCode(HttpServletRequest request) { String verifyCodeExpected = (String) request.getSession().getAttribute( com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY); String verifyCodeActual = HttpServletRequestUtil.getString(request, "verifyCodeActual"); if (verifyCodeActual == null || !verifyCodeActual.equalsIgnoreCase(verifyCodeExpected)) { return false; } return true; } }
import javax.servlet.http.HttpServletRequest; public class HttpServletRequestUtil { public static int getInt(HttpServletRequest request, String name) { try { return Integer.decode(request.getParameter(name)); } catch (Exception e) { return -1; } } public static long getLong(HttpServletRequest request, String name) { try { return Long.valueOf(request.getParameter(name)); } catch (Exception e) { return -1; } } public static Double getDouble(HttpServletRequest request, String name) { try { return Double.valueOf(request.getParameter(name)); } catch (Exception e) { return -1d; } } public static Boolean getBoolean(HttpServletRequest request, String name) { try { return Boolean.valueOf(request.getParameter(name)); } catch (Exception e) { return false; } } public static String getString(HttpServletRequest request, String name) { try { String result = request.getParameter(name); if (result != null) { result = result.trim(); } if ("".equals(result)) result = null; return result; } catch (Exception e) { return null; } } }
上述就是小編為大家分享的使用kaptcha怎么實(shí)現(xiàn)一個(gè)驗(yàn)證碼功能了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。