第一范文网 - 专业文章范例文档资料分享平台

基于SSH2框架的登录验证码的实现

来源:用户分享 时间:2025/6/26 1:08:31 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

logger.info(getUserName() + \登录失败,用户名或者密码不正确\ this.setRequestAttribute(\登录失败,用户名或者密码不正确\

return INPUT; } } }

package com.base.util;

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.util.Random;

import javax.imageio.ImageIO;

import javax.imageio.stream.ImageOutputStream; /**

* 验证码类,主要生成几种不同类型的验证码 * 第一种:简单验证码,4位随机数字 * 第二种:英文字符加数字的验证码

* 第三种:像铁路订票系统一样的验证码,肆+?=21 *

* @author 李朋飞

* */

public class VerificationCodeUtil {

private ByteArrayInputStream image;// 图像 private String str;// 验证码

private static final int WIDTH = 80; private static final int HEIGHT = 20;

public static void main(String[] arg) {

VerificationCodeUtil vcu = VerificationCodeUtil.Instance(); System.err.println(vcu.getVerificationCodeValue()); }

/**

* 功能:获取一个验证码类的实例

*

* @return */

public static VerificationCodeUtil Instance() { return new VerificationCodeUtil(); }

private VerificationCodeUtil() {

BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);

int randomNum = new Random().nextInt(3); if (randomNum == 0) {

initNumVerificationCode(image); } else if (randomNum == 1) {

initLetterAndNumVerificationCode(image); } else {

initChinesePlusNumVerificationCode(image); } }

/**

* 功能:设置第一种验证码的属性

*/

public void initNumVerificationCode(BufferedImage image) {

Random random = new Random(); // 生成随机类 Graphics g = initImage(image, random); String sRand = \

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

String rand = String.valueOf(random.nextInt(10)); sRand += rand;

// 将认证码显示到图象中

g.setColor(new Color(20 + random.nextInt(110), 20 + random .nextInt(110), 20 + random.nextInt(110)));

// 调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成 g.drawString(rand, 13 * i + 6, 16); }

this.setStr(sRand);/* 赋值验证码 */ // 图象生效

g.dispose();

this.setImage(drawImage(image)); }

/**

* 功能:设置第二种验证码属性

*/

public void initLetterAndNumVerificationCode(BufferedImage image) {

Random random = new Random(); // 生成随机类

Graphics g = initImage(image, random);

String[] letter = { \ \ \ String sRand = \

for (int i = 0; i < 4; i++) { String tempRand = \

if (random.nextBoolean()) {

tempRand = String.valueOf(random.nextInt(10)); } else {

tempRand = letter[random.nextInt(25)];

if (random.nextBoolean()) {// 随机将该字母变成小写

tempRand = tempRand.toLowerCase(); } }

sRand += tempRand;

g.setColor(new Color(20 + random.nextInt(10), 20 + random .nextInt(110), 20 + random.nextInt(110))); g.drawString(tempRand, 13 * i + 6, 16); }

this.setStr(sRand);/* 赋值验证码 */ g.dispose(); // 图象生效

this.setImage(drawImage(image)); }

/**

* 功能:设置第三种验证码属性即:肆+?=24

*/

public void initChinesePlusNumVerificationCode(BufferedImage image) { String[] cn = { \壹\贰\叁\肆\伍\陆\柒\捌\玖\拾\ Random random = new Random(); // 生成随机类 Graphics g = initImage(image, random); int x = random.nextInt(10) + 1; int y = random.nextInt(30); this.setStr(String.valueOf(y));

g.setFont(new Font(\楷体\设定字体 g.setColor(new random.nextInt(110),

Color(20

+

random.nextInt(10),

20

+

20 + random.nextInt(110))); g.drawString(cn[x - 1], 1 * 1 + 6, 16); g.drawString(\ g.drawString(\?\

g.drawString(\

g.drawString(String.valueOf(x + y), 61, 16); g.dispose(); // 图象生效

this.setImage(drawImage(image));

}

public Graphics initImage(BufferedImage image, Random random) { Graphics g = image.getGraphics(); // 获取图形上下文 g.setColor(getRandColor(200, 250));// 设定背景色

g.fillRect(0, 0, WIDTH, HEIGHT);

g.setFont(new Font(\设定字体 g.setColor(getRandColor(160, 200)); // 随机产生165条干扰线,使图象中的认证码不易被其它程序探测到

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

int x = random.nextInt(WIDTH); int y = random.nextInt(HEIGHT); int xl = random.nextInt(12); int yl = random.nextInt(12); g.drawLine(x, y, x + xl, y + yl); }

return g; }

public ByteArrayInputStream drawImage(BufferedImage image) { ByteArrayInputStream input = null;

ByteArrayOutputStream output = new ByteArrayOutputStream(); try {

ImageOutputStream imageOut = ImageIO .createImageOutputStream(output); ImageIO.write(image, \ imageOut.close();

input = new ByteArrayInputStream(output.toByteArray()); } catch (Exception e) {

System.out.println(\验证码图片产生出现错误:\ }

return input; }

/*

搜索更多关于: 基于SSH2框架的登录验证码的实现 的文档
基于SSH2框架的登录验证码的实现.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c5gqlp9uat141z4g1ryzk_3.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top