基于Java实现随机字母数字验证码

开发
今天给大家介绍Java实现随机字母数字验证码

 [[344446]]

生成随机验证码
VerifyCode 工具类

  1. package com.meeno.common.cerifycode; 
  2. import javax.imageio.ImageIO; 
  3. import java.awt.*; 
  4. import java.awt.image.BufferedImage; 
  5. import java.io.FileNotFoundException; 
  6. import java.io.FileOutputStream; 
  7. import java.io.IOException; 
  8. import java.io.OutputStream; 
  9. import java.util.Random; 
  10. /** 
  11.  * @description: 随机验证码 
  12.  * @author: Wzq 
  13.  * @create: 2020-09-08 16:55 
  14.  */ 
  15. public class VerifyCode { 
  16.     private int w=70; 
  17.     private int h=35; 
  18.     private Random r= new Random(); 
  19.     private String[] fontNames={"宋体","华文楷体","黑体","微软雅黑","楷体_GB2312"}; 
  20.     private String codes="012345678901234567890123456789abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"
  21.     private Color bgColor = new Color(255,255,255); 
  22.     private String text; 
  23.     private Color randomColor(){ 
  24.         int red = r.nextInt(150); 
  25.         int green = r.nextInt(150); 
  26.         int blue = r.nextInt(150); 
  27.         return new Color(red,green,blue); 
  28.     }    private Font randomFont(){ 
  29.         int index = this.r.nextInt(fontNames.length); 
  30.         String fontName = fontNames[index];        int style = this.r.nextInt(4); 
  31.         int size = this.r.nextInt(5) + 24; 
  32.         return new Font(fontName, style, size); 
  33.     }    private void drawLine (BufferedImage image){ 
  34.         int num = 3; 
  35.         Graphics2D g2=(Graphics2D)image.getGraphics();        for(int i=0;i<num;i++){ 
  36.             int x1=r.nextInt(w); 
  37.             int y1=r.nextInt(h); 
  38.             int x2=r.nextInt(w); 
  39.             int y2=r.nextInt(h); 
  40.             g2.setStroke(new BasicStroke(1.5F)); 
  41.             g2.setColor(Color.BLUE);            g2.drawLine(x1,y1,x2,y2);        }    }    private char randomChar(){ 
  42.         int index=r.nextInt(this.codes.length()); 
  43.         return this.codes.charAt(index); 
  44.     }    private BufferedImage createImage(){ 
  45.         BufferedImage image=new BufferedImage(this.w,this.h,BufferedImage.TYPE_INT_RGB);//BufferedImage.TYPE_INT_RGB 
  46.         Graphics2D g2 = (Graphics2D)image.getGraphics(); 
  47.         g2.setColor(this.bgColor); 
  48.         g2.fillRect(0,0,this.w,this.h); 
  49.         return image; 
  50.     } 
  51.     public BufferedImage getImage(){ 
  52.         BufferedImage image=createImage(); 
  53.         Graphics2D g2=(Graphics2D)image.getGraphics(); 
  54.         StringBuilder&nbsp;sb&amp;amp;nbsp;= new StringBuilder(); 
  55.         for(int i =0;i<4;i++){ 
  56.             String s= randomChar()+""
  57.            &nbsp;sb.append(s); 
  58.             float x= i*1.0F*this.w/4.0F; 
  59.             g2.setFont(randomFont()); 
  60.             g2.setColor(randomColor()); 
  61.             g2.drawString(s,x,this.h-5); 
  62.         } 
  63.         this.text=sb.toString(); 
  64.         drawLine(image);//添加干扰线 
  65.         return image; 
  66.     } 
  67.     //返回验证码上的文本 
  68.     public String getText(){ 
  69.         return this.text; 
  70.     } 
  71.     //保存图片到指定的输出流 
  72.     public static void output(BufferedImage image, OutputStream out
  73.             throws IOException { 
  74.         ImageIO.write(image,"JPEG",out); 
  75.     } 
  76.     public static void main(String[] args) throws IOException { 
  77.         VerifyCode vc=new VerifyCode(); 
  78.         BufferedImage bi = vc.getImage(); 
  79.         VerifyCode.output(bi,new FileOutputStream("E:\\work\\temp\\xxx.jpg")); 
  80.         System.out.println("图片中的验证是:"+vc.getText()); 
  81.     } 

使用

  1. /** 
  2.      * 生成随机验证码     */    @RequestMapping("randomVerifyCode.do"
  3.     public ResponseBean randomVerifyCode() throws IOException {        VerifyCode vc = new VerifyCode();        BufferedImage bi = vc.getImage();        //VerifyCode.output(bi,response.getOutputStream()); 
  4.         ByteArrayOutputStream out = new ByteArrayOutputStream();        ImageIO.write(bi, "png"out); 
  5.         byte[] bytes = out.toByteArray(); 
  6.         String str = Base64.encode(bytes);        str = "data:image/png;base64," + str; 
  7.         String uuid = IdUtil.simpleUUID();        Map<String,String> resultMap = Maps.newHashMap();        resultMap.put("base64", str); 
  8.         resultMap.put("uuid", uuid); 
  9.         //save redis        String key = "RandomVerify:" + uuid; 
  10.         RedisUtil.set(key, vc.getText(), 180); 
  11.         return ResultUtil.success(resultMap); 
  12.     } 

验证方法

  1. /** 
  2.      * 账号登录 
  3.      * @param session 
  4.      * @param phone 
  5.      * @param pwd 
  6.      * @param entryType 
  7.      * @return 
  8.      */ 
  9.     @RequestMapping("accountLogin.do"
  10.     public ResponseBean accountLogin(final HttpSession session, String phone, String pwd, String entryType, 
  11.                                      String uuid, String randomCerifyCode){ 
  12.         //校验验证码 
  13.         MeenoAssert.hasLength(randomCerifyCode,"randomCerifyCode can not empty!"); 
  14.         Object randomCerifyCodeObj = RedisUtil.get("RandomVerify:" + uuid); 
  15.         MeenoAssert.notNull(randomCerifyCodeObj, CErrEnum.RANDOM_VERIFY_CODE_FAILURE); 
  16.         MeenoAssert.isTrue(randomCerifyCode.toLowerCase().equals(randomCerifyCodeObj.toString().toLowerCase()), CErrEnum.RANDOM_VERIFY_CODE_ERR); 
  17.         LoginResult loginResult = this.employeeService.accountLogin(session, phone, pwd, entryType); 
  18.         EmpView employeeView = this.employeeService.getEmployee(loginResult.getUserInfo().getId()); 
  19.         Map<String,Object> resultMap = Maps.newHashMap(); 
  20.         resultMap.put("loginResult", loginResult); 
  21.         resultMap.put("employee", employeeView); 
  22.         return ResultUtil.success(resultMap); 
  23.     } 

 

 

责任编辑:姜华 来源: 今日头条
相关推荐

2015-09-21 15:31:05

php实现验证码

2009-12-16 15:46:41

Ruby on rai

2013-06-19 10:19:59

2009-06-26 15:17:27

jQuery

2009-11-23 16:59:23

PHP图形验证码

2020-11-16 07:28:53

验证码

2009-02-09 14:17:36

2009-08-11 14:05:28

JSP验证码

2017-12-21 07:38:19

2015-03-23 17:58:04

验证码倒计时并行

2021-01-19 10:29:34

短信验证码密码

2022-02-11 07:10:15

验证码

2022-02-02 20:21:24

短信验证码登录

2024-05-14 08:16:13

Spring验证码系统

2015-03-17 09:28:04

2011-11-02 12:43:33

2011-11-02 16:46:41

2019-06-18 07:12:25

验证码漏洞加密

2023-09-22 11:51:13

PythonFlask
点赞
收藏

51CTO技术栈公众号