软件编程
位置:首页>> 软件编程>> java编程>> springMVC实现图形验证码(kaptcha)代码实例

springMVC实现图形验证码(kaptcha)代码实例

作者:厌世阎罗  发布时间:2022-02-14 22:02:14 

标签:spring,mvc,图形,验证码,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,加入这个架包就不会抛异常了。

来源:https://www.cnblogs.com/lizs0314/p/10008273.html

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com