不使用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,中文经常翻译为会话,其本来的含义是 指有始有终的一系列动作/消息,比如打电话时从拿起电话拨号到挂断电话这中间的一系列过程可以称之为一 ...
随机推荐
- liunux 修改hostname
最近鼓捣Oracle,记录些技巧 修改hostname # vim /ect/hosts # vim /etc/sysconfig/network 修改hostname # service netwo ...
- tornado学习笔记18 _RequestDispatcher 请求分发器
根据Application的配置,主要负责将客户端的请求分发到具体的RequestHandler.这个类实现了HTTPMessageDelegate接口. 18.1 构造函数 定义: def __in ...
- 【验证】C# dataSource 的记忆功能
做项目时遇到的问题:dataSource被ComboBox引用过一次,会记忆最后一次选中的值,然后下一次再用时这个值会直接呈现在ComboBox中. 为验证是dataSource还是ComboBox自 ...
- (原).NET程序加入多语言包解决方案工具,超级棒
Multi-Language Add-In Version 5.04.0088 for Visual Studio 2013 安装包:http://www.jollans.com/SetupMulti ...
- winhttp demo
#include <string>#include <iostream>#include <windows.h>#include <winhttp.h> ...
- Page
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head> <met ...
- Unity - 接入Android SDK
在网络上,关于Unity与Android如何进行交互,雨松MOMO大神已经有两篇文章简单介绍了如何操作(1)Unity3D研究院之打开Activity与调用JAVA代码传递参数(2)Unity3D研究 ...
- ie8 jquery parents() 获取多个的问题
今天开发的时候碰到了一个奇怪的问题 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3 ...
- Mono 3.2 测试NPinyin 中文转换拼音代码
C#中文转换为拼音NPinyin代码 在Mono 3.2下运行正常,Spacebuilder 有使用到NPinyin组件,代码兼容性没有问题. using System; using System. ...
- ABP理论学习之Abp Session
返回总目录 本篇目录 介绍 注入Session 使用Session属性 介绍 当应用程序要求用户登录时,那么应用程序也需要知道当前用户正在执行的操作.虽然ASP.NET本身在展现层提供了Session ...