不使用session,借助redis实现验证码
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实现验证码的更多相关文章
- 把旧系统迁移到.Net Core 2.0 日记 (15) --Session 改用Redis
安装Microsoft.Extensions.Caching.Redis.Core NuGet中搜索Microsoft.Extensions.Caching.Redis.Core并安装,此NuGet包 ...
- springboot security+redis+jwt+验证码 登录验证
概述 基于jwt的token认证方案 验证码 框架的搭建,可以自己根据网上搭建,或者看我博客springboot相关的博客,这边就不做介绍了.验证码生成可以利用Java第三方组件,引入 <dep ...
- 【Tomcat】Tomcat Session在Redis共享
参考的优秀文章 Redis-backed non-sticky session store for Apache Tomcat 简单地配置Tomcat Session在Redis共享 我使用的是现有的 ...
- session 加入redis的实现代码方式
session,中文经常翻译为会话,其本来的含义是 指有始有终的一系列动作/消息,比如打电话时从拿起电话拨号到挂断电话这中间的一系列过程可以称之为一个session.有时候我们可以看到这样的话&quo ...
- 让php Session 存入 redis 配置方法
首先要做的就是安装redis 安装方法:http://redis.io/download Installation Download, extract and compile Redis with: ...
- 借助Redis做秒杀和限流的思考
最近群里聊起秒杀和限流,我自己没有做过类似应用,但是工作中遇到过更大的数据和并发. 于是提出了一个简单的模型: var count = rds.inc(key); if(count > 1000 ...
- 在SpringBoot中存放session到Redis
前言 今天你们将再一次领略到SpringBoot的开发到底有多快,以及SpringBoot的思想(默认配置) 我们将使用redis存放用户的session,用户session存放策略有很多,有存放到内 ...
- Spring Session - 使用Redis存储HttpSession例子
目的 使用Redis存储管理HttpSession: 添加pom.xml 该工程基于Spring Boot,同时我们将使用Spring IO Platform来维护依赖版本号: 引入的依赖有sprin ...
- session存入redis
Session信息入Redis Session简介 session,中文经常翻译为会话,其本来的含义是 指有始有终的一系列动作/消息,比如打电话时从拿起电话拨号到挂断电话这中间的一系列过程可以称之为一 ...
随机推荐
- CodeSimth-.NetFrameworkDataProvider可能没有安装。解决方法
原文地址:http://www.haogongju.net/art/2561889 1.下载System.Data.SQLite驱动:注意:根据自己的CPU选择是32位还是64位的驱动.建议选择4.0 ...
- centos 7 安装和配置vncserver
前期准备: 关闭防火墙,centos的防火墙是firewalld,关闭防火墙的命令 systemctl stop firewalld.service 关闭enforce setenforce 0 ce ...
- Meta标签介绍
Meta标签写法与作用 meta标签是在HTML网页源代码中一个重要的html标签.meta位于head区的辅助性标签,提供用户不可用的信息. META标签用来描述一个HTML网页文档的属性,例 ...
- IPv6进阶
IPV6报文部分字段介绍 1.没有校验和字段:优点:当TTL减少时,不需要重新处理,相对于IPV4能减少处理的时间:缺点:必须在上层包含校验和2.下一个报文:可指向扩展报文:(大部分节点不处理和查看大 ...
- 算法_bitmap算法
概述 所谓bitmap就是用一个bit位来标记某个元素对应的value,而key即是这个元素.由于采用bit为单位来存储数据,因此在可以大大的节省存储空间 算法思想 32位机器上,一个整形,比 ...
- 前端UI框架和JS类库
一.前端框架库: 1.Zepto.js 地址:http://www.css88.com/doc/zeptojs/ 描述:Zepto是一个轻量级的针对现代高级浏览器的JavaScript库, 它与jqu ...
- 文档ID:某某 模板文件不存在,无法解析文档!
如果是生成栏目列表时出现这样的问题]: 1.可以修改include/arc.listview.class.php这个文件. 2.复制代码 echo "模板文件不存在,无法解析文档 ...
- jsfl脚本设置导出AS链接名遇到的奇怪问题
今天写jsfl脚本发现一个奇怪的问题,脚本用于对库对象设置AS链接名,代码如下: var item = fl.getDocumentDOM().library.items[0];var exportN ...
- #1094 : Lost in the City
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi gets lost in the city. He does not know where he is ...
- JQuery的父、子、兄弟节点查找方法
jQuery.parent(expr) //找父元素 jQuery.parents(expr) //找到所有祖先元素,不限于父元素 jQuery.children ...