内容导航:@param width 图片的宽 * @param height 图片的高 * @param code 验证码字符串 * @return 返回图片验证码,int width= height=,@param width 图片的宽 * @param height 图片的高 * @param code 验证码字符串 * @param lineCnt,验证码为code的图片
  • 本人初学Java有什么好的图形验证码推荐吗
  • 怎样用java实现验证码
  • J2EE如何生成验证码图片和点击刷新验证码htmlcssWEBITnose百度知
  • Java如何实现验证码验证功能
  • {image}

    一、本人初学Java有什么好的图形验证码推荐吗

    如果你历蔽正在学习Java,可以使用Java的一些开源库来生成图形验证码。以下是一些常用的Java图形验证码库:

    • Kaptcha:Kaptcha是一个Java验证码库 ,可以生成随机的验证码图片,包括数字 、字母、汉字等。Kaptcha支持自定义验证码图片大小、颜色 、字体、干扰线等参数,并且支持多种图片格式 ,如JPEG、哪烂枯PNG等 。

    • Jcaptcha:Jcaptcha是一个Java验证码库,可以生成随机的验证码图片,包括数字 、字母、符号等。Jcaptcha支持自定义验证码图片大小、颜色 、字体、干扰线等参数 ,并且支持多种图片格式,如JPEG、PNG等。

    • SimpleCaptcha:SimpleCaptcha是一个简单的Java验证码库,可以生成随机的验证码图片 ,包括数字 、字母 、符号等 。SimpleCaptcha支持自定义验证码图片大小、颜色、字体 、干扰线等参数,并且支持多种图片格式,如JPEG、PNG等。

    以上是一些常用的Java图形验证李洞码库 ,它们都比较简单易用 ,适合初学者使用。你可以根据自己的需求选择一个适合自己的库来生成验证码 。

    二、怎样用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();

    三 、J2EE如何生成验证码图片和点击刷新验证码htmlcssWEBITnose百度知

    验证码图片生成步骤创建BufferedImage对象。 获取BufferedImage的画笔,即调用getGraphics()方法获取Graphics对象。 调用Graphics对象的setColor()方法和fillRect()方法设置图片背景颜色 。 调用Graphics对象的setColor()方法和drawLine()方法设置图片干扰线。 调用BufferedImaged对象的setRGB()方法设置图片的噪点。 调用掘物Graphics对象的setColor()方法、setFont()方法和drawString()方法设置图片验证码 。因为验证码的图片的宽度和高度要根据网站的风格来确定的,所以字体的大小需要根据图片的宽度和高度来确定 ,用到了小小的技巧 。
    package util;import ;import ;import ;import ;import mage;import OutputStream;import on;import ;import ;public class Verification { private static final String ALPHABET = ""; / * 生成一个宽为width, 高为height, 验证码为code的图片 * @param width 图片的宽 * @param height 图片的高 * @param code 验证码字符串 * @return 返回图片验证码 */ public static BufferedImage getImage(int width, int height, String code){ return getImage(width, height, code, 20); } / * 生成一个宽为width , 高为height, 验证码为code的图片,图片中干扰线的条数为lineCnt * @param width 图片的宽 * @param height 图片的高 * @param code 验证码字符串 * @param lineCnt 干扰线的条数 ,建议为10条左右,可根据结果适当调整 * @return 返回图片验证码 / public static BufferedImage getImage(int width, int height, String code, int lineCnt){ return createImage(width, height, code, lineCnt, 0.01); } / * 生成一个宽为width, 高为height , 验证码为code的图片,图片中干扰线的条数为lineCnt * 噪声比为noiseRate,即图片中噪音像素点的百分比 * @param width 图片的宽 * @param height 图片的高 * @param code 验李散段证码字符串 * @param lineCnt 干扰线的条数 ,建议为10条左右 ,可根据结果适当调整 * @param noiseRate 图片中噪音像素点占总像素的百分比 * @return 返回图片验证码 / public static BufferedImage getImage(int width, int height, String code, int lineCnt, double noiseRate){ return createImage(width, height, code, lineCnt, noiseRate); } / * 生成一个宽为width, 高为height, 验证码为code的图片哪誉 ,图片中干扰线的条数为lineCnt * 噪声比为noiseRate,即图片中噪音像素点的百分比 * @param width 图片的宽 * @param height 图片的高 * @param code 验证码字符串 * @param lineCnt 干扰线的条数,建议为10条左右 ,可根据结果适当调整 * @param noiseRate 图片中噪音像素点占总像素的百分比 * @return 返回图片验证码 / private static BufferedImage createImage(int width, int height, String code, int lineCnt, double noiseRate){ int fontWidth = ((int)(width * 0.8)) / (); int fontHeight = (int)(height * 0.7); //为了在任意的width和height下都能生成良好的验证码, //字体的大小为fontWdith何fontHeight中的小者, int fontSize = (fontWidth, fontHeight); //drawString时要用到 int paddingX = (int) (width * 0.1); int paddingY = height - (height - fontSize) / 2; //创建图像 BufferedImage buffimg = new BufferedImage(width, height, _INT_RGB); //获得画笔 Graphics g = cs(); //设置画笔的颜色 (getRandColor(200, 255)); //然后填充一个矩形 ,即设置背景色 (0, 0, width, height); // 设置干扰线 for (int i = 0; i < lineCnt; i++) { //随机获取干扰线的起点和终点 int xs = (int)(() * width); int ys = (int)(() * height); int xe = (int)(() * width); int ye = (int)(() * height); (getRandColor(1, 255)); (xs, ys, xe, ye); } // 添加噪点 int area = (int) (noiseRate * width * height); for(int i=0; i 255) L = 255; if(R > 255) R = 255; if(L < 0) L = 0; if(R < 0) R = 0; int interval = R - L; int r = L + (int)(() * interval); int g = L + (int)(() * interval); int b = L + (int)(() * interval); return new Color(r, g, b); } / * 随机生成若干个由大小写字母和数字组成的字符串 * @param len 随机生成len个字符 * @return 返回随机生成的若干个由大小写字母和数字组成的字符串 / public static String getRandCode(int len){ String code = ""; for(int i=0; i
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = n(); //随机生成字符串,并写入session String code = de(4); ute("verification", code); BufferedImage image = (100,30, code, 5); tType("image/png"); OutputStream out = Stream(); (ray(image)); (); (); } 在中设置验证码,用户点击验证码时 ,调用js代码请求服务器得到新的验证码。因为上面的那个生成验证码的servlet会被浏览器缓存,所以js代码中需要给该servlet一个随机的参数,这样浏览器就会向服务器发请求得到新的验证码 ,而不是去缓存中读取。
    Insert title here 最后是在这个servlet中判断用户输入的验证码是否正确 ,为了方便用户,验证码一般都设置成大小写不敏感,所以要先转化为小写字母再比对 。
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = n(); String verification = (String)ute("verification"); String submitVerification = ter("submitVerification"); PrintWriter out = (); if(verification!=null && submitVerification!=null){ if(se().equals(se())){ ("yes!!!"); } else{ ("no!!!"); } } else{ ("no!!!"); } ribute("verification");//防止用户重复提交表单 } /
    * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) / protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } 最后运行的效果图如下
    该文章在我的个人博客地址是: /

    四、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=" + ();

    • }

      • }

    如回答不详细可追问

    本文版权归趣快排www.sEoguruBlog.com 所有,如有转发请注明来出,竞价开户托管,seo优化请联系QQ✈61910465