天天看点

kaptcha html页面验证码,kaptcha验证码使用(示例代码)

效果图:

kaptcha html页面验证码,kaptcha验证码使用(示例代码)

kaptcha 是一个很有用的验证码生成工具。有了它,你能够生成各种样式的验证码,由于它是可配置的。

kaptcha工作的原理是调用 com.google.code.kaptcha.servlet.KaptchaServlet,生成一个图片。

同一时候将生成的验证码字符串放到 HttpSession中。

使用kaptcha能够方便的配置:

验证码的字体

验证码字体的大小

验证码字体的字体颜色

验证码内容的范围(数字,字母,中文汉字!)

验证码图片的大小。边框,边框粗细,边框颜色

验证码的干扰线(能够自己继承com.google.code.kaptcha.NoiseProducer写一个自己定义的干扰线)

验证码的样式(鱼眼样式、3D、普通模糊……当然也能够继承com.google.code.kaptcha.GimpyEngine自己定义样式)

……

以下介绍一下使用方法:

实现方式有多种,以下介绍Spring注入方式和Servlet方式

Servlet方式

2.配置web.xml添加servlet

Kaptcha

com.google.code.kaptcha.servlet.KaptchaServlet

Kaptcha

/kaptcha.jpg

3、在jsp页面中

kaptcha html页面验证码,kaptcha验证码使用(示例代码)

name="kaptcha" value="" />

value="submit" />

当中src="kaptcha.jpg"会被定位到servlet上

4、KaptchaServlet会把验证码设置到session中。能够例如以下方式获取

String kaptchaExpected = (String)request.getSession().getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);5、假设是struts2的action。能够例如以下方式获取

String kaptchaExpected = (String)ActionContext.getContext().getSession().get(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);6、假设想设置点击图片更换验证码。能够加上例如以下js。须要jquery

$(function(){

$(‘#kaptchaImage‘).click(function () { $(this).attr(‘src‘, ‘/kaptcha.jpg?‘ + Math.floor(Math.random()*100) ); })

});

7、或者来点fade效果

$(function() {

$(‘#kaptchaImage‘).click(

function() {

$(this).hide().attr(‘src‘,

‘kaptcha.jpg?

‘ + Math.floor(Math.random() * 100)).fadeIn();

});

});

8、验证码图片还有非常多參数设置

设置方法。在web.xml的servlet中

kaptcha.border

no

Constant

描写叙述

默认值

kaptcha.border

图片边框,合法值:yes , no

yes

kaptcha.border.color

边框颜色,合法值: r,g,b (and optional alpha) 或者 white,black,blue.

black

kaptcha.border.thickness

边框厚度。合法值:>0

1

kaptcha.image.width

图片宽

200

kaptcha.image.height

图片高

50

kaptcha.producer.impl

图片实现类

com.google.code.kaptcha.impl.DefaultKaptcha

kaptcha.textproducer.impl

文本实现类

com.google.code.kaptcha.text.impl.DefaultTextCreator

kaptcha.textproducer.char.string

文本集合,验证码值从此集合中获取

abcde2345678gfynmnpwx

kaptcha.textproducer.char.length

验证码长度

5

kaptcha.textproducer.font.names

字体

Arial, Courier

kaptcha.textproducer.font.size

字体大小

40px.

kaptcha.textproducer.font.color

字体颜色,合法值: r,g,b  或者 white,black,blue.

black

kaptcha.textproducer.char.space

文字间隔

2

kaptcha.noise.impl

干扰实现类

com.google.code.kaptcha.impl.DefaultNoise

kaptcha.noise.color

干扰颜色。合法值: r,g,b 或者 white,black,blue.

black

kaptcha.obscurificator.impl

图片样式:

水纹com.google.code.kaptcha.impl.WaterRipple

鱼眼com.google.code.kaptcha.impl.FishEyeGimpy

阴影com.google.code.kaptcha.impl.ShadowGimpy

com.google.code.kaptcha.impl.WaterRipple

kaptcha.background.impl

背景实现类

com.google.code.kaptcha.impl.DefaultBackground

kaptcha.background.clear.from

背景颜色渐变,開始颜色

light grey

kaptcha.background.clear.to

背景颜色渐变。结束颜色

white

kaptcha.word.impl

文字渲染器

com.google.code.kaptcha.text.impl.DefaultWordRenderer

kaptcha.session.key

session key

KAPTCHA_SESSION_KEY

kaptcha.session.date

session date

KAPTCHA_SESSION_DATE

9、

水纹效果

kaptcha html页面验证码,kaptcha验证码使用(示例代码)

鱼眼效果

kaptcha html页面验证码,kaptcha验证码使用(示例代码)

阴影效果

kaptcha html页面验证码,kaptcha验证码使用(示例代码)

下面代码是我在SpringMVC中的代码

package com.rapido.controller;

import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;

import javax.servlet.ServletOutputStream;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.servlet.ModelAndView;

import com.google.code.kaptcha.Constants;

import com.google.code.kaptcha.Producer;

@Controller

@RequestMapping("/Captcha")

public class CaptchaRandomCodeController {

private Producer captchaProducer = null;

@Autowired

public void setCaptchaProducer(Producer captchaProducer) {

this.captchaProducer = captchaProducer;

}

@RequestMapping("/captcha-image")

public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {

response.setDateHeader("Expires", 0);// 禁止server端缓存

// 设置标准的 HTTP/1.1 no-cache headers.

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

// 设置IE扩展 HTTP/1.1 no-cache headers (use addHeader).

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

response.setHeader("Pragma", "no-cache");// 设置标准 HTTP/1.0 不缓存图片

response.setContentType("image/jpeg");// 返回一个 jpeg 图片,默认是text/html(输出文档的MIMI类型)

String capText = captchaProducer.createText();// 为图片创建文本

// 将文本保存在session中。这里就使用包中的静态变量吧

request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);

BufferedImage bi = captchaProducer.createImage(capText); // 创建带有文本的图片

ServletOutputStream out = response.getOutputStream();

// 图片数据输出

ImageIO.write(bi, "jpg", out);

try {

out.flush();

} finally {

out.close();

}

System.out.println("Session 验证码是:" + request.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY));

return null;

}

}

applicationContext.xml

yes

105,179,90

160

50

code

red

35

3

5

彩云,宋体,楷体,微软雅黑

JSP

$(function(){

// 验证码渐变效果切换

$(‘#kaptchaImage‘).click(

function() {

$(this).hide().attr(‘src‘,‘Captcha/captcha-image.do?‘+ Math.floor(Math.random() * 100)).fadeIn();

}

);

});

验证码:

kaptcha html页面验证码,kaptcha验证码使用(示例代码)

验证码不但能够使用图形还能够使用语音,比方SimpleCaptcha就能够。kaptcha扩展了SimpleCaptcha,等等都能够使用语音验证码,官方中有提供此类的demo能够下载部署在tomcat上使用

使用Java生成验证码的库有非常多,比方下面列表中的举例