java实现随机字母数字验证码
生成随街验证码
VerifyCode 工具类
package com.meeno.common.cerifycode;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
/**
* @description: 随机验证码
* @author: Wzq
* @create: 2020-09-08 16:55
*/
public class VerifyCode {
private int w=70;
private int h=35;
private Random r= new Random();
private String[] fontNames={"宋体","华文楷体","黑体","微软雅黑","楷体_GB2312"};
private String codes="012345678901234567890123456789abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
private Color bgColor = new Color(255,255,255);
private String text;
private Color randomColor(){
int red = r.nextInt(150);
int green = r.nextInt(150);
int blue = r.nextInt(150);
return new Color(red,green,blue);
}
private Font randomFont(){
int index = this.r.nextInt(fontNames.length);
String fontName = fontNames[index];
int style = this.r.nextInt(4);
int size = this.r.nextInt(5) + 24;
return new Font(fontName, style, size);
}
private void drawLine (BufferedImage image){
int num = 3;
Graphics2D g2=(Graphics2D)image.getGraphics();
for(int i=0;i<num;i++){
int x1=r.nextInt(w);
int y1=r.nextInt(h);
int x2=r.nextInt(w);
int y2=r.nextInt(h);
g2.setStroke(new BasicStroke(1.5F));
g2.setColor(Color.BLUE);
g2.drawLine(x1,y1,x2,y2);
}
}
private char randomChar(){
int index=r.nextInt(this.codes.length());
return this.codes.charAt(index);
}
private BufferedImage createImage(){
BufferedImage image=new BufferedImage(this.w,this.h,BufferedImage.TYPE_INT_RGB);//BufferedImage.TYPE_INT_RGB
Graphics2D g2 = (Graphics2D)image.getGraphics();
g2.setColor(this.bgColor);
g2.fillRect(0,0,this.w,this.h);
return image;
}
public BufferedImage getImage(){
BufferedImage image=createImage();
Graphics2D g2=(Graphics2D)image.getGraphics();
StringBuilder sb = new StringBuilder();
for(int i =0;i<4;i++){
String s= randomChar()+"";
sb.append(s);
float x= i*1.0F*this.w/4.0F;
g2.setFont(randomFont());
g2.setColor(randomColor());
g2.drawString(s,x,this.h-5);
}
this.text=sb.toString();
drawLine(image);//添加干扰线
return image;
}
//返回验证码上的文本
public String getText(){
return this.text;
}
//保存图片到指定的输出流
public static void output(BufferedImage image, OutputStream out)
throws IOException {
ImageIO.write(image,"JPEG",out);
}
public static void main(String[] args) throws IOException {
VerifyCode vc=new VerifyCode();
BufferedImage bi = vc.getImage();
VerifyCode.output(bi,new FileOutputStream("E:\\work\\temp\\xxx.jpg"));
System.out.println("图片中的验证是:"+vc.getText());
}
}
使用
/**
* 生成随机验证码
*/
@RequestMapping("randomVerifyCode.do")
public ResponseBean randomVerifyCode() throws IOException {
VerifyCode vc = new VerifyCode();
BufferedImage bi = vc.getImage();
//VerifyCode.output(bi,response.getOutputStream());
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(bi, "png", out);
byte[] bytes = out.toByteArray();
String str = Base64.encode(bytes);
str = "data:image/png;base64," + str;
String uuid = IdUtil.simpleUUID();
Map<String,String> resultMap = Maps.newHashMap();
resultMap.put("base64", str);
resultMap.put("uuid", uuid);
//save redis
String key = "RandomVerify:" + uuid;
RedisUtil.set(key, vc.getText(), 180);
return ResultUtil.success(resultMap);
}
验证方法
/**
* 账号登录
* @param session
* @param phone
* @param pwd
* @param entryType
* @return
*/
@RequestMapping("accountLogin.do")
public ResponseBean accountLogin(final HttpSession session, String phone, String pwd, String entryType,
String uuid, String randomCerifyCode){
//校验验证码
MeenoAssert.hasLength(randomCerifyCode,"randomCerifyCode can not empty!");
Object randomCerifyCodeObj = RedisUtil.get("RandomVerify:" + uuid);
MeenoAssert.notNull(randomCerifyCodeObj, CErrEnum.RANDOM_VERIFY_CODE_FAILURE);
MeenoAssert.isTrue(randomCerifyCode.toLowerCase().equals(randomCerifyCodeObj.toString().toLowerCase()), CErrEnum.RANDOM_VERIFY_CODE_ERR);
LoginResult loginResult = this.employeeService.accountLogin(session, phone, pwd, entryType);
EmpView employeeView = this.employeeService.getEmployee(loginResult.getUserInfo().getId());
Map<String,Object> resultMap = Maps.newHashMap();
resultMap.put("loginResult", loginResult);
resultMap.put("employee", employeeView);
return ResultUtil.success(resultMap);
}
java实现随机字母数字验证码的更多相关文章
- 007-TreeMap、Map和Bean互转、BeanUtils.copyProperties(A,B)拷贝、URL编码解码、字符串补齐,随机字母数字串
一.转换 1.1.TreeMap 有序Map 无序有序转换 使用默认构造方法: public TreeMap(Map<? extends K, ? extends V> m) 1.2.Ma ...
- canvas验证码 - 随机字母数字
基于canvas制作随机生成数字英文组合验证码效果,点击或刷新会自动重组.输入验证码提交验证效果代码. <div class="verification"> <i ...
- PHP字母数字验证码和中文验证码
1:字母数字组合的验证码 HTML代码: 验证码:<input type="text" name="code"> <img onclick=& ...
- 字母数字、字母、汉字验证码 (java)
原文:http://blog.csdn.net/qh_java/article/details/49854477 一.字母数字,字母,汉字验证码的生成代码 1.字母数字验证码: package com ...
- php生成纯数字、字母数字、图片、纯汉字的随机数验证码
现在讲开始通过PHP生成各种验证码旅途,新手要开车了,请刷卡! 首先,我们开始先生成一个放验证码的背景图片 注:没有Imagejpg()这个函数,只有imagepng()函数 imagecreatet ...
- JS生成随机的由字母数字组合的字符串
前言 最近有个需求,是需要生成3-32位长度的字母数字组合的随机字符串,另一个是生成43位随机字符串. 方法一 奇妙的写法 1 Math.random().toString(36).substr( ...
- js随机生成字母数字组合的字符串 随机动画数字
效果描述: 附件中只有一个index.html文件有效 其中包含css以及html两部分内容 纯js生成的几个随机数字 每次都不重复,点击按钮后再次切换 使用方法: 1.将css样式引入到你的网页中 ...
- php实现随机数字、字母的验证码
php实现随机数字.字母的验证码 可自定义生成验证码文字的大小.数量.干扰项等等,也可以自定义验证文字的字体... 废话不多说,直接上代码: 1.classgd.class.php <?php ...
- JavaScript 编写随机四位数验证码(大小写字母和数字)
1.JavaScript编写随机四位数验证码,用到的知识点为: a.Math对象的随机数:Math.random() b.Math对象的取整 :Math.floor() c.处理所需要的下标个数 ...
随机推荐
- QT. 学习之路 二
Qt 的信号槽机制并不仅仅是使用系统提供的那部分,还会允许我们自己设计自己的信号和槽. 举报纸和订阅者的例子:有一个报纸类 Newspaper,有一个订阅者类 Subscriber.Subscribe ...
- 建立属于自己的scrapy crawl模板
本人安装PYTHON3.7安装位置:D:\Python\Python37模板位置:D:\Python\Python37\Lib\site-packages\scrapy\templates\spide ...
- SyntaxError: unexpected EOF while parsing成功解决
报错在eval()函数: 我加了个 if 判断是否为空,就可以正常运行了!
- Linux上生产环境源码方式安装配置postgresql12
1.Linux上源码方式安装postgresql12 01.准备操作系统环境 echo "192.168.1.61 tsepg61" >> /etc/hosts mou ...
- 【JavaWeb】请求和响应Request&Response
请求 请求对象 关于请求 顾名思义,意思就是请求一个"对象" 请求不到的,别想了 请求,就是使用者希望从服务器端索取一些资源,向服务器发出询问.在B/S架构中,就是客户浏览器向服务 ...
- 搭建SAMBA服务
说明:这里是Linux服务综合搭建文章的一部分,本文可以作为单独搭建SABMA服务的参考. 注意:这里所有的标题都是根据主要的文章(Linux基础服务搭建综合)的顺序来做的. 如果需要查看相关软件版本 ...
- C++引用的概念以及基本使用
引言 引用是C++的新增内容,在实际开发中会经常使用:C++用的引用就如同C语言的指针一样重要,但它比指针更加方便和易用. 我们知道,参数的传递本质上是一次赋值的过程,即将一块内存上的数据复制到另一块 ...
- 第二篇 -- Django写一个接口并用Jmeter进行测试
第一节学习了Jmeter的下载和安装,那么第二节就来看看具体怎么使用. 本篇介绍的是使用Jmeter进行http接口测试,那么接口程序使用Django开发的一个小接口. 一.Django编写接口 这一 ...
- Python基础之用PyQt5创建menu
前一篇文章中,我们已经安装了PyQt5,并且已经测试过可用.那么接下来第一步开始学习如何创建菜单. 第一步:在想要运行py的地方右击External Tools-->designer,打开des ...
- Selenium环境搭建 - Mac电脑
一. JDK安装 1.1.官网下载1.8版本 可参考以下链接步骤: 'https://blog.csdn.net/u014801367/article/details/86288078' 1.2.jd ...