在做项目时,我们常用到验证码,也许你知道怎么用的了。但是依然有很多的朋友不
知道,所以在此我做了一个例子和大家共享一下。希望对您有所帮助。
因为我也搞了两天,我不想随便的家伙都来下载。所以我会设置10个积分才可以下载。
如果你真的想学习而没有积分,直接找我要也行的。呵呵,好运
下载地址:han-验证码.rar
下载地址:http://down.51cto.com/data/337848
===============================================================
验证码,我们使用时肯定会涉及跨域的情况。跨域的情况我们应该如何做呢?
思路可以这样:
生成验证码时:
放入cookie的是,比如:(“KZ”,“UUID”)
放入memcache的值是:(“UUID”,“验证码”)
当我们验证时:先取cookie的值,UUID。然后根据UUID,去memcache中取验证码的值。如果对的话,OK了。
eg.SpringMvc中使用验证码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
package
com.viptv.marketing.controller;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
org.slf4j.Logger;
import
org.slf4j.LoggerFactory;
import
org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.stereotype.Controller;
import
org.springframework.web.bind.annotation.RequestMapping;
import
com.sohu.viptv.marketing.yzm.RandomValidateCode;
/**
* 提供给前台验证码图片
* @author liweihan ()
* @version 1.0 (2014年12月3日 下午4:39:53)
* 说明:如果生成其他类型的验证码,可以参考:http://hanchaohan.blog.51cto.com/2996417/791811
*/
@Controller
@RequestMapping
(
"/yzm"
)
public
class
YZM {
private
static
final
Logger logger = LoggerFactory.getLogger(YZM.
class
);
@Autowired
RandomValidateCode rc;
/**
* 简单的数字字母验证码
*
* @param h 生成图片的高
* @param w 生成图片的宽
* @param request
* @param response
*
* 2014年12月3日 下午4:51:56
* liweihan
*/
@RequestMapping
(
"/image1.do"
)
public
void
getImage1(
HttpServletRequest request,
HttpServletResponse response) {
response.setContentType(
"image/jpeg"
);
//设置相应的类型,告诉浏览器输出的内容为图片
response.setHeader(
"Pragma"
,
"No-cache"
);
//设置响应头信息,告诉浏览器不要缓存此内容
response.setHeader(
"Cache-Control"
,
"no-cache"
);
response.setDateHeader(
"Expires"
,
0
);
//RandomValidateCode rc = new RandomValidateCode();
try
{
rc.getRandcode(request,response);
//输出图片的方法
//rc.getRandcode(request, response, h, w);
}
catch
(Exception e) {
e.printStackTrace();
logger.info(
" ===== 生成图片验证码时出现异常"
,e);
}
}
}
|
验证码生成的类:就是把以前的session缓存memcache和cookie的形式就行了。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
/**
*产生随机数字
* @param request
* @param response
*/
public
void
getRandcode(HttpServletRequest request,
HttpServletResponse response)
throws
Exception{
// HttpSession session = request.getSession();
BufferedImage image =
new
BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
//1.BufferedImage类具有缓冲区的Image类,Image类是用于描述图像信息的类
Graphics g = image.getGraphics();
//2.产生Image对象的Graphics对象,该对象可以在图像上进行各种绘制操作
g.fillRect(
0
,
0
, width, height);
//设定边框
g.setColor(getRandColor(
110
,
133
));
//绘制干扰线
for
(
int
i =
0
; i < lineSize; i++) {
drowLine(g);
}
//绘制随机字符
String randomString =
""
;
for
(
int
i =
0
; i < stringNum; i++) {
randomString = drowString(g,randomString,i);
}
//将随机产生的字符存在Session中 用于验证
// session.removeAttribute(RANDOMCODEKEY);
// session.setAttribute(RANDOMCODEKEY,randomString);
setCookieAndMemcache(response,randomString);
//System.out.println("randomString: "+randomString);//验证信息1
//System.out.println("randomString1: "+session.getAttribute(RANDOMCODEKEY));//验证信息1
//System.out.println("randomString2: "+session.getAttribute("keycode"));//验证信息1
g.dispose();
ImageIO.write(image,
"JPEG"
,response.getOutputStream());
//3..将内存中的图片通过数据流的形式输出到客户端
}
/**
* 存放code值到缓存
* @param response
* @param code
* @throws Exception
*
* 2014年12月3日 下午7:21:51
* liweihan
*/
private
void
setCookieAndMemcache(HttpServletResponse response,String code)
throws
Exception {
//生成memcache的key值
UUID uuid = UUID.randomUUID();
String memcacheKey =
"HUAZHU_"
+uuid;
//放入cookie
CookieUtil.addCookie(response, ConstantUtil.SOHU_MARKET_HUAZHU, memcacheKey,
null
,
null
,
60
*
60
*
24
);
//存验证码到memcache中
memcachedClient.set(memcacheKey,
60
*
5
, code);
//5分钟过期
}
|
比如你要访问:http://xxx.xxx.com/activity/yzm/image1
当然在其他类中你要做验证时,就该这样了:
先从cookie中取值,再从memcache中取值了。
本文转自韩立伟 51CTO博客,原文链接:http://blog.51cto.com/hanchaohan/791811,如需转载请自行联系原作者