springMVC實現圖形驗證碼(kaptcha)


springMVC項目中實現圖形驗證碼功能,可以使用kaptcha來實現,下面是步驟

一、引入架包,pom.xml

<dependency>
        <groupId>com.google.code</groupId>
        <artifactId>kaptcha</artifactId>
        <version>2.3.2</version>
    </dependency>

二、kaptchaProducer配置,需要在spring-mvc.xml中對kaptchaProducer的bean進行配置,可以對驗證碼圖形的屬性進行配置,網上也有很多可以參考的,我這是放我項目中的相關代碼

<!-- 使用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事件重新調用生成驗證碼的方法,即可達到更換驗證碼。

下面說下我遇到的幾個問題

一、異常,異常信息

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兩個注解加上之后,啟動服務就不會拋這個異常了。

二、異常,上面的異常解決之后,又出現了下面這個異常

 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,加入這個架包就不會拋異常了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM