自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

五分鐘搞定驗(yàn)證碼,你學(xué)會(huì)了嗎?

開(kāi)發(fā) 前端
我們其實(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ò)的大佬還望不吝雅教!

責(zé)任編輯:武曉燕 來(lái)源: Java技術(shù)指北
相關(guān)推薦

2022-03-08 08:39:22

gRPC協(xié)議云原生

2023-12-30 13:41:39

JSON格式數(shù)據(jù)

2023-04-12 08:21:30

ChatGPTQQDiscord

2023-09-11 13:08:26

2024-07-29 12:21:12

2015-12-03 14:10:26

systemd容器Linux

2021-12-01 06:50:50

Docker底層原理

2022-12-09 09:21:10

分庫(kù)分表算法

2017-12-19 09:05:39

2023-04-04 08:14:45

2024-07-10 18:55:09

Python定時(shí)

2022-10-11 08:48:08

HTTP狀態(tài)碼瀏覽器

2025-03-18 09:20:00

Go語(yǔ)言Golang

2024-01-02 12:05:26

Java并發(fā)編程

2023-08-01 12:51:18

WebGPT機(jī)器學(xué)習(xí)模型

2023-01-10 08:43:15

定義DDD架構(gòu)

2024-02-04 00:00:00

Effect數(shù)據(jù)組件

2023-07-26 13:11:21

ChatGPT平臺(tái)工具

2024-01-19 08:25:38

死鎖Java通信

2022-04-26 08:10:33

MySQL存儲(chǔ)InnoDB
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)