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

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);

不使用session,借助redis实现验证码的更多相关文章

  1. 把旧系统迁移到.Net Core 2.0 日记 (15) --Session 改用Redis

    安装Microsoft.Extensions.Caching.Redis.Core NuGet中搜索Microsoft.Extensions.Caching.Redis.Core并安装,此NuGet包 ...

  2. springboot security+redis+jwt+验证码 登录验证

    概述 基于jwt的token认证方案 验证码 框架的搭建,可以自己根据网上搭建,或者看我博客springboot相关的博客,这边就不做介绍了.验证码生成可以利用Java第三方组件,引入 <dep ...

  3. 【Tomcat】Tomcat Session在Redis共享

    参考的优秀文章 Redis-backed non-sticky session store for Apache Tomcat 简单地配置Tomcat Session在Redis共享 我使用的是现有的 ...

  4. session 加入redis的实现代码方式

    session,中文经常翻译为会话,其本来的含义是 指有始有终的一系列动作/消息,比如打电话时从拿起电话拨号到挂断电话这中间的一系列过程可以称之为一个session.有时候我们可以看到这样的话&quo ...

  5. 让php Session 存入 redis 配置方法

    首先要做的就是安装redis 安装方法:http://redis.io/download Installation Download, extract and compile Redis with: ...

  6. 借助Redis做秒杀和限流的思考

    最近群里聊起秒杀和限流,我自己没有做过类似应用,但是工作中遇到过更大的数据和并发. 于是提出了一个简单的模型: var count = rds.inc(key); if(count > 1000 ...

  7. 在SpringBoot中存放session到Redis

    前言 今天你们将再一次领略到SpringBoot的开发到底有多快,以及SpringBoot的思想(默认配置) 我们将使用redis存放用户的session,用户session存放策略有很多,有存放到内 ...

  8. Spring Session - 使用Redis存储HttpSession例子

    目的 使用Redis存储管理HttpSession: 添加pom.xml 该工程基于Spring Boot,同时我们将使用Spring IO Platform来维护依赖版本号: 引入的依赖有sprin ...

  9. session存入redis

    Session信息入Redis Session简介 session,中文经常翻译为会话,其本来的含义是 指有始有终的一系列动作/消息,比如打电话时从拿起电话拨号到挂断电话这中间的一系列过程可以称之为一 ...

随机推荐

  1. Asp.net 配置web.Config 在出错时跳转到相应页面

    <!--<customErrors mode="On" defaultRedirect="error.aspx">      <erro ...

  2. 浏览器兼容性-JS篇

    总结一下平时遇到的浏览器兼容性问题,本篇关于JS. 1.事件绑定 兼容写法: function add(obj,event){ if (obj.addEventListener) { obj.addE ...

  3. [LintCode] Climbing Stairs 爬梯子问题

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  4. iOS 琐碎点------切某个或某几个角的圆角

    不说废话----------> 1.如果是切四个角的圆角,代码示例: self.picImage.layer.cornerRadius = 8; self.picImage.layer.mask ...

  5. *HDU 1028 母函数

    Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ...

  6. java.net.ConnectException: Connection timed out

    原因可能如下: (1)生成的WSDL的IP地址是不是服务器的IP: (2)代码中是否需要代理,若否,取消代理,若是,设置代理:

  7. 一些值得练习的github项目

    简单粗暴,一晚上用 node.Vue 写个联机五子棋 https://github.com/ccforward/cc/issues/51 Vue2.0实现简易豆瓣电影webApp https://gi ...

  8. Mysql的用户名密码设置方法

    方法如下: 1, 关闭mysql服务 /etc/init.d/mysqld stop 2,使用 –skip-grant-tables选项启动mysql服务,可以修 改/etc/inin.d/mysql ...

  9. GPRS 接入外网的过程

    请问GPRS模块与Internet上主机的连接.数据传输过程 虽然按照GPRS模块的说明文档能够通过内嵌TCP/IP实现数据的传输,但是对GPRS模块和主机之间的连接关系了解得不是很多.有谁可以介绍一 ...

  10. 关于shiro

    1.1  简介 Apache Shiro是Java的一个安全框架.目前,使用Apache Shiro的人越来越多,因为它相当简单,对比Spring Security,可能没有Spring Securi ...