java登录的 验证码_java登录验证码

一、创建web项目

二、生成验证码

创建VcodeObject.java,存储验证码与图片

package com.demo.vcode;

import java.io.InputStream;

public class VcodeObject {

private String code;

private InputStream in;

public VcodeObject() {

}

public VcodeObject(String code, InputStream in) {

this.code = code;

this.in = in;

}

//getters & setters

}

创建Vcode.java生成验证码与图片

package com.demo.vcode;

import java.awt.Color;

import java.awt.Font;

import java.awt.Graphics;

import java.awt.image.BufferedImage;

import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.util.Random;

import javax.imageio.ImageIO;

public class Vcode {

public VcodeObject create() {

// 生成随机验证码

String code = this.generateRandomVerifyCode(4);

// 生成附带验证码的图片

BufferedImage img = this.generateImgWithCode(code, 80, 34);

InputStream in = this.putImgInStream(img);

return new VcodeObject(code, in);

}

/**

* 随机生成验证码

*

* @param length

* @return String

*/

private String generateRandomVerifyCode(int length) {

// 需要生成验证码的长度

if (length <= 0 || length >= 8)

length = 4;

// 随机生成字符的范围

String scope = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

StringBuffer code = new StringBuffer();

Random random = new Random();

for (int i = 0; i < length; i++) {

code.append(scope.charAt(random.nextInt(62)));

}

return code.toString();

}

/**

* 生成带有验证码的图片

*

* @param code

* @param width

* @param height

* @return BufferedImage

*/

private BufferedImage generateImgWithCode(String code, int width, int height) {

BufferedImage img = new BufferedImage(width, height,

BufferedImage.TYPE_INT_RGB);

Graphics brush = img.getGraphics();

// 设置背景和边框颜色

brush.setColor(this.getColor());

brush.fillRect(0, 0, width - 1, height - 1);

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

// 写入验证码

brush.setColor(this.getColor());

brush.setFont(new Font("Times New Roman", Font.ITALIC, 28));

brush.drawString(code, 7, 22);

// 绘制干扰线条

// 图片画完,关闭资源

brush.dispose();

return img;

}

/**

* 获取随机颜色

*

* @return Color

*/

private Color getColor() {

Random random = new Random();

int r = random.nextInt(255);

int g = random.nextInt(255);

int b = random.nextInt(255);

return new Color(r, g, b);

}

/**

* 将图片放入输入流中

*

* @param image

* @return InputStream

*/

private InputStream putImgInStream(BufferedImage image) {

ByteArrayOutputStream out = new ByteArrayOutputStream();

try {

ImageIO.write(image, "JPEG", out);

} catch (IOException e) {

e.printStackTrace();

}

InputStream in = new ByteArrayInputStream(out.toByteArray());

return in;

}

}

三、输出验证码

创建DemoServlet.java

package com.demo.vcode;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import javax.servlet.ServletConfig;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class DemoServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

public void init(ServletConfig config) throws ServletException {

}

public void doGet(HttpServletRequest req, HttpServletResponse res)

throws IOException, ServletException {

res.setContentType("image/png");

Vcode vcode = new Vcode();

VcodeObject vcodeObject = vcode.create();

InputStream in = vcodeObject.getIn();

req.getSession().setAttribute("vcode", vcodeObject.getCode());

byte[] bytes = new byte[1024];

int length = 0;

try {

OutputStream out = res.getOutputStream();

while ((length = in.read(bytes)) != -1) {

out.write(bytes, 0, length);

}

in.close();

out.flush();

} catch (Exception e) {

}

}

public void doPost(HttpServletRequest req, HttpServletResponse resp)

throws IOException, ServletException {

}

public void destroy() {

}

}

配置web.xml

xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

version="2.5">

vcode

vcode

com.demo.vcode.DemoServlet

vcode

/vcode

浏览器地址栏访问

http://127.0.0.1:8080/vcode/vcode

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用 Java 实现邮箱验证码的示例代码: ```java import java.util.Properties; import javax.mail.*; import javax.mail.internet.*; public class EmailVerification { public static void main(String[] args) throws MessagingException { String to = "recipient@example.com"; // 收件人邮箱地址 String from = "sender@example.com"; // 发件人邮箱地址 String host = "smtp.example.com"; // SMTP 服务器 // 获取系统属性 Properties properties = System.getProperties(); // 设置邮件服务器 properties.setProperty("mail.smtp.host", host); // 获取默认的 Session 对象 Session session = Session.getDefaultInstance(properties); // 创建一封邮件 MimeMessage message = new MimeMessage(session); // 设置发件人地址 message.setFrom(new InternetAddress(from)); // 设置收件人地址 message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // 设置邮件主题 message.setSubject("邮箱验证码"); // 生成随机验证码 String code = generateCode(); // 设置邮件正文 message.setText("您的验证码是:" + code); // 发送邮件 Transport.send(message); } // 生成随机验证码 private static String generateCode() { int codeLength = 6; // 验证码长度 String code = ""; for (int i = 0; i < codeLength; i++) { code += (int) (Math.random() * 10); } return code; } } ``` 以上代码使用 JavaMail API 实现了发送一封带有随机验证码的电子邮件。你需要替换 `to`、`from` 和 `host` 变量的值为你自己的邮箱地址和 SMTP 服务器地址,然后就可以运行代码了。 需要注意的是,在发送邮件之前,你需要先引入 JavaMail API 库,并且需要认证你的邮箱账户信息。具体实现方式可以参考 JavaMail 官方文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值