Java實(shí)現(xiàn)EasyCaptcha圖形驗(yàn)證碼的具體使用
EasyCaptcha
github地址: https://github.com/whvcse/EasyCaptcha
簡(jiǎn)介
Java圖形驗(yàn)證碼,支持gif、中文、算術(shù)等類(lèi)型,可用于Java Web、JavaSE等項(xiàng)目。
效果展示(部分驗(yàn)證碼閃動(dòng),截圖無(wú)法展示)

導(dǎo)入方式
1、 maven
<dependencies> <dependency> <groupId>com.github.whvcse</groupId> <artifactId>easy-captcha</artifactId> <version>1.6.2</version> </dependency> </dependencies>
2、 gradle
dependencies {
compile 'com.github.whvcse:easy-captcha:1.6.2'
}
jar包
下載地址 EasyCaptcha.jar
在SpringMVC中使用
@Controller
public class CaptchaController {
@RequestMapping("/captcha")
public void captcha(HttpServletRequest request, HttpServletResponse response) throws Exception {
CaptchaUtil.out(request, response);
}
}
前端HTML
<img src="/captcha" width="130px" height="48px" />
如果使用了安全框架之類(lèi)的攔截功能,要把/captcha路徑排除登錄攔截。
有些同學(xué)可能需要在Servlet中使用如下
web.xml中配置servlet:
<web-app> <!-- 圖形驗(yàn)證碼servlet --> <servlet> <servlet-name>CaptchaServlet</servlet-name> <servlet-class>com.wf.captcha.servlet.CaptchaServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CaptchaServlet</servlet-name> <url-pattern>/captcha</url-pattern> </servlet-mapping> </web-app>
前端html代碼:
<img src="/captcha" width="130px" height="48px" />
比較驗(yàn)證碼
@Controller
public class LoginController {
@PostMapping("/login")
public JsonResult login(String username,String password,String verCode){
if (!CaptchaUtil.ver(verCode, request)) {
CaptchaUtil.clear(request); // 清除session中的驗(yàn)證碼
return JsonResult.error("驗(yàn)證碼不正確");
}
}
設(shè)置寬高和位數(shù)
@Controller
public class CaptchaController {
@RequestMapping("/captcha")
public void captcha(HttpServletRequest request, HttpServletResponse response) throws Exception {
// 設(shè)置位數(shù)
CaptchaUtil.out(5, request, response);
// 設(shè)置寬、高、位數(shù)
CaptchaUtil.out(130, 48, 5, request, response);
// 使用gif驗(yàn)證碼
GifCaptcha gifCaptcha = new GifCaptcha(130,48,4);
CaptchaUtil.out(gifCaptcha, request, response);
}
}
還有更多參數(shù),可到作者github查看
到此這篇關(guān)于Java實(shí)現(xiàn)EasyCaptcha圖形驗(yàn)證碼的具體使用的文章就介紹到這了,更多相關(guān)Java EasyCaptcha圖形驗(yàn)證碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Mabitis中的#與$符號(hào)區(qū)別及用法介紹
這篇文章主要介紹了Mabitis中的#與$符號(hào)區(qū)別,需要的朋友可以參考下2017-02-02
SpringBoot超詳細(xì)講解Thymeleaf模板引擎
這篇文章主要分享了Spring Boot整合使用Thymeleaf,Thymeleaf是新一代的Java模板引擎,類(lèi)似于Velocity、FreeMarker等傳統(tǒng)引擎,關(guān)于其更多相關(guān)內(nèi)容,需要的小伙伴可以參考一下2022-07-07
SpringBoot?分模塊開(kāi)發(fā)的操作方法
這篇文章主要介紹了SpringBoot?分模塊開(kāi)發(fā)的操作方法,通過(guò)在原項(xiàng)目新增一個(gè)maven模塊,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04
Springboot訪問(wèn)html頁(yè)面的教程詳解
這篇文章主要介紹了Springboot訪問(wèn)html頁(yè)面的教程,本文圖文并茂給大家介紹的非常詳細(xì),需要的朋友可以參考下2018-03-03
mybatis?返回Map類(lèi)型key默認(rèn)為大寫(xiě)問(wèn)題
這篇文章主要介紹了mybatis?返回Map類(lèi)型key默認(rèn)為大寫(xiě)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
一篇文章帶你入門(mén)java網(wǎng)絡(luò)編程
網(wǎng)絡(luò)編程是指編寫(xiě)運(yùn)行在多個(gè)設(shè)備(計(jì)算機(jī))的程序,這些設(shè)備都通過(guò)網(wǎng)絡(luò)連接起來(lái)。本文介紹了一些網(wǎng)絡(luò)編程基礎(chǔ)的概念,并用Java來(lái)實(shí)現(xiàn)TCP和UDP的Socket的編程,來(lái)讓讀者更好的了解其原理2021-08-08
基于SpringBoot實(shí)現(xiàn)圖片上傳與顯示
這篇文章主要為大家詳細(xì)介紹了基于SpringBoot實(shí)現(xiàn)圖片上傳與顯示,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08

