「java带验证码登录」java登录验证码怎么做

博主:adminadmin 2023-01-28 04:12:06 631

今天给各位分享java带验证码登录的知识,其中也会对java登录验证码怎么做进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java 登陆时的验证码怎么做?

后台写一个生成图片随机的代码,生成图片给前台。切换图片的时候,使用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,BufferedImage.TYPE_INT_RGB);

Graphics2D g = buffImg.createGraphics();

// 创建一个随机数生成器类

Random random = new Random();

// 将图像填充为白色

g.setColor(Color.WHITE);

g.fillRect(0, 0, width, height);

// 创建字体,字体的大小应该根据图片的高度来定。

Font font = new Font("Fixedsys", Font.PLAIN, fontHeight);

// 设置字体。

g.setFont(font);

// 画边框。

g.setColor(Color.BLACK);

g.drawRect(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 = String.valueOf(codeSequence[random.nextInt(62)]);

// 产生随机的颜色分量来构造颜色值,这样输出的每位数字的颜色值都将不同。

red = random.nextInt(255);

green = random.nextInt(255);

blue = random.nextInt(255);

// 用随机产生的颜色将验证码绘制到图像中。

g.setColor(new Color(red, green, blue));

g.drawString(strRand, (i ) * x+20, codeY);

// 将产生的四个随机数组合在一起。

randomCode.append(strRand);

}

this.Code=randomCode.toString().toUpperCase();

this.buffImg=buffImg;

}

public String getCode() {

return Code;

}

public void setCode(String code) {

Code = code;

}

public BufferedImage getBuffImg() {

return buffImg;

}

public void setBuffImg(BufferedImage buffImg) {

this.buffImg = buffImg;

}

}

java怎么开通短信验证码登录功能?

实现jiava短信验证码可以按下面的步奏进行:

1、首先,找到一个支持Java语言的接口短信平台。

2、接着下载接口文档,和自己的开发平台进行对接。

3、注意在对接之前测试一下短信的速度,一旦对接好想换就比较麻烦,之前就吃过这个亏,最后有个朋友介绍我去短信网。

4、如果要购买的话,一定要多测试几家。

如果在碰到有疑问的地方一定要和技术或者客服多多沟通。

java post登录带验证码怎么办

生成验证码的时候把验证码的正确值保存到session中,然后登录时把验证码参数传给servlet,比对request和session中的验证码,如果一样就通过

java swing中登录界面验证码的实现。

public class ValidCode extends JComponent implements MouseListener {

private String code;

private int width, height = 40;

private int codeLength = 4;

private Random random = new Random();

public ValidCode() {

width = this.codeLength * 16 + (this.codeLength - 1) * 10;

setPreferredSize(new Dimension(width, height));

setSize(width, height);

this.addMouseListener(this);

setToolTipText("点击可以更换验证码");

}

public int getCodeLength() {

return codeLength;

}

/*

设置验证码文字的长度

*/

public void setCodeLength(int codeLength) {

if(codeLength 4) {

this.codeLength = 4;

} else {

this.codeLength = codeLength;

}

}

public String getCode() {

return code;

}

/*

产生随机的颜色

*/

public Color getRandColor(int min, int max) {

if (min 255)

min = 255;

if (max 255)

max = 255;

int red = random.nextInt(max - min) + min;

int green = random.nextInt(max - min) + min;

int blue = random.nextInt(max - min) + min;

return new Color(red, green, blue);

}

/*

设置验证码具体的字母是什么

*/

protected String generateCode() {

char[] codes = new char[this.codeLength];

for (int i = 0, len = codes.length; i len; i++) {

if (random.nextBoolean()) {

codes[i] = (char) (random.nextInt(26) + 65);

} else {

codes[i] = (char) (random.nextInt(26) + 97);

}

}

this.code = new String(codes);

return this.code;

}

@Override

protected void paintComponent(Graphics g) {

super.paintComponent(g);

if(this.code == null || this.code.length() != this.codeLength) {

this.code = generateCode();

}

width = this.codeLength * 16 + (this.codeLength - 1) * 10;

super.setSize(width, height);

super.setPreferredSize(new Dimension(width, height));

Font mFont = new Font("Arial", Font.BOLD | Font.ITALIC, 25);

g.setFont(mFont);

//绘制出验证码的背景的矩形轮廓

Graphics2D g2d = (Graphics2D) g;

g2d.setColor(getRandColor(200, 250));

g2d.fillRect(0, 0, width, height);

g2d.setColor(getRandColor(180, 200));

g2d.drawRect(0, 0, width - 1, height - 1);

//绘制出验证码背景的线

int i = 0, len = 150;

for (; i len; i++) {

int x = random.nextInt(width - 1);

int y = random.nextInt(height - 1);

int x1 = random.nextInt(width - 10) + 10;

int y1 = random.nextInt(height - 4) + 4;

g2d.setColor(getRandColor(180, 200));

g2d.drawLine(x, y, x1, y1);

}

/*i = 0; len = 300;

for (; i len; i++) {

int x = random.nextInt(width);

int y = random.nextInt(height);

g2d.setColor(getRandColor(150, 180));

g2d.drawRect(x, y, 0, 0);

}*/

//绘制出验证码的具体字母

i = 0; len = this.codeLength;

FontMetrics fm = g2d.getFontMetrics();

int base = (height - fm.getHeight())/2 + fm.getAscent();

for(;ilen;i++) {

int b = random.nextBoolean() ? 1 : -1;

g2d.rotate(random.nextInt(10)*0.01*b);

g2d.setColor(getRandColor(20, 130));

g2d.drawString(code.charAt(i)+"", 16 * i + 10, base);

}

}

//下一个验证码

public void nextCode() {

generateCode();

repaint();

}

@Override

public void mouseClicked(MouseEvent e) {

nextCode();

}

@Override

public void mousePressed(MouseEvent e) {

// TODO Auto-generated method stub

}

@Override

public void mouseReleased(MouseEvent e) {

// TODO Auto-generated method stub

}

@Override

public void mouseEntered(MouseEvent e) {

// TODO Auto-generated method stub

}

@Override

public void mouseExited(MouseEvent e) {

// TODO Auto-generated method stub

}

}

关于java带验证码登录和java登录验证码怎么做的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。