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.处理所需要的下标个数 ...
随机推荐
- Spring Boot中的那些生命周期和其中的可扩展点(转)
前言可扩展点的种类Spring Boot启动过程 1.SpringApplication的启动过程 2.ApplicationContext的启动过程 3.一般的非懒加载单例Bean在Spring B ...
- Java | Map集合
Map集合 在现实生活中,有非常多的东西,是和另外一种东西对应的,并且还是唯一的,比如:身份证号与个人,个人与手机,一夫一妻...等,这种关系就是对应关系,又叫做映射.Java为这种数据类型提供了专门 ...
- mybatis 加载策略及注解开发
1. 延迟策略 在需要用到数据时在加载相关数据,常用于一对多关系, 优点:先从单表查询,需要时再从关联表去关联查询,大大提高数据库性能, 缺点:当需要用到数据时,才会进行数据库查询,这样在大批量数据查 ...
- git rebase 和 git merger
& git merge 在上图中,每一个绿框均代表一个commit.除了c1,每一个commit都有一条有向边指向它在当前branch当中的上一个commit. 图中的项目,在c2之后就开了另 ...
- LeetCode 982. Triples with Bitwise AND Equal To Zero
题目链接:https://leetcode.com/problems/triples-with-bitwise-and-equal-to-zero/ 题意,已知数组A,长度不超过1000,最大的数不超 ...
- linux安装tomcat后启动报错Cannot find ./catalina.sh的解决方法
linux安装tomcat后启动报错: Cannot find ./catalina.shThe file is absent or does not have execute permissionT ...
- DRF使用JWT进行用户认证
1. 首先需要安装第三方依赖包 pip install djangorestframework-jwt 2. 在Django的settings文件中 配置全局的JWT认证类 REST_FRAMEWOR ...
- 判断Windows系统是32位或64位并执行不同脚本命令
判断Windows系统是32位或64位并执行不同脚本命令 https://www.autoahk.com/?p=16549&preview=true https://www.cnblogs.c ...
- 离线安装zadig
官网:https://koderover.com/ 官方给的离线安装问题有些问题,这里记录下自己离线安装的一些过程. 整体安装思路: 根据官方给出的helm安装方式以及离线安装方式结合而来. 通过官方 ...
- [洛谷P3376题解]网络流(最大流)的实现算法讲解与代码
[洛谷P3376题解]网络流(最大流)的实现算法讲解与代码 更坏的阅读体验 定义 对于给定的一个网络,有向图中每个的边权表示可以通过的最大流量.假设出发点S水流无限大,求水流到终点T后的最大流量. 起 ...