服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Java动态验证码单线设计的两种方法

Java动态验证码单线设计的两种方法

2021-05-19 14:13垂眸爱笑约 Java教程

这篇文章主要介绍了Java动态验证码单线设计的两种方法,需要的朋友可以参考下

1.java的动态验证码我这里将介绍两种方法:

一:根据java本身提供的一种验证码的写法,这种呢只限于大家了解就可以了,因为java自带的模式编写的在实际开发中是没有意义的,所以只供学习一下就可以了,待会讲解的第二种呢就是我们需要掌握的一种模式了:

第一种的代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import java.awt.color;
import java.awt.font;
import java.awt.graphics;
import java.awt.image.bufferedimage;
import java.io.ioexception;
import java.util.random;
import javax.imageio.imageio;
import javax.servlet.servletexception;
import javax.servlet.annotation.webservlet;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import javax.servlet.http.httpsession;
/**
 * servlet implementation class inde
 */
@webservlet("/inde")
public class inde extends httpservlet {
  private static final long serialversionuid = 1l;
  /**
   * @see httpservlet#httpservlet()
   */
  public inde() {
    super();
    // todo auto-generated constructor stub
  }
  /**
   * @see httpservlet#doget(httpservletrequest request, httpservletresponse response)
   */
  protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
      this.dopost(request, response);     
  }
  /**动态生成图片验证码
   * @see httpservlet#dopost(httpservletrequest request, httpservletresponse response)
   */
  protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
    // todo auto-generated method stub
    //创建图像
    int width=100;
    int height=40;
    //图片的大小设置
    bufferedimage image=new bufferedimage(width,height,bufferedimage.type_int_rgb);
    //创建画板   
    graphics g=image.getgraphics(); 
     setsquarebackground(g,width,height,5);
    //确定画笔颜色
    g.setcolor(color.black);
    //填充矩形
    g.fillrect(0, 0, width, height);
      //在大矩形中放小矩形
      g.setcolor(color.white);
      g.fillrect(1, 1, width-2, height-2);       
    //填充字符
    string str = "qwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnm0123456789";
    stringbuffer sb=new stringbuffer();
     //画随机干扰框
    setsquarebackground(g,width,height,3);
    //画干扰点
    createrandompoint(width, height,100,g,100);   
    //随机画几条线
    createrandomline(width, height,8,g,100); 
    //随机获取4个字符
    random random=new random();
    for (int i = 0; i < 4; i++) {
      //62个填充字符里面随机的随机的收取字符
      int index=random.nextint(62);
      //截取一个字符
      string st=str.substring(index, index+1);
      //把字符放到图片中去
      g.setcolor(color.red);
      //设置字体
      g.setfont(new font("宋体",font.bold,30));
      g.drawstring(st, 20*i, 30);//防止4个字符在一起
      sb.append(st);
    }
    //把stringbuffer中的验证码放到session里面,目的是让login调用
    httpsession se=request.getsession();
    se.setattribute("number", sb.tostring());
    //发送图片到浏览器 指定发送的图片 和格式
    response.setcontenttype("image/jpeg");
    //图片,图片的格式,输出的方式
    imageio.write(image, "jpg", response.getoutputstream());
  }
  random rand = new random();
  private void createrandompoint(int width,int height,int many,graphics g,int alpha)
  { // 随机产生干扰点
    for (int i=0;i<many;i++) {
      int x = rand.nextint(width);
      int y = rand.nextint(height);
      g.setcolor(getcolor(alpha));
      g.drawoval(x,y,rand.nextint(3),rand.nextint(3));
    }
  }
