验证码是什么
验证码,这个大家应该都见过。最普遍的验证码就是一张上面有4-6个歪歪扭扭的数字字母,还有点看不清楚,但是基本可以肉眼识别出上面的数字字母。那为什么要有这个东东呢?
其实验证码的出现为了区分人与机器。对于歪歪妞妞还有点看不清的数字字母,由于人脑的特殊构造,是可以完全无障碍识别的,但是想让奇迹识别出这些字母数字,就会出现识别错误。那为什么要区别人与机器呢?假如一个一个系统没有验证码,我知道了你的用户名,并且知道你的登录密码是8位的数字,那我完全可以写个脚本程序穷举出所有的8位数组合,挨个去尝试登录,这个过程对于人来说可能耗时耗力,但是对于程序来说,so easy。所以验证码的出现就会阻止程序进行这样的穷举登录。
随着技术的发展,现在很多的验证码系统都可以通过图像处理、机器学习深度学习等方式进行攻破,验证码已经不再安全,即使是非常有名的12306验证码,也已经被利用深度学习达到了很高的识别精度。所以也出现了手机验证码、拖动滑块到指定位置的验证码等各种验证码。
验证码是指网页的验证码还是手机的验证码
下面是随机生成四位数的相关代码
import javautilRandom;public class RandomTest {
public static void main(String[] args) {
Systemoutprintln("Mathrandom得到小数");
Systemoutprintln(Mathround(Mathrandom() 10000));
Systemoutprintln("Random");
Systemoutprintln(new Random()nextInt(9999));
Systemoutprintln("字符串前面补0的话就这样Stringformat");
Systemoutprintln(Stringformat("%04d",new Random()nextInt(9999)));
}
}
后台写一个生成随机的代码,生成给前台。切换的时候,使用ajax获取数据就行。
附上生成的代码
public class ValidateCode {
private int width=180;
private int height=60;
private int codeCount = 4;
private int x = 0;
private int codeY;
private String Code;
private BufferedImage buffImg;
static char[] codeSequence = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
'X', 'Y', 'Z','a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
'x', 'y', 'z', 'o', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
private int fontHeight;
public ValidateCode() {
x = width / (codeCount + 2);
fontHeight = height - 2;
codeY = height - 4;
CreateCode();
}
public void CreateCode(){
// 定义图像buffer
BufferedImage buffImg = new BufferedImage(width, height,BufferedImageTYPE_INT_RGB);
Graphics2D g = buffImgcreateGraphics();
// 创建一个随机数生成器类
Random random = new Random();
// 将图像填充为白色
gsetColor(ColorWHITE);
gfillRect(0, 0, width, height);
// 创建字体,字体的大小应该根据的高度来定。
Font font = new Font("Fixedsys", FontPLAIN, fontHeight);
// 设置字体。
gsetFont(font);
// 画边框。
gsetColor(ColorBLACK);
gdrawRect(0, 0, width - 1, height - 1);
// randomCode用于保存随机产生的验证码,以便用户登录后进行验证。
StringBuffer randomCode = new StringBuffer();
int red = 0, green = 0, blue = 0;
// 随机产生codeCount数字的验证码。
for (int i = 0; i < codeCount; i++) {
// 得到随机产生的验证码数字。
String strRand = StringvalueOf(codeSequence[randomnextInt(62)]);
// 产生随机的颜色分量来构造颜色值,这样输出的每位数字的颜色值都将不同。
red = randomnextInt(255);
green = randomnextInt(255);
blue = randomnextInt(255);
// 用随机产生的颜色将验证码绘制到图像中。
gsetColor(new Color(red, green, blue));
gdrawString(strRand, (i ) x+20, codeY);
// 将产生的四个随机数组合在一起。
randomCodeappend(strRand);
}
thisCode=randomCodetoString()toUpperCase();
thisbuffImg=buffImg;
}
public String getCode() {
return Code;
}
public void setCode(String code) {
Code = code;
}
public BufferedImage getBuffImg() {
return buffImg;
}
public void setBuffImg(BufferedImage buffImg) {
thisbuffImg = buffImg;
}
}
以上就是关于java怎么实现验证码识别全部的内容,包括:java怎么实现验证码识别、用java实现:随机获取4位的验证码、java 登陆时的验证码怎么做等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)