Kaptcha生成验证码
来源:互联网 发布:怎样申请淘宝直播间 编辑:程序博客网 时间:2024/06/04 18:53
1.导入jar包 如kaptchar.2.3.jar
2.添加配置文件
3.使用方法`
获取验证码并校验
<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.说明
使用加法验证码需要注意(重写)
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(); } }}
使用文字验证码直接修改文本集合即可,也可以重写类
如下` public String getText() {
int length = getConfig().getTextProducerCharLength();
//char[] charS = getConfig().getTextProducerCharString();
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();}`
0 0
- kaptcha验证码生成
- Kaptcha生成验证码
- Kaptcha验证码生成
- kaptcha生成验证码
- 验证码生成库Kaptcha
- 利用kaptcha 生成验证码
- 使用kaptcha生成验证码
- 使用kaptcha生成验证码
- 使用kaptcha生成验证码
- google kaptcha验证码生成
- 使用Kaptcha生成验证码
- kaptcha组件生成验证码
- 使用kaptcha生成验证码
- 使用kaptcha生成验证码
- 使用kaptcha生成验证码
- java自动生成验证码插件-kaptcha
- java自动生成验证码插件-kaptcha
- spring mvc 使用kaptcha生成验证码
- 冲突域和广播域
- POJ 2492 A Bug's Life (高级并查集||带权并查集 ||二分图的判断+二维vector)
- python自学之文件
- Java归并算法递归实现
- No5 触摸事件
- Kaptcha生成验证码
- protocol buffer语法
- Perl学习笔记(三)
- php数据库编程:利用mysql扩展库操作数据库。
- 判断两个字符串是否为兄弟字符串
- hdu2181C - 哈密顿绕行世界问题
- Cocos2d-JS使用log方法控制台无法打印出结果
- DP-POJ-1458-Common Subsequence
- hdu acm 1548 A strange lift