Google 验证码

1.添加jar文件(kaptcha-2.3.2.jar)。下载地址 https://code.google.com/p/kaptcha/ 点击打开链接

2.前台jsp页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
	<script type="text/javascript" src="js/jquery-2.1.1.js"></script>
	<script type="text/javascript">
    $(document).ready(function(){  
		$("#kaptchaImage").click(function(){
			$(this).hide().attr('src','kaptcha.jpg?' + Math.floor(Math.random() * 100)).fadeIn();
		});
		

		$("#submit").click(function(){
			var inputcode=$("#inputcode").val();
        	$.ajax({ 
            	type:"get", 
            	url:"<%=basePath%>verificat",
				contentType:"application/json", 
				data:{kaptcha:inputcode},              
				success : function(data) {
					alert(data);
				}
			});
		});
	});
	</script>
  </head>
  
  <body>
    <form>
    	 <img id="kaptchaImage" src="kaptcha.jpg" alt="刷新" width="95" height="30"/><input id="inputcode" type="text" name="kaptcha" value="" />
		 <input type="button" value="提交" id="submit"/>
    </form>
  </body>
</html>

3.后台action(spring4 mvc)

@Controller
public class Verificat {

	@ResponseBody
	@RequestMapping(value="/verificat",method=RequestMethod.GET)
	public String verifi(HttpServletRequest request){
		String kaptchaExpected = (String) request.getSession().getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
		String kaptchaReceived = request.getParameter("kaptcha");

		if (kaptchaReceived == null || !kaptchaReceived.equalsIgnoreCase(kaptchaExpected)) {
			return "false";
		}else{
			return "true";
		}
	}
	
}

4.web.xml配置

	<!-- google verfication code -->			
	<servlet>
		<servlet-name>Kaptcha</servlet-name>
		<servlet-class>com.google.code.kaptcha.servlet.KaptchaServlet</servlet-class>
		<init-param>
			<param-name>kaptcha.border</param-name>
			<param-value>no</param-value>
		</init-param>
	</servlet>
	<servlet-mapping>
		<servlet-name>Kaptcha</servlet-name>
		<url-pattern>/kaptcha.jpg</url-pattern>
	</servlet-mapping>

下面是官网配置参数

Configuration is done through web.xml. All values can be left blank and a default value will be chosen.

Below is an example of the init-param you would add to your web.xml. If you want to set other variables, then you would add more init-param blocks.

<init-param>
    <param-name>kaptcha.border</param-name>
    <param-value>yes</param-value>
</init-param>

Note: This only works with the KaptchaServlet. SimpleServlet does not pay attention to the web.xml parameters.

Details

These values are stored in the com.google.code.kaptcha.Constants class.

ConstantDescriptionDefault
kaptcha.borderBorder around kaptcha. Legal values are yes or no.yes
kaptcha.border.colorColor of the border. Legal values are r,g,b (and optional alpha) or white,black,blue.black
kaptcha.border.thicknessThickness of the border around kaptcha. Legal values are > 0.1
kaptcha.image.widthWidth in pixels of the kaptcha image.200
kaptcha.image.heightHeight in pixels of the kaptcha image.50
kaptcha.producer.implThe image producer.com.google.code.kaptcha.impl.DefaultKaptcha
kaptcha.textproducer.implThe text producer.com.google.code.kaptcha.text.impl.DefaultTextCreator
kaptcha.textproducer.char.stringThe characters that will create the kaptcha.abcde2345678gfynmnpwx
kaptcha.textproducer.char.lengthThe number of characters to display.5
kaptcha.textproducer.font.namesA list of comma separated font names.Arial, Courier
kaptcha.textproducer.font.sizeThe size of the font to use.40px.
kaptcha.textproducer.font.colorThe color to use for the font. Legal values are r,g,b.black
kaptcha.textproducer.char.spaceThe space between the characters2
kaptcha.noise.implThe noise producer.com.google.code.kaptcha.impl.DefaultNoise
kaptcha.noise.colorThe noise color. Legal values are r,g,b.black
kaptcha.obscurificator.implThe obscurificator implementation.com.google.code.kaptcha.impl.WaterRipple
kaptcha.background.implThe background implementation.com.google.code.kaptcha.impl.DefaultBackground
kaptcha.background.clear.fromStarting background color. Legal values are r,g,b.light grey
kaptcha.background.clear.toEnding background color. Legal values are r,g,b.white
kaptcha.word.implThe word renderer implementation.com.google.code.kaptcha.text.impl.DefaultWordRenderer
kaptcha.session.keyThe value for the kaptcha is generated and is put into the HttpSession. This is the key value for that item in the session.KAPTCHA_SESSION_KEY
kaptcha.session.dateThe date the kaptcha is generated is put into the HttpSession. This is the key value for that item in the session.KAPTCHA_SESSION_DATE

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值