Spring整合Kaptcha谷歌驗證碼工具的開發(fā)步驟
開發(fā)步驟:
1、加入依賴
<dependency> <groupId>com.google.code.kaptcha</groupId> <artifactId>kaptcha</artifactId> <version>2.3</version> </dependency>
國內(nèi)鏡像無法下載該依賴,需要手動通過jar包在本地倉庫安裝一個依賴。
安裝命令:
mvn install:install-file -Dfile=jar文件的位置 -DgroupId=目標(biāo)安裝的groupid -DartifactId=目標(biāo)安裝的artifactId -Dpackaging=jar -Dversion=版本號
2、創(chuàng)建xml配置文件
spring-context-kaptcha.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="captchaProducer" class="com.google.code.kaptcha.impl.DefaultKaptcha">
<property name="config">
<bean class="com.google.code.kaptcha.util.Config">
<constructor-arg>
<props>
<!-- 是否顯示邊框 -->
<prop key="kaptcha.border">yes</prop>
<!-- 邊框顏色 -->
<prop key="kaptcha.border.color">105,179,90</prop>
<!-- 驗證碼字體顏色 -->
<prop key="kaptcha.textproducer.font.color">blue</prop>
<!-- 驗證碼的寬 -->
<prop key="kaptcha.image.width">125</prop>
<!-- 驗證碼的高 -->
<prop key="kaptcha.image.height">45</prop>
<!-- 驗證碼的字體大小 -->
<prop key="kaptcha.textproducer.font.size">45</prop>
<!-- 驗證碼的session key -->
<prop key="kaptcha.session.key">code</prop>
<!-- 驗證碼個數(shù) -->
<prop key="kaptcha.textproducer.char.length">4</prop>
<!-- 驗證碼的字體 -->
<prop key="kaptcha.textproducer.font.names">宋體,楷體,微軟雅黑</prop>
</props>
</constructor-arg>
</bean>
</property>
</bean>
</beans>
注意:別忘記加載這個配置文件
3、Controller層代碼
import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
@Controller
public class KaptchaController {
@Autowired
private Producer captchaProducer;
@RequestMapping(value = "verification", method = RequestMethod.GET)
public ModelAndView verification(HttpServletRequest request, HttpServletResponse response) throws IOException {
System.out.println("進(jìn)入生成驗證碼控制層");
response.setDateHeader("Expires", 0);
// Set standard HTTP/1.1 no-cache headers.
response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
// Set IE extended HTTP/1.1 no-cache headers (use addHeader).
response.addHeader("Cache-Control", "post-check=0, pre-check=0");
// Set standard HTTP/1.0 no-cache header.
response.setHeader("Pragma", "no-cache");
// 返回一個圖片
response.setContentType("image/jpeg");
// 創(chuàng)建text文本在圖片中
String capText = captchaProducer.createText();
// 將文本內(nèi)容存儲在session中
request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
// 用文本創(chuàng)建圖像
BufferedImage bi = captchaProducer.createImage(capText);
//servlet輸出流
ServletOutputStream out = response.getOutputStream();
// 寫出數(shù)據(jù)
ImageIO.write(bi, "jpg", out);
try {
// 提交
out.flush();
} finally {
// 提交后將輸出流關(guān)閉
out.close();
}
return null;
}
}
4、JSP代碼
將驗證碼顯示在該圖片中。
<img id="verification" src="/verification" style="cursor: pointer;" title="看不清?換一張" />
5、給圖片綁定點(diǎn)擊刷新驗證碼事件
//換驗證碼事件
$("#verification").click(function () {
$(this).attr("src","verification");
})
6、驗證輸入驗證碼
//比較驗證碼
//Constants.KAPTCHA_SESSION_KEY 是之前生成驗證碼存在session中的
//code是用戶點(diǎn)擊提交后,通過name屬性發(fā)送到控制層的內(nèi)容,為用戶輸入的內(nèi)容
if(!(code.equals(session.getAttribute(Constants.KAPTCHA_SESSION_KEY)))){
//如果用戶輸入的內(nèi)容與session中的內(nèi)容不一致返回登錄頁面
return "login";
}
到此這篇關(guān)于Spring整合Kaptcha谷歌驗證碼工具的文章就介紹到這了,更多相關(guān)Spring整合Kaptcha谷歌驗證碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Mybatis 一對多和多對一關(guān)聯(lián)查詢問題
這篇文章主要介紹了Mybatis 一對多和多對一關(guān)聯(lián)查詢問題,需要的朋友可以參考下2017-04-04
基于Springboot+Mybatis對數(shù)據(jù)訪問層進(jìn)行單元測試的方式分享
本文將介紹一種快高效、可復(fù)用的解決測試方案——對數(shù)據(jù)訪問層做單元測試,文章通過代碼示例介紹的非常詳細(xì),具有一定的參考價值,需要的朋友可以參考下2023-07-07
Java8新特性之lambda(動力節(jié)點(diǎn)Java學(xué)院整理)
這篇文章主要介紹了Java8新特性之lambda(動力節(jié)點(diǎn)Java學(xué)院整理)表達(dá)式的相關(guān)知識,包括lambda語法方面的知識,非常不錯,具有參考借鑒價值,需要的朋友參考下吧2017-06-06
在ssm中使用ModelAndView跳轉(zhuǎn)頁面失效的解決
這篇文章主要介紹了在ssm中使用ModelAndView跳轉(zhuǎn)頁面失效的解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-05-05

