首页 技术 正文
技术 2022年11月15日
0 收藏 569 点赞 2,413 浏览 2472 个字

1.首先看一下基本的流程

不使用session,借助redis实现验证码

2.看一下代码

   注:其中用到的一些工具类,可以到我的github上去下载

     https://github.com/hjzgg/usually_util/tree/master/utils

    windows 下的 redis下载

    https://github.com/hjzgg/redis

  获取验证码的tooken

   @RequestMapping(value="loginCode")
@ResponseBody
public String getCode(){
PrintWriter out = null;
JSONObject jsono = new JSONObject();
try {
       //验证码工具类
ValidateCode vCode = new ValidateCode(55,25,4,80);
String randomCode = vCode.randomCode();
String encCode = DesUtil.strEnc(randomCode+System.currentTimeMillis(), "1", "2", "3");
//存储验证码字符串,过期时间为1分钟
redisTemplate.opsForValue().set(encCode, randomCode);
redisTemplate.expire(encCode, 1, TimeUnit.MINUTES);
//存储验证码生成器,过期时间为1分钟
redisTemplate.opsForValue().set(encCode+"ValidateCode", SerializeUtil.serialize(vCode));
redisTemplate.expire(encCode+"ValidateCode", 1, TimeUnit.MINUTES);
jsono.put("success", true);
jsono.put("message", encCode);
} catch (Exception e) {
e.printStackTrace();
jsono.put("success", true);
jsono.put("message", "inner error.");
} finally{
if(out != null) {
out.flush();
out.close();
}
}
return jsono.toString();
}

  本例中的tooken是通过加密生成的,加密串为 验证码+当前时间。或者采用UUID生成唯一tooken,都是可以得。生成ValidateCode(验证码工具类),然后将键值对(tooken,ValidateCode)放入redis中。

   获取验证码图片

  @RequestMapping(value="loginCodeImage")
public void getCodeImage(String codeAuth, HttpServletResponse response){
if(codeAuth == null) return;
String randomCode = (String) redisTemplate.opsForValue().get(codeAuth);
if(randomCode == null) return;
ValidateCode vCode = (ValidateCode)SerializeUtil.unserialize((byte[])redisTemplate.opsForValue().get(codeAuth+"ValidateCode"));
//产生图片
vCode.createCode(randomCode);
if(vCode == null) return;
// 设置响应的类型格式为图片格式
response.setContentType("image/jpeg");
//禁止图像缓存。
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
try {
vCode.write(response.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}

  根据tooken,在redis中找到对应的ValidateCode(验证码工具类),生成验证码图片。

3.前台获取验证码

  网页中获取

    <img src=”htpp://……”/>

java中获取

  public static ImageIcon getCodeImage(){
String data = JavaRequest.sendPost("loginCode", null);
JSONObject result = JSONObject.fromObject(data);
if((Boolean) result.get("success")){
JavaRequest.codeAuth = result.getString("message");
ImageIcon codeImg = null;
try{
codeImg = new ImageIcon(new URL(“.....”));
} catch (Exception e) {
e.printStackTrace();
return null;
}
return codeImg;
} else {
System.out.println("获取验证码图片: " + result);
return null;
}
}  ImageIcon codeImg = JavaRequest.getCodeImage();
if(codeImg == null){
codeImg = new ImageIcon("获取失败的图片.png");
}
  /////////////////
JLable codeImgLabel = new JLabel(codeImg);
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,086
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,561
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,410
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,183
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,820
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,903