我們其實(shí)很經(jīng)??吹?,登錄一些網(wǎng)站其實(shí)是需要驗(yàn)證碼的。使用驗(yàn)證碼是現(xiàn)在很多網(wǎng)站通行的一種方式,因?yàn)橛?jì)算機(jī)很難識(shí)別驗(yàn)證碼,所以可以識(shí)別驗(yàn)證碼的用戶(hù)就可以被認(rèn)為是人類(lèi)。
哈嘍,大家好,我是了不起。
我們其實(shí)很經(jīng)常看到,登錄一些網(wǎng)站其實(shí)是需要驗(yàn)證碼的。使用驗(yàn)證碼是現(xiàn)在很多網(wǎng)站通行的一種方式,因?yàn)橛?jì)算機(jī)很難識(shí)別驗(yàn)證碼,所以可以識(shí)別驗(yàn)證碼的用戶(hù)就可以被認(rèn)為是人類(lèi)。
今天我們講一下在 Java 中驗(yàn)證碼的使用。
驗(yàn)證碼生成
本效果是利用easy-captcha工具包實(shí)現(xiàn),首先需要添加相關(guān)依賴(lài)到pom.xml中,代碼如下:
<dependency>
<groupId>com.github.whvcse</groupId>
<artifactId>easy-captcha</artifactId>
<version>1.6.2</version>
</dependency>
驗(yàn)證碼格式
easy-captcha驗(yàn)證碼工具支持GIF、中文、算術(shù)等類(lèi)型,分別通過(guò)下面幾個(gè)實(shí)例對(duì)象實(shí)現(xiàn):
- SpecCaptcha(PNG類(lèi)型的靜態(tài)圖片驗(yàn)證碼)
- GifCaptcha(Gif類(lèi)型的圖片驗(yàn)證碼)
- ChineseCaptcha(GIF類(lèi)型中文圖片驗(yàn)證碼)
- ArithmeticCaptcha(算術(shù)類(lèi)型的圖片驗(yàn)證碼)
字符類(lèi)型分為以下幾種:
- TYPE_DEFAULT:數(shù)字和字母混合
- TYPEONLYNUMBER:純數(shù)字
- TYPEONLYCHAR:純字母
- TYPEONLYUPPER:純大寫(xiě)字母
- TYPEONLYLOWER:純小寫(xiě)字母
- TYPENUMAND_UPPER:數(shù)字和大寫(xiě)字母混合
后端邏輯的實(shí)現(xiàn)
package com.yanx.controller;
import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.base.Captcha;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.util.StringUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Controller
public class KapchaController {
@GetMapping("/kaptcha")
public void defaultKaptcha(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException {
httpServletResponse.setHeader("Cache-Control","no-store");
httpServletResponse.setHeader("Pragma","no-cache");
httpServletResponse.setDateHeader("Expires",0);
httpServletResponse.setContentType("image/gif");
//三個(gè)參數(shù)分別為寬、高、位數(shù)
SpecCaptcha captcha=new SpecCaptcha(75,30,4);
//設(shè)置類(lèi)型為數(shù)字和字母混合
captcha.setCharType(Captcha.TYPE_DEFAULT);
//設(shè)置字體
captcha.setCharType(Captcha.FONT_9);
//驗(yàn)證碼存入session
httpServletRequest.getSession().setAttribute("verifyCode",captcha.text().toLowerCase());
//輸出圖片流
captcha.out(httpServletResponse.getOutputStream());
}
}
這里控制器新增了defaultKaptcha()方法,該方法所攔截處理的路徑為/kaptcha
前端邏輯的實(shí)現(xiàn)
在static目錄中新建kaptcha.html頁(yè)面,代碼如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>驗(yàn)證碼</title>
</head>
<body>
<img src="/kaptcha" onclick="this.src='/kaptcha?t=new Date()'">
</body>
</html>
訪(fǎng)問(wèn)后端驗(yàn)證碼路徑/kaptcha,驗(yàn)證碼為圖片形式。onclick方法為點(diǎn)擊該標(biāo)簽時(shí)可以動(dòng)態(tài)切換顯示驗(yàn)證碼。
啟動(dòng)Spring Boot項(xiàng)目,打開(kāi)瀏覽器輸入地址:
??http://localhost:8080/kaptcha.html??
效果如下:

驗(yàn)證碼驗(yàn)證
后端代碼
package com.yanx.controller;
import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.base.Captcha;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.util.StringUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@Controller
public class KapchaController {
@GetMapping("/kaptcha")
public void defaultKaptcha(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException {
httpServletResponse.setHeader("Cache-Control","no-store");
httpServletResponse.setHeader("Pragma","no-cache");
httpServletResponse.setDateHeader("Expires",0);
httpServletResponse.setContentType("image/gif");
//三個(gè)參數(shù)分別為寬、高、位數(shù)
SpecCaptcha captcha=new SpecCaptcha(75,30,4);
//設(shè)置類(lèi)型為數(shù)字和字母混合
captcha.setCharType(Captcha.TYPE_DEFAULT);
//設(shè)置字體
captcha.setCharType(Captcha.FONT_9);
//驗(yàn)證碼存入session
httpServletRequest.getSession().setAttribute("verifyCode",captcha.text().toLowerCase());
//輸出圖片流
captcha.out(httpServletResponse.getOutputStream());
}
@GetMapping("/verify")
@ResponseBody
public String verify(@RequestParam("code") String code, HttpSession session){
if(StringUtils.isEmpty(code)){
return "驗(yàn)證碼不能為空";
}
String kapchaCode = session.getAttribute("verifyCode")+"";
if(StringUtils.isEmpty(kapchaCode)||!code.toLowerCase().equals(kapchaCode)){
return "驗(yàn)證碼輸入錯(cuò)誤";
}
return "驗(yàn)證成功";
}
}
前端代碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>驗(yàn)證碼驗(yàn)證</title>
</head>
<body>
<img src="/kaptcha" onclick="this.src='/kaptcha?d=new Date()'">
<br>
<input type="text" maxlength="5" id="code" placeholder="請(qǐng)輸入驗(yàn)證碼"/>
<button id="verify">驗(yàn)證</button>
<br/>
<p id="verifyResult"></p>
</body>
<script src="https://s3.pstatp.com/cdn/expire-1-M/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript" >
$(function(){
//驗(yàn)證按鈕點(diǎn)擊事件
$('#verify').click(function(){
var code=$('#code').val();
$.ajax({
type:'GET',//方法類(lèi)型
url:'/verify?code='+code,
success:function(result){
$('#verifyResult').html(result);
},
error:function(){
alert('請(qǐng)求失敗');
},
});
});
});
</script>
</html>
效果



結(jié)束語(yǔ)
生成驗(yàn)證碼功能還是比較常用的,所以記錄整理一下,方便以后回顧,如果有幫到你們的地方倍感榮幸,有路過(guò)的大佬還望不吝雅教!