原文:http://www.cnblogs.com/chizizhixin/p/5311619.html

在项目中经常会使用验证码,kaptcha 就一个非常不错的开源框架,分享下自己在项目中的使用:

1、首先下载kaptcha 把kaptcha-2.3.2.jar包放在lib下

2、登陆页面初始化 document.getElementById("mycode").src="${pageContext.request.contextPath}/kaptcha/code?"+Math.random();

3、@RequestMapping("/kaptcha")
public class CaptchaController {

@Autowired private Producer captchProducer;
    
    
    /**
     * 生产验证码方法
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping("/code")
    public ModelAndView getKaptchaImage(HttpServletRequest request,HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        //String code = session.getAttribute(Constants.KAPTCHA_SESSION_KEY).toString();//得到验证码
        //System.out.println("******************************验证码是:"+code+"***************************");
        //设置浏览器的请求头
        response.setDateHeader("Expires", 0);
        response.addHeader("Cache-Control", "no-store,no-cache,must-revalidate");
        response.setHeader("Pragma", "no-cache");
        response.setContentType("image/jpeg");
        
        String capText = captchProducer.createText();//创建验证码内容
        session.setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);//将验证码存入到session中
        
        BufferedImage bi = captchProducer.createImage(capText);
        ServletOutputStream out = response.getOutputStream();
        ImageIO.write(bi, "jpg", out);//输出验证码图片
        out.flush();
        out.close();
        return null;
    }

最新文章

  1. STM32 IIC
  2. nginx配置多个虚拟主机vhost
  3. sql 语句 事务
  4. Javascript中document.execCommand()的用法
  5. A strange lift_BFS
  6. cocos2dx创建sprite的多种方法
  7. Codeforces Round #312 (Div. 2) B.Amr and The Large Array
  8. Opencv学习笔记(六)SURF学习笔记
  9. Recover a file when you use git reset head by mistake.
  10. kettle连接Hbase中数据导入(8)
  11. hdu--2084--dp--数塔
  12. 初学Python(第二课)
  13. 1-1 maven 学习笔记(1-6章)
  14. swoole框架和其所涉及的协议
  15. zookeeper分布式服务中选主的应用
  16. app -webkit-box-orient: vertical 打包后不显示
  17. jacoco + eclipse单元测试覆盖率
  18. 重置AD用户密码
  19. 002.MMM高可用MySQL简介
  20. poj2406 Power Strings 【KMP】

热门文章

  1. android 远程Service以及AIDL的跨进程通信
  2. <转自原博客> NOIP2008 传纸条
  3. hdu 1512
  4. JavaScript—获取本地时间以12小时制显示
  5. 【CF edu 27 G. Shortest Path Problem?】
  6. AtCoder Grand Contest 018 A
  7. [ZJOI2008]树的统计——树链剖分
  8. [poj_3469]多核CPU
  9. centos 安装使用smb
  10. shell脚本查看服务器基本信息