您的位置:首页 > 编程语言 > Java开发

spring通过kaptcha配置验证码生成的代码实例

2018-05-21 19:39 417 查看

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

2.web.xml中配置kaptcha属性

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


//验证码 @RequestMapping("/kaptcha") public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { 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 = kaptchaProducer.createText(); request.getSession().setAttribute("validateCode", capText); BufferedImage bi = kaptchaProducer.createImage(capText); ServletOutputStream out = response.getOutputStream(); ImageIO.write(bi, "jpg", out); try { out.flush(); } finally { out.close(); } }

  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


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: