资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

springmvc使用kaptcha配置生成验证码实例

使用Kaptcha 生成验证码十分简单并且参数可以进行自定义,以下简单记录下使用步骤。

石阡ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为成都创新互联的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:13518219792(备注:SSL证书合作)期待与您的合作!

1.在pom.xml中添加maven依赖:


  com.google.code.kaptcha
  kaptcha
  2.3
  jdk15

2.web.xml中配置kaptcha属性:


    
      
        
          
            yes
            105,179,90
            1

            blue

            150
            50

            verifyCode

            
            4
            4


            30
            blue

          
        
      
    
  

其中bean节点的id值 verifyCodeProducer 为在类中引用@Resource生成实例时的名称;属性配置中 kaptcha.session.key 的值为在session中存取名称。

在servlet节点中配置

3.controller类中的相关方法:

@Controller
public class CommonController {

  @Autowired
  private Producer verifyCodeProducer;

  @RequestMapping(path = "/getVerifyCodeImage", method = RequestMethod.GET)
  public void getVerifyCodeImage(HttpServletRequest request, HttpServletResponse response) {
    HttpSession session = request.getSession();

    ResponseUtils.noCache(response);
    response.setContentType("image/jpeg");

    String capText = verifyCodeProducer.createText();
    session.setAttribute(Constants.SESSION_KEY_VERIFY_CODE, capText);

    BufferedImage bi = verifyCodeProducer.createImage(capText);
    ServletOutputStream out = null;
    try {
      out = response.getOutputStream();
      ImageIO.write(bi, "jpg", out);
      out.flush();
    } catch (Exception ex) {
      LOGGER.error("Failed to produce the verify code image: ", ex);
      throw new ServerInternalException("Cannot produce the verify code image.");
    } finally {
      IOUtils.closeQuietly(out);
    }
  }
}

Constants.SESSION_KEY_VERIFY_CODE为属性配置中 kaptcha.session.key 的值。

4.jsp:

function changeVerifyCode() {
  $('#verifyCodeImage').hide().attr('src', '${pageContext.request.contextPath}/getVerifyCodeImage?' + Math.floor(Math.random()*100) ).fadeIn(); 
  event.cancelBubble=true; 
}

5.kaptcha属性说明:

  1. kaptcha.border.color   边框颜色   默认为Color.BLACK 
  2. kaptcha.border.thickness  边框粗细度  默认为1 
  3. kaptcha.producer.impl   验证码生成器  默认为DefaultKaptcha 
  4. kaptcha.textproducer.impl   验证码文本生成器  默认为DefaultTextCreator 
  5. kaptcha.textproducer.char.string   验证码文本字符内容范围  默认为abcde2345678gfynmnpwx 
  6. kaptcha.textproducer.char.length   验证码文本字符长度  默认为5 
  7. kaptcha.textproducer.font.names    验证码文本字体样式  默认为new Font("Arial", 1, fontSize), new Font("Courier", 1, fontSize) 
  8. kaptcha.textproducer.font.size   验证码文本字符大小  默认为40 
  9. kaptcha.textproducer.font.color  验证码文本字符颜色  默认为Color.BLACK 
  10. kaptcha.textproducer.char.space  验证码文本字符间距  默认为2 
  11. kaptcha.noise.impl    验证码噪点生成对象  默认为DefaultNoise 
  12. kaptcha.noise.color   验证码噪点颜色   默认为Color.BLACK 
  13. kaptcha.obscurificator.impl   验证码样式引擎  默认为WaterRipple 
  14. kaptcha.word.impl   验证码文本字符渲染   默认为DefaultWordRenderer 
  15. kaptcha.background.impl   验证码背景生成器   默认为DefaultBackground 
  16. kaptcha.background.clear.from   验证码背景颜色渐进   默认为Color.LIGHT_GRAY 
  17. kaptcha.background.clear.to   验证码背景颜色渐进   默认为Color.WHITE 
  18. kaptcha.image.width   验证码图片宽度  默认为200 
  19. kaptcha.image.height  验证码图片高度  默认为50  

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。


网页题目:springmvc使用kaptcha配置生成验证码实例
标题来源:http://cdkjz.cn/article/pggesc.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220