kaptcha图形验证码组件

kaptcha 是一个非常实用的验证码生成工具。有了它,你可以生成各种样式的验证码,因为它是可配置的。kaptcha工作的原理是调用 com.google.code.kaptcha.servlet.KaptchaServlet,生成一个图片。同时将生成的验证码字符串放到 HttpSession中。

1.下载kaptcha-2.3.2.jar并添加到项目

2.spring 配置文件 applicationContext.xml

    <!-- 图形验证码配置 -->
    <bean id="captchaProducer" class="com.google.code.kaptcha.impl.DefaultKaptcha">  
        <property name="config">  
            <bean class="com.google.code.kaptcha.util.Config">  
                <!--通过构造函数注入属性值 -->  
                <constructor-arg type="java.util.Properties">  
                    <props>  
                      <!-- 验证码宽度 -->  
                      <prop key="kaptcha.image.width">90</prop>   
                      <!-- 验证码高度 -->  
                      <prop key="kaptcha.image.height">36</prop>  
                      <!-- 生成验证码内容范围 -->  
                      <prop key="kaptcha.textproducer.char.string">0123456789qwertyuioplkjhgfdsazxcvbnm</prop>  
                      <!-- 验证码个数 -->  
                      <prop key="kaptcha.textproducer.char.length">4</prop>  
                      <!-- 是否有边框 -->  
                      <prop key="kaptcha.border">no</prop>  
                      <!-- 边框颜色 -->  
                      <prop key="kaptcha.border.color">black</prop>  
                      <!-- 边框厚度 -->  
                      <prop key="kaptcha.border.thickness">1</prop>  
                      <!-- 验证码字体颜色 -->  
                      <prop key="kaptcha.textproducer.font.color">black</prop>  
                      <!-- 验证码字体大小 -->  
                      <prop key="kaptcha.textproducer.font.size">33</prop>  
                      <!-- 验证码所属字体样式 -->  
                      <prop key="kaptcha.textproducer.font.names">宋体,楷体,微软雅黑</prop>  
                      <!-- 干扰线颜色 -->
                      <prop key="kaptcha.noise.color">blue</prop>  
                      <prop key="kaptcha.noise.impl">com.google.code.kaptcha.impl.NoNoise</prop>
                      <!-- 验证码文本字符间距 -->  
                      <prop key="kaptcha.textproducer.char.space">3</prop>                                            
                    </props>
                </constructor-arg>  
            </bean>  
        </property>  
     </bean>

3. Controller的实现

map保存服务端为生成的验证码,可传入service中与前端传入的验证码进行对比从而完成验证。

  ......
    @Autowired
    private Producer producer;
    
    private Map<String, String> map;
  ......

  @RequestMapping("/kaptcha") public void initCaptcha(HttpServletRequest request, HttpServletResponse response) throws Exception { HttpSession session = request.getSession(); response.setDateHeader("Expires", 0); response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate"); response.addHeader("Cache-Control", "post-check=0, pre-check=0"); response.setHeader("Pragma", "no-cache"); response.setContentType("image/jpeg"); String capText = producer.createText(); session.setAttribute(Constants.KAPTCHA_SESSION_KEY, capText); BufferedImage bi = producer.createImage(capText); ServletOutputStream out = response.getOutputStream(); ImageIO.write(bi, "jpg", out); try { out.flush(); } finally { String kaptchaCode = (String)session.getAttribute(Constants.KAPTCHA_SESSION_KEY); map = new HashMap<String, String>(); map.put("kaptchaCode", kaptchaCode); out.close(); } }

4.页面上添加

img的src的属性以自己的实际路径进行修改

                    <b style="padding:0 2px 0 120px;">
                        <input type="text" id="code" placeholder="请输入验证码" style="vertical-align:middle;"/>
                        <img class="kaptcha" id="kaptcha" src="/frame/kaptcha" style="vertical-align:middle;"/>
                    </b>

5.在页面对应js添加

//更换图形验证码
$(function (){  
    $('#kaptcha').click(  
       function (){  
           $(this).attr('src', '/frame/kaptcha?' + Math.floor(Math.random() * 100));   
        }  
    );            
});
原文地址:https://www.cnblogs.com/007sx/p/5676247.html