内容导航:可以生成随机的验证码图片,int width= height=,* final int y = (height),* final int x = (width)
{image}
一、Java如何实现验证码验证功能
Java如何实现验证码验证功能呢?日常生活中 ,验证码随处可见 ,他可以在一定程度上保护账号安全,那么他是怎么实现的呢?
Java实现验证码验证功能其实非常简单:用到了一个Graphics类在画板上绘制字母,随机选取一定数量的字母随机生成 ,然后在画板上随机生成几条干扰线。
首先,写一个验证码生成帮助类,用来绘制随机字母:
-
import ;
-
import 纯丛橘;
-
import ;
-
import mage;
-
import on;
-
import eam;
-
import ;
-
- import ;
-
- public final class GraphicHelper {
-
- /**
-
- 以字符串形式返回生成的验证码 ,同时输出一个图片
-
-
- @param width
-
- 图片的宽度
-
- @param height
-
- 图片的高度
-
- @param imgType
-
- 图片的类型
-
- @param output
-
- 做团 图片的输出流(图片将输出到这个流中)
-
- @return 返回所生成的验证码(字符串)
-
*/
-
public static String create(final int width, final int height, final String imgType, OutputStream output) {
-
StringBuffer sb = new StringBuffer();
-
Random random = new Random();
-
- BufferedImage image = new BufferedImage(width, height, _INT_RGB);
-
Graphics graphic = cs();
-
- (("F8F8F8"));
-
(0, 0, width, height);
-
- Color[] colors = new Color[] { , , , , , ,
-
};
-
// 在 "画板"上生成干扰线条 ( 50 是线条个数)
-
for (int i = 0; i < 50; i++) {
-
(colors[()]);
-
final int x = (width);
-
final int y = (height);
-
final int w = (20);
-
final int h = (20);
-
final int signA = an() ? 1 : -1;
-
final int signB = an() ? 1 : -1;
-
(x, y, x + w * signA, y + h * signB);
-
}
-
- // 在 "画板"上绘制字母
-
(new Font("Comic Sans MS", , 30));
-
for (int i = 0; i < 6; i++) {
-
final int temp = (26) + 97;
-
String s = ((char) temp);
-
(s);
-
(colors[()]);
-
g(s, i * (width / 6), height - (height / 3));
-
}
-
();
-
try {
-
(image, imgType, output);
-
} catch (IOException e) {
-
kTrace();
-
}
-
return ();
-
}
-
- }
接着,创建一个servlet,用来固定图片大小 ,以及处理验证码的使用场景,以及郑派捕获页面生成的验证码(捕获到的二维码与用户输入的验证码一致才能通过) 。
-
import eam;
-
- import ception;
-
import t;
-
import et;
-
import etRequest;
-
import etResponse;
-
import on;
-
- @WebServlet(urlPatterns = "/verify/" )
-
public class VerifyCodeServlet extends HttpServlet {
-
- private static final long serialVersionUID = L;
-
- @Override
-
protected void service(HttpServletRequest request, HttpServletResponse response)
-
throws ServletException, IOException {
-
- // 获得 当前请求 对应的 会话对象
-
HttpSession session = n();
-
- // 从请求中获得 URI ( 统一资源标识符 )
-
String uri = tURI();
-
("hello : " + uri);
-
- final int width = 180; // 图片宽度
-
final int height = 40; // 图片高度
-
final String imgType = "jpeg"; // 指定图片格式 (不是指MIME类型)
-
final OutputStream output = Stream(); // 获得可以向客户端返回图片的输出流
-
// (字节流)
-
// 创建验证码图片并返回图片上的字符串
-
String code = (width, height, imgType, output);
-
("验证码内容: " + code);
-
- // 建立 uri 和 相应的 验证码 的关联 ( 存储到当前会话对象的属性中 )
-
ute(uri, code);
-
- (ute(uri));
-
- }
-
- }
接着写一个HTML注册页面用来检验一下:
-
注册
效果如下图:
在控制台接收到的图片中验证码的变化如下:
当点击刷新页面的时候,验证码也会随着变化 ,但我们看不清验证码时,只要点击验证码就会刷新,这样局部的刷新可以用JavaScript来实现 。
在src="/demo/verify/">中 ,添加一个问号和一串后缀数字 ,当刷新时让后缀数字不断改变,那么形成的验证码也会不断变化,我们可以采用的一种办法是后缀数字用date代替 ,date获取本机时间,时间是随时变的,这样就保证了刷新验证码可以随时变化。
代码如下:
-
function myRefersh( e ) {
-
- const source = ; // 获得原来的 src 中的内容
-
//( "source : " + source ) ;
-
- var index = ( "?" ) ; // 从 source 中寻找 ? 第一次出现的位置 (如果不存在则返回 -1 )
-
//( "index : " + index ) ;
-
- if( index > -1 ) { // 如果找到了 ? 就进入内部
-
var s = ( 0 , index ) ; // 从 source 中截取 index 之前的内容 ( index 以及 index 之后的内容都被舍弃 )
-
//( "s : " + s ) ;
-
- var date = new Date(); // 创建一个 Date 对象的 一个 实例
-
var time = () ; // 从 新创建的 Date 对象的实例中获得该时间对应毫秒值
-
= s + "?time=" + time ; // 将 加了 尾巴 的 地址 重新放入到 src 上
-
- //( ) ;
-
} else {
-
var date = new Date();
-
= source + "?time=" + ();
-
}
-
- }
如回答不详细可追问
二 、JAVA识别图片验证码
package ;
import .;
import .;
import .;
import .;
import .;
import .;
import .*;
public class CodeFact
extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws
ServletException, IOException {
//设置页面不缓存
("Pragma", "No-cache");
("Cache-Control", "no-cache");
ader("Expires", 0);
// 在内存中创建图象
int width = 60, height = 20;
BufferedImage image = new BufferedImage(width, height,
_INT_RGB);
// 获取图形上下文
Graphics g = cs();
//生成随机类
Random random = new Random();
// 设定背景色
(getRandColor(200, 250));
(0, 0, width, height);
//设定字体
(new Font("Times New Roman", , 18));
//画边框
(new Color(33,66,99));
(0,0,width-1,height-1);
// 随机产生155条干扰线 ,使图象中的认证码不易被其它程序探测到
(getRandColor(160, 200));
for (int i = 0; i < 155; i++) {
int x = (width);
int y = (height);
int xl = (12);
int yl = (12);
(x, y, x + xl, y + yl);
}
// 取随机产生的认证码(4位数字)
String sRand = "";
for (int i = 0; i < 4; i++) {
String rand = ((10));
sRand += rand;
// 将认证码显示到图象中
(new Color(20 + (110), 20 + (110),
20 + (110))); //调用函数出来的颜色相同,可能是因为种子太接近,所以只能闹亩谨直接生成
g(rand, 13 * i + 6, 16);
}
// 将认证耐逗码存液基入SESSION
HttpSession session = n();
ute("rand", sRand);
// 图象生效
();
// 输出图象到页面
(image, "JPEG", Stream());
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws
ServletException, IOException {
doGet(request, response);
}
//给定范围获得随机颜色
private Color getRandColor(int fc, int bc) {
Random random = new Random();
if (fc > 255) {
fc = 255;
}
if (bc > 255) {
bc = 255;
}
int r = fc + (bc - fc);
int g = fc + (bc - fc);
int b = fc + (bc - fc);
return new Color(r, g, b);
}
}
你试试!!
三 、怎样用java实现验证码
现在许多系统的注册 登录或者发布信息模块都添加的随机验证码功能 就是为了避免自动注册程序或者自动发布程序的使用
验证码实际上就是随机选择一些字符以图片的形式展现在页面上 如果进行提交操作的同时需要将图片上的字符同时提交 如果提交的字符与服务器session保存的不同 则认为提交基数信息无效 为了避免自动程序分析解析图片 通常会在图片上随机生成一些干扰线或者将字符进行扭曲 增加自动识别验证码的难度
在这里 我们使用java实现验证码
<%@ page contentType= image/jpeg import= java awt * java awt image * java util * javax imageio * %>
<%!
Color getRandColor(int fc int bc){//给定范围获得随机颜色
Random random = new Random();
if(fc> ) fc= ;
租锋做if(bc> ) bc= ;
int r=fc+random nextInt(bc fc);
int g=fc+random nextInt(bc fc);
int b=fc+random nextInt(bc fc);
return new Color(r g b);
}
%>
<%
//设置页面不缓存
response setHeader( Pragma No cache );
弊衡response setHeader( Cache Control no cache );
response setDateHeader( Expires );
// 在内存中创建图象
int width= height= ;
BufferedImage image = new BufferedImage(width height BufferedImage TYPE_INT_RGB);
// 获取图形上下文
Graphics g = image getGraphics();
//生成随机类
Random random = new Random();
// 设定背景色
g setColor(getRandColor( ));
g fillRect( width height);
//设定字体
g setFont(new Font( Times New Roman Font PLAIN ));
// 随机产生 条干扰线 使图象中的认证码不易被其它程序探测到
g setColor(getRandColor( ));
for (int i= ;i< ;i++)
{
int x = random nextInt(width);
int y = random nextInt(height);
int xl = random nextInt( );
int yl = random nextInt( );
g drawLine(x y x+xl y+yl);
}
// 取随机产生的认证码( 位数字)
String codeList = ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ;
String sRand= ;
for (int i= ;i< ;i++){
int a=random nextInt(codeList length() );
String rand=codeList substring(a a+ );
sRand+=rand;
// 将认证码显示到图象中
g setColor(new Color( +random nextInt( ) +random nextInt( ) +random nextInt( )));//调用函数出来的颜色相同 可能是因为种子太接近 所以只能直接生成
g drawString(rand *i+ );
}
// 将认证码存入SESSION
session setAttribute( rand sRand);
// 图象生效
g dispose();
// 输出图象到页面
ImageIO write(image JPEG response getOutputStream());
out clear();
out = pageContext pushBody();
四、本人初学Java有什么好的图形验证码推荐吗
如果你历蔽正在学习Java ,可以使用Java的一些开源库来生成图形验证码。以下是一些常用的Java图形验证码库:
-
Kaptcha:Kaptcha是一个Java验证码库,可以生成随机的验证码图片,包括数字、字母 、汉字等 。Kaptcha支持自定义验证码图片大小、颜色、字体 、干扰线等参数 ,并且支持多种图片格式,如JPEG、哪烂枯PNG等。
-
Jcaptcha:Jcaptcha是一个Java验证码库,可以生成随机的验证码图片 ,包括数字、字母 、符号等。Jcaptcha支持自定义验证码图片大小、颜色、字体 、干扰线等参数 ,并且支持多种图片格式,如JPEG、PNG等 。
-
SimpleCaptcha:SimpleCaptcha是一个简单的Java验证码库,可以生成随机的验证码图片 ,包括数字、字母 、符号等。SimpleCaptcha支持自定义验证码图片大小 、颜色、字体、干扰线等参数,并且支持多种图片格式,如JPEG 、PNG等。
以上是一些常用的Java图形验证李洞码库 ,它们都比较简单易用,适合初学者使用 。你可以根据自己的需求选择一个适合自己的库来生成验证码。