没有合适的资源?快使用搜索试试~ 我知道了~
首页JSP实现登录功能之添加验证码
JSP实现登录功能之添加验证码
8 下载量 157 浏览量
更新于2023-05-10
评论 1
收藏 76KB PDF 举报
jsp登陆验证,网页登陆验证带验证码校验,登录功能之添加验证码,非常不错,具有参考借鉴价值,需要的朋友参考下吧
资源详情
资源评论
资源推荐
JSP实现登录功能之添加验证码实现登录功能之添加验证码
jsp登陆验证,网页登陆验证带验证码校验,登录功能之添加验证码,非常不错,具有参考借鉴价值,需要的朋友参考下吧
jsp登陆验证,网页登陆验证带验证码校验,登录功能之添加验证码
part_1:专门用于生成一个验证码图片的类:专门用于生成一个验证码图片的类:VerificationCode.Java
package cn.mike.javase.test;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import javax.imageio.ImageIO;
import org.junit.Test;
/**
* @author : Administrator
* @function : 这是用来测试随机生成验证码图片的类;
*/
public class VerificationCode {
/**
* 单元测试,试一下能不能自动生成验证码图片
*/
// 这个函数是单元测试时使用的,这里private一下外面就调用不到了;
/* @Test */
/* public */private void test_fun() {
VerificationCode vc = new VerificationCode();
BufferedImage image = vc.getImage();
try {
// 生成验证码图片,并保存到指定的路径
VerificationCode.output(image, new FileOutputStream(new File(
".\image\vcode_2.jpg")));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// 将随机生成的文本内容输出到控制台,用于校验
System.out.println(vc.getText());
}
private int w = 70;// 宽
private int h = 35;// 高
private String text;// 文本内容(验证码字符串)
private Random r = new Random();
private String[] fontNames = { "宋体", "华文楷体", "黑体", "微软雅黑", "楷体_GB2312" };
// 随机字符集合中不包括0和o,O,1和l,因为这些不易区分
private String codes = "23456789abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYXZ";
// 验证码图片的背景色:白色
private Color bgColor = new Color(255, 255, 255);
/**
* 返回一个验证码图片buffer对象:BufferedImage
*/
public BufferedImage getImage() {
BufferedImage image = createImage();
// 获取绘图环境(画笔工具)
Graphics2D g2 = (Graphics2D) image.getGraphics();
// sb : 用来保存验证码字符串文本内容
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 4; ++i) {// 随机生成4个字符
String s = randomChar() + "";
sb.append(s);
float x = i * 1.0F * w / 4;
g2.setFont(randomFont());
g2.setColor(randomColor());
g2.drawString(s, x, h - 5);
}
this.text = sb.toString();// 记录验证码文本内容
drawLine(image);// 画干扰线
return image;
}
/**
* @return 获取验证码文本内容
*/
public String getText() {
return text;
}
/**
* @param image
* @param out
* 将文本写到指定的输出流。比如本测试中FileOutputStream指定的保存路径
*/
public static void output(BufferedImage image, OutputStream out) {
try {
ImageIO.write(image, "jpeg", out);
} catch (IOException e) {
e.printStackTrace();
}
}
private void drawLine(BufferedImage image) {
Graphics2D g2 = (Graphics2D) image.getGraphics();
for (int i = 0; i < 3; ++i) {// 画3条干扰线
int x1 = r.nextInt(w);
int y1 = r.nextInt(h);
int x2 = r.nextInt(w);
int y2 = r.nextInt(h);
g2.setColor(Color.BLUE);
g2.drawLine(x1, y1, x2, y2);
}
}
private Color randomColor() {
weixin_38654944
- 粉丝: 2
- 资源: 943
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- zigbee-cluster-library-specification
- JSBSim Reference Manual
- c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf
- 建筑供配电系统相关课件.pptx
- 企业管理规章制度及管理模式.doc
- vb打开摄像头.doc
- 云计算-可信计算中认证协议改进方案.pdf
- [详细完整版]单片机编程4.ppt
- c语言常用算法.pdf
- c++经典程序代码大全.pdf
- 单片机数字时钟资料.doc
- 11项目管理前沿1.0.pptx
- 基于ssm的“魅力”繁峙宣传网站的设计与实现论文.doc
- 智慧交通综合解决方案.pptx
- 建筑防潮设计-PowerPointPresentati.pptx
- SPC统计过程控制程序.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0