前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Kaptcha生成验证码

Kaptcha生成验证码

作者头像
geekfly
发布2022-05-06 18:46:36
8030
发布2022-05-06 18:46:36
举报
文章被收录于专栏:geekfly

1.导入jar包 如kaptchar.2.3.jar 2.添加配置文件

这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

3.使用方法`

代码语言:javascript
复制

 function changeR(node){ // 用于点击时产生不同的验证码 node.src = "/randomcode.jpg?time="+new Date().getTime() ; }  
获取验证码并校验
代码语言:javascript
复制
<body>
    <%
        // 检查是否是正确的验证码
        String k = (String) session
                .getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
        String str = request.getParameter("r");
        if (k.equals(str))
            out.print("true");
        out.print(k + "---" + str);
    %>
</body>

4.说明 使用加法验证码需要注意(重写)

代码语言:javascript
复制
import com.google.code.kaptcha.Producer;
import com.google.code.kaptcha.util.Config;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;
import javax.imageio.ImageIO;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class KaptchaServlet extends HttpServlet implements Servlet {
    private Properties props;
    private Producer kaptchaProducer;
    private String sessionKeyValue;

    public KaptchaServlet() {
        this.props = new Properties();

        this.kaptchaProducer = null;

        this.sessionKeyValue = null;
    }

    public void init(ServletConfig conf) throws ServletException {
        super.init(conf);

        ImageIO.setUseCache(false);

        Enumeration initParams = conf.getInitParameterNames();
        while (initParams.hasMoreElements()) {
            String key = (String) initParams.nextElement();
            String value = conf.getInitParameter(key);
            this.props.put(key, value);
        }

        Config config = new Config(this.props);
        this.kaptchaProducer = config.getProducerImpl();
        this.sessionKeyValue = config.getSessionKey();
    }

    public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        resp.setDateHeader("Expires", 0L);

        resp.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");

        resp.addHeader("Cache-Control", "post-check=0, pre-check=0");

        resp.setHeader("Pragma", "no-cache");

        resp.setContentType("image/jpeg");

        String capText = this.kaptchaProducer.createText();
        //直接从验证码集合中选取四位,前两位作为加数1,后两位作为加数2 因此配置需要填写**4**位验证码
        String s1 = capText.substring(0, 2);
        String s2 = capText.substring(2, 4); 
        int r = Integer.valueOf(s1).intValue() + Integer.valueOf(s2).intValue();

        req.getSession().setAttribute(this.sessionKeyValue, String.valueOf(r));

        BufferedImage bi = this.kaptchaProducer.createImage(s1+"+"+s2+"=?");

        ServletOutputStream out = resp.getOutputStream();

        ImageIO.write(bi, "jpg", out);
        try {
            out.flush();
        } finally {
            out.close();
        }
    }
}

使用文字验证码直接修改文本集合即可,也可以重写类 如下

代码语言:javascript
复制
` public String getText() { 
 int length = getConfig().getTextProducerCharLength(); 
 //char[] charS = getConfig().getTextProducerCharString();
代码语言:javascript
复制
    String[] s = new String[]{"慕","课","网","教","程","验","证","码","实","例"};

    Random rand = new Random();
    StringBuffer sb = new StringBuffer();
    for(int i = 0; i < length; i++){
        int ind = rand.nextInt(s.length);
        sb.append(s[ind]);
    }
    return sb.toString();
}`
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2015-10-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
验证码
腾讯云新一代行为验证码(Captcha),基于十道安全栅栏, 为网页、App、小程序开发者打造立体、全面的人机验证。最大程度保护注册登录、活动秒杀、点赞发帖、数据保护等各大场景下业务安全的同时,提供更精细化的用户体验。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档