?Hutool參考文檔
1.1 pom文件中導(dǎo)入hutool-captcha依賴
?? 新建springboot項(xiàng)目,并在其pom.xml中導(dǎo)入hutool-captcha依賴:
<dependency>
? ? ? ? <groupId>cn.hutool</groupId>
? ? ? ? <artifactId>hutool-all</artifactId>
? ? ? ? <version>5.8.1</version>
</dependency>
1.2 創(chuàng)建前端頁面與跳轉(zhuǎn)頁面
前端頁面index.html
<h2>Hutool-captcha驗(yàn)證碼驗(yàn)證</h2>
? ? <form action="/loginc" method="post">
? ? ? ? <input type="text" name="verifyCode" placeholder="請輸入驗(yàn)證碼" required="true">
? ? ? ? <img alt="單擊圖片刷新!" class="pointer" src="/common/verify"
? ? ? ? ? ? ?onclick="this.src='/common/verify?d='+new Date()*1">
? ? ? ? </br>
? ? ? ? <button type="submit" value="submit">登陸</button>
? ? </form>
跳轉(zhuǎn)頁面success.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
? ? <meta charset="UTF-8">
? ? <title>Title</title>
</head>
<body>
<h2>success</h2>
</body>
</html>
1.3 實(shí)現(xiàn)后端代碼
1.3.1 創(chuàng)建后端控制類生成驗(yàn)證碼
?? 創(chuàng)建控制類CommonController類,一方面通過流的方式將隨機(jī)生成的驗(yàn)證碼圖片信息發(fā)送到前端瀏覽器;另一方面將驗(yàn)證碼中的驗(yàn)證信息寫入session中,以方便后續(xù)的驗(yàn)證
@RestController
public class HutoolController {
? ? @GetMapping("/common/verify")
? ? public void Verify(HttpServletRequest request,HttpServletResponse response) throws IOException {
? ? ? ? //定義圖形驗(yàn)證碼的長、寬、驗(yàn)證碼字符數(shù)、干擾線寬度
? ? ? ? ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(150, 40, 5, 4);
? ? ? ? //圖形驗(yàn)證碼寫出,可以寫出到文件,也可以寫出到流
? ? ? ? captcha.write(response.getOutputStream());
? ? ? ? //獲取驗(yàn)證碼中的文字內(nèi)容
? ? ? ? String verifyCode = captcha.getCode();
? ? ? ? request.getSession().setAttribute("verifyCode",verifyCode);
? ? }
1.3.3 實(shí)現(xiàn)驗(yàn)證碼的驗(yàn)證與頁面跳轉(zhuǎn)
?? 對前端輸入的數(shù)據(jù)并發(fā)送到服務(wù)器的驗(yàn)證信息進(jìn)行校驗(yàn),當(dāng)輸入信息與驗(yàn)證碼信息一致則跳轉(zhuǎn)至success.html頁面,否則跳轉(zhuǎn)至false.html頁面文章來源:http://www.zghlxwxcb.cn/news/detail-440943.html
@Controller
public class AdminController {
? ? @PostMapping("/loginc")
? ? public String loginByHutool(@RequestParam("verifyCode") String verifyCode,
? ? ? ? ? ? ? ? ? ? ? ? HttpSession session){
? ? ? ? String captchaCode = session.getAttribute("verifyCode") + "";
? ? ? ? if(verifyCode.equals(captchaCode)){
? ? ? ? ? ? return "success";
? ? ? ? }
? ? ? ? return "false";
? ? }
}
Spring使用驗(yàn)證碼:Kaptcha - 簡書文章來源地址http://www.zghlxwxcb.cn/news/detail-440943.html
到了這里,關(guān)于常用工具類之使用hutool生成驗(yàn)證碼的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!