kaptcha 實現登入驗證碼
介紹:近期公司的網站出現了惡意註冊,惡意刷簡訊的惡劣行為,唉,真惡劣,所以完善登入驗證和傳送簡訊驗證的國字號任務祕密開啟
1.相關引數
Constant | 描述 | 預設值 |
kaptcha.border | 圖片邊框,合法值:yes , no | yes |
kaptcha.border.color | 邊框顏色,合法值: r,g,b (and optional alpha) 或者 white,black,blue. | black |
kaptcha.border.thickness | 邊框厚度,合法值:>0 | 1 |
kaptcha.image.width | 圖片寬 | 200 |
kaptcha.image.height | 圖片高 | 50 |
kaptcha.producer.impl | 圖片實現類 | com.google.code.kaptcha.impl.DefaultKaptcha |
kaptcha.textproducer.impl | 文字實現類 | com.google.code.kaptcha.text.impl.DefaultTextCreator |
kaptcha.textproducer.char.string | 文字集合,驗證碼值從此集合中獲取 | abcde2345678gfynmnpwx |
kaptcha.textproducer.char.length | 驗證碼長度 | 5 |
kaptcha.textproducer.font.names | 字型 | Arial, Courier |
kaptcha.textproducer.font.size | 字型大小 | 40px. |
kaptcha.textproducer.font.color | 字型顏色,合法值: r,g,b 或者 white,black,blue. | black |
kaptcha.textproducer.char.space | 文字間隔 | 2 |
kaptcha.noise.impl | 干擾實現類 | com.google.code.kaptcha.impl.DefaultNoise |
kaptcha.noise.color | 干擾 顏色,合法值: r,g,b 或者 white,black,blue. | black |
kaptcha.obscurificator.impl | 圖片樣式: 水紋com.google.code.kaptcha.impl.WaterRipple 魚眼com.google.code.kaptcha.impl.FishEyeGimpy 陰影com.google.code.kaptcha.impl.ShadowGimpy | com.google.code.kaptcha.impl.WaterRipple |
kaptcha.background.impl | 背景實現類 | com.google.code.kaptcha.impl.DefaultBackground |
kaptcha.background.clear.from | 背景顏色漸變,開始顏色 | light grey |
kaptcha.background.clear.to | 背景顏色漸變, 結束顏色 | white |
kaptcha.word.impl | 文字渲染器 | com.google.code.kaptcha.text.impl.DefaultWordRenderer |
kaptcha.session.key | session key | KAPTCHA_SESSION_KEY |
kaptcha.session.date | session date | KAPTCHA_SESSION_DATE |
2.配置web.xml
<!--驗證碼--> <servlet> <servlet-name>kaptcha</servlet-name> <servlet-class>com.google.code.kaptcha.servlet.KaptchaServlet</servlet-class> <init-param> <param-name>kaptcha.border</param-name> <param-value>no</param-value> </init-param> <init-param> <param-name>kaptcha.image.width</param-name> <param-value>120</param-value> </init-param> <init-param> <param-name>kaptcha.image.height</param-name> <param-value>42</param-value> </init-param> <init-param> <param-name>kaptcha.textproducer.font.size</param-name> <param-value>32</param-value> </init-param> <init-param> <param-name>kaptcha.textproducer.char.length</param-name> <param-value>5</param-value> </init-param> <init-param> <param-name>kaptcha.obscurificator.impl</param-name> <param-value>com.google.code.kaptcha.impl.WaterRipple</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>kaptcha</servlet-name> <url-pattern>/kaptcha</url-pattern> </servlet-mapping>
3. 頁面顯示
<li class="proving-li clearfix"> <input type="text" placeholder="請輸入圖片驗證碼" onkeyup="checkImageCode()" class="form-control input-lg pull-left" id="imageRandomCode"> <div class="input-group pull-right"> <img src="/kaptcha" id="codeImg" onclick="getCode()" width="146" height="46"> </div> </li>
4.新增maven依賴
<dependency> <groupId>com.google.code.kaptcha</groupId> <artifactId>kaptcha</artifactId> <version>2.3.2</version> </dependency>
5.檢查圖片驗證碼的有效性
/** * 檢查圖片驗證碼 * @param request * @param captcha * @return */ @RequestMapping("/checkCaptcha") @ResponseBody public Map<String, Object> checkCaptcha(HttpServletRequest request, @RequestParam("captcha") String captcha) { try { //從session當中讀取驗證碼 String kaptchaExpected = (String) request.getSession() .getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY); if (captcha.equals(kaptchaExpected)) { return this.getResultMap(ResultCode.SUCCESS, "輸入正確", null); } else { return this.getResultMap(ResultCode.FAIL, "檢驗失敗", null); } } catch (Exception e) { logger.error("checkCaptcha---error", e); return this.getResultMap(ResultCode.ERROR, "系統錯誤", null); } }