生成随街验证码

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实现随机字母数字验证码的更多相关文章

  1. 007-TreeMap、Map和Bean互转、BeanUtils.copyProperties(A,B)拷贝、URL编码解码、字符串补齐,随机字母数字串

    一.转换 1.1.TreeMap 有序Map 无序有序转换 使用默认构造方法: public TreeMap(Map<? extends K, ? extends V> m) 1.2.Ma ...

  2. canvas验证码 - 随机字母数字

    基于canvas制作随机生成数字英文组合验证码效果,点击或刷新会自动重组.输入验证码提交验证效果代码. <div class="verification"> <i ...

  3. PHP字母数字验证码和中文验证码

    1:字母数字组合的验证码 HTML代码: 验证码:<input type="text" name="code"> <img onclick=& ...

  4. 字母数字、字母、汉字验证码 (java)

    原文:http://blog.csdn.net/qh_java/article/details/49854477 一.字母数字,字母,汉字验证码的生成代码 1.字母数字验证码: package com ...

  5. php生成纯数字、字母数字、图片、纯汉字的随机数验证码

    现在讲开始通过PHP生成各种验证码旅途,新手要开车了,请刷卡! 首先,我们开始先生成一个放验证码的背景图片 注:没有Imagejpg()这个函数,只有imagepng()函数 imagecreatet ...

  6. JS生成随机的由字母数字组合的字符串

    前言 最近有个需求,是需要生成3-32位长度的字母数字组合的随机字符串,另一个是生成43位随机字符串. 方法一 奇妙的写法   1 Math.random().toString(36).substr( ...

  7. js随机生成字母数字组合的字符串 随机动画数字

    效果描述: 附件中只有一个index.html文件有效 其中包含css以及html两部分内容 纯js生成的几个随机数字 每次都不重复,点击按钮后再次切换 使用方法: 1.将css样式引入到你的网页中 ...

  8. php实现随机数字、字母的验证码

     php实现随机数字.字母的验证码 可自定义生成验证码文字的大小.数量.干扰项等等,也可以自定义验证文字的字体... 废话不多说,直接上代码: 1.classgd.class.php <?php ...

  9. JavaScript 编写随机四位数验证码(大小写字母和数字)

    1.JavaScript编写随机四位数验证码,用到的知识点为: a.Math对象的随机数:Math.random() b.Math对象的取整    :Math.floor() c.处理所需要的下标个数 ...

随机推荐

  1. Linux | 系统状态检测

    ifconfig ifconfig 命令用于获取网卡配置与网络状态等信息,格式 ifconfig[网络设备][参数] 使用 ifconfig 命令来查看本机当前的网卡配置与网络状态等信息时,其实主要查 ...

  2. H3C交换机常用命令

    选择多个端口: interface range ethernet 1/0/1 to ethernet 1/0/12 vlan-interface1 常用命令 密码修改:  查看是否有相应的用户名:di ...

  3. React组件三大属性之 refs

    React组件三大属性之 refs refs属性 1) 组件内的标签都可以定义ref属性来标识自己 a. <input type="text" ref={input => ...

  4. Spring RestTemplate 之exchange方法

    ●exchange方法提供统一的方法模板进行四种请求:POST,PUT,DELETE,GET (1)POST请求 String reqJsonStr = "{\"code\&quo ...

  5. Ubuntu21.04 / Linux Mint20.2 安装 TradingView分析软件

    TradingView 是我们经常使用的一个分析工具!在Linux 下安装桌面版,就可以爽快的用起来了,目前是Beta测试版,相信很快就会有正式版的 1.打开终端,执行下列命令,更新系统到最新 sud ...

  6. jvm源码解读--19 Java的join()方法解读 以及 invokestatic 字节码 执行 流程图

  7. ;~ 小部分AutoHotkey脚本源代码测试模板样板.ahk

    ; ;~ 小部分AutoHotkey脚本源代码测试模板样板.ahk ;~ 请把一行或几行少量代码放到此文件中实际测试一下,;~ 看看测试结果如何,等到能够实现代码功能时再复制到自己的脚本代码文件中;~ ...

  8. 终拿字节Offer...动态规划复盘...

    大家好!我是 Johngo 呀! 和大家一起刷题不快不慢,没想到已经进行到了第二阶段,「动态规划」这部分题目很难,而且很不容易理解,目前我的题目做了一半,凭着之前对于「动态规划」的理解和最近做的题目做 ...

  9. 2020年最新阿里、字节、腾讯、京东等一线大厂高频面试(Android岗)真题合集,面试轻松无压力

    本文涵盖了阿里巴巴.腾讯.字节跳动.京东.华为等大厂的Android面试真题,不管你是要面试大厂还是普通的互联网公司,这些面试题对你肯定是有帮助的,毕竟大厂一定是行发展的标杆,很多公司的面试官同样会研 ...

  10. idea启动桌面出现三个日志文件解决办法

    idea一启动就出现上图三个日志文件,soapui是一个接口测试工具插件,在setting中搜索soapui,一般不需要,卸载掉就可以了.