/**
 * 随机产生干扰线条
 * @param width
 * @param height
 * @param minmany 最少产生的数量
 * @param g
 * @param alpha 透明度0~255 0表示全透
 */
  private void createrandomline(int width,int height,int minmany,graphics g,int alpha)
  { // 随机产生干扰线条
    for (int i=0;i<getintrandom(minmany, minmany+6);i++) {
      int x1 =getintrandom(0,(int)(width*0.6));
      int y1 =getintrandom(0,(int)(height*0.6));
      int x2 =getintrandom((int)(width*0.4),width);
      int y2 =getintrandom((int)(height*0.2),height);
      g.setcolor(getcolor(alpha));
      g.drawline(x1, y1, x2, y2);
    }
  }
  /**
   * 由随机产生的方块来作为干扰背景
   */
  private void setsquarebackground(graphics g,int width,int height,int count){
    // 随机产生干扰线条
    for (int i=0;i<getintrandom(count, count+2);i++) {
      int x1 =getintrandom(0,(int)(width*0.3));
      int y1 =getintrandom(0,(int)(height*0.3));
      int x2 =getintrandom((int)(width*0.5),width);
      int y2 =getintrandom((int)(height*0.55),height);
      g.setcolor(getcolor(100));
      int w=x2-x1;
      int h=y2-y1;
      if(w<0) w=-w;
      if(h<0) h=-h;
      g.drawrect(x1, y1, w, h);
      g.setcolor(getcolor(25));
      g.fillrect(x1, y1, w, h);
    }
  }
  private int getintrandom(double start,double end)
  if(end<start)
    {
     double t=end;
     end=start;
     start=t;
    }
    double i=start+(int) (math.random()*(end-start));
    return (int)i;
  }

上面的代码呢写的很详细了,这里我就不重复了,这里的干扰线条是有很多写法的,我这里就没有全写出来,有需要的话可以私聊我哦!

下面介绍第二种:

这种呢是我们开发中是可以用得到的,使用在的是网页端的交互,我们在登录网站的时候有很多的验证码就可以用这个来写了

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import java.io.ioexception;
import java.util.random;
import javax.imageio.imageio;
import javax.servlet.servletexception;
import javax.servlet.annotation.webservlet;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import javax.servlet.http.httpsession;
/**
 * servlet implementation class inde
 */
@webservlet("/inde")
public class inde extends httpservlet {
  private static final long serialversionuid = 1l;
  /**
   * @see httpservlet#httpservlet()
   */
  public inde() {
    super();
    // todo auto-generated constructor stub
  }
  /**
   * @see httpservlet#doget(httpservletrequest request, httpservletresponse response)
   */
  protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
      this.dopost(request, response);     
  }
  /**动态生成图片验证码
   * @see httpservlet#dopost(httpservletrequest request, httpservletresponse response)
   */
  protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
    // todo auto-generated method stub
    //创建图像
    int width=100;
    int height=40;
    //图片的大小设置
    bufferedimage image=new bufferedimage(width,height,bufferedimage.type_int_rgb);
    //创建画板   
    graphics g=image.getgraphics(); 
     setsquarebackground(g,width,height,5);
    //确定画笔颜色
    g.setcolor(color.black);
    //填充矩形
    g.fillrect(0, 0, width, height);
      //在大矩形中放小矩形
      g.setcolor(color.white);
      g.fillrect(1, 1, width-2, height-2);       
    //填充字符
    string str = "qwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnm0123456789";
    stringbuffer sb=new stringbuffer();
//随机获取4个字符
    random random=new random();
    for (int i = 0; i < 4; i++) {
      //62个填充字符里面随机的随机的收取字符
      int index=random.nextint(62);
      //截取一个字符
      string st=str.substring(index, index+1);
      //把字符放到图片中去
      g.setcolor(color.red);
      //设置字体
      g.setfont(new font("宋体",font.bold,30));
      g.drawstring(st, 20*i, 30);//防止4个字符在一起
      sb.append(st);
    }
//把stringbuffer中的验证码放到session里面,目的是让login调用
    httpsession se=request.getsession();
    se.setattribute("number", sb.tostring());
    //发送图片到浏览器 指定发送的图片 和格式
    response.setcontenttype("image/jpeg");
    //图片,图片的格式,输出的方式
    imageio.write(image, "jpg", response.getoutputstream());
  }

这样就可以运行了,当然了我们可以在这里面加入第一种的干扰模块给拿过来这样就可以实现整体的一个效果了。

总结

以上所述是小编给大家介绍的java动态验证码单线设计的两种方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

原文链接:https://www.cnblogs.com/shuiqian/archive/2018/07/22/9352161.html

延伸 · 阅读

精彩推荐