springMVC實現(xiàn)圖形驗證碼(kaptcha)代碼實例
springMVC項目中實現(xiàn)圖形驗證碼功能,可以使用kaptcha來實現(xiàn),下面是步驟
一、引入架包,pom.xml
<dependency>
<groupId>com.google.code</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.2</version>
</dependency>
二、kaptchaProducer配置,需要在spring-mvc.xml中對kaptchaProducer的bean進行配置,可以對驗證碼圖形的屬性進行配置,網(wǎng)上也有很多可以參考的,我這是放我項目中的相關代碼
<!-- 使用Kaptcha生成驗證碼 -->
<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">60</prop>
<prop key="kaptcha.textproducer.font.size">40</prop>
<prop key="kaptcha.session.key">code</prop>
<prop key="kaptcha.textproducer.char.length">4</prop>
<prop key="kaptcha.textproducer.font.names">宋體,楷體,微軟雅黑</prop>
</props>
</constructor-arg>
</bean>
</property>
</bean>
三、kaptchaProducer配置完畢后,就只可以寫生成驗證碼的方法了。
@Autowired
private Producer captchaProducer = null;
/**
* 生成驗證碼
* @param request
* @param response
* @throws IOException
*/
@RequestMapping("/yzmImg")
public void yzmImg(HttpServletRequest request,HttpServletResponse response) throws IOException{
log.info("-----生成驗證碼-----");
HttpSession session = request.getSession();
String preCode = (String)session.getAttribute(Constants.KAPTCHA_SESSION_KEY);
log.info("-----生成驗證碼-----前一個驗證碼:"+preCode);
System.out.println("-----生成驗證碼-----前一個驗證碼:"+preCode);
//生成驗證碼
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");
// return a jpeg
response.setContentType("image/jpeg");
// create the text for the image
String capText = captchaProducer.createText();
System.err.println(capText);
// store the text in the session
session.setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
session.setAttribute(Constants.KAPTCHA_SESSION_DATE, new Date());
// 存放到緩存服務器上,并把key記錄到cookie
//CodeUtils.creatCode(capText, response, request);
// create the image with the text
BufferedImage bi = captchaProducer.createImage(capText);
ServletOutputStream out = response.getOutputStream();
// write the data out
ImageIO.write(bi, "jpg", out);
try {
out.flush();
} finally {
out.close();
}
}
四、生成驗證碼的方法寫完后,前端可以使用img標簽作為圖形驗證碼的容器
jsp代碼
<img src="yzmImg.htm" onclick="getYZMImg()" id="yzmimg">
javascript代碼
function getYZMImg(){
$("#yzmimg").attr("src","yzmImg.htm");
}
img作為圖形的容器,src寫生成驗證碼的映射,如果需要換一個驗證碼,點擊圖片,onclick事件重新調(diào)用生成驗證碼的方法,即可達到更換驗證碼。
下面說下我遇到的幾個問題
一、異常,異常信息
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.google.code.kaptcha.Producer' available: expected at least 1 bean which qualifies as autowire candidate.
Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
我說下具體的情況,代碼我寫完了之后,就啟動服務,可是報了上面的異常,可是我仔細檢查了,代碼都沒有問題,但就是拋異常,最后在類的前面加了兩個注解
@Controller
@Scope("prototype")
@SuppressWarnings("all")
public class IndexController {
}
其中Scope和SuppressWarnings兩個注解加上之后,啟動服務就不會拋這個異常了。
二、異常,上面的異常解決之后,又出現(xiàn)了下面這個異常
java.lang.NoClassDefFoundError: com/jhlabs/image/RippleFilter] with root cause java.lang.ClassNotFoundException: com.jhlabs.image.RippleFilter
提示RippleFilter類找不到,所以加入Filter架包,我是直接加入的架包,架包名為:filters-2.0.235-1.jar,加入這個架包就不會拋異常了。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Java中char[] 和 String 類型占用字節(jié)大小問題
這篇文章主要介紹了Java中char[] 和 String 類型占用字節(jié)大小問題,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-08-08
SpringCloud整合Netty集群實現(xiàn)WebSocket的示例代碼
文章主要介紹了SpringCloud整合Netty集群實現(xiàn)WebSocket的相關內(nèi)容,包括服務注冊和發(fā)現(xiàn)中心的配置,如使用Nacos、CommandLineRunner啟動Netty服務等,還介紹了通過Redis實現(xiàn)消息發(fā)布訂閱的機制,需要的朋友可以參考下2024-11-11
SpringBoot @Value注解支持配置自動刷新能力擴展方式
本文介紹了如何通過自定義注解和BeanPostProcessor實現(xiàn)SpringBoot中@Value注解的配置自動刷新能力,主要步驟包括:定義一個支持動態(tài)刷新的注解,實現(xiàn)配置的動態(tài)變更,以及通過BeanPostProcessor掃描并刷新使用@Value注解的變量2024-12-12

