springMVC如何實現(xiàn)圖形驗證碼

這篇文章主要介紹springMVC如何實現(xiàn)圖形驗證碼,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!

成都創(chuàng)新互聯(lián)公司網(wǎng)絡(luò)公司擁有十余年的成都網(wǎng)站開發(fā)建設(shè)經(jīng)驗,成百上千客戶的共同信賴。提供成都網(wǎng)站設(shè)計、成都網(wǎng)站制作、網(wǎng)站開發(fā)、網(wǎng)站定制、賣鏈接、建網(wǎng)站、網(wǎng)站搭建、成都響應(yīng)式網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計師打造企業(yè)風(fēng)格,提供周到的售前咨詢和貼心的售后服務(wù)

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)上也有很多可以參考的,我這是放我項目中的相關(guān)代碼

<!-- 使用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());
    // 存放到緩存服務(wù)器上,并把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標(biāo)簽作為圖形驗證碼的容器

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)}

我說下具體的情況,代碼我寫完了之后,就啟動服務(wù),可是報了上面的異常,可是我仔細(xì)檢查了,代碼都沒有問題,但就是拋異常,最后在類的前面加了兩個注解

@Controller
@Scope("prototype")
@SuppressWarnings("all")
public class IndexController {
}

其中Scope和SuppressWarnings兩個注解加上之后,啟動服務(wù)就不會拋這個異常了。

二、異常,上面的異常解決之后,又出現(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,加入這個架包就不會拋異常了。

以上是“springMVC如何實現(xiàn)圖形驗證碼”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

文章標(biāo)題:springMVC如何實現(xiàn)圖形驗證碼
URL分享:http://bm7419.com/article30/jdgdso.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管網(wǎng)站建設(shè)、網(wǎng)站制作云服務(wù)器、App設(shè)計、動態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁設(shè)計公司