当前位置: 移动技术网 > IT编程>开发语言>Java > javaweb登录验证码的实现方法

javaweb登录验证码的实现方法

2019年07月19日  | 移动技术网IT编程  | 我要评论

科苑星空,丝袜论坛,手机电影免费下

本文实例为大家分享了javaweb登录验证码的具体代码,供大家参考,具体内容如下

使用:

controller:生成验证码

@requestmapping("/user/check.jpg") 
  public void createcode(httpservletrequest request, httpservletresponse response) throws ioexception { 
    // 通知浏览器不要缓存 
    response.setheader("expires", "-1"); 
    response.setheader("cache-control", "no-cache"); 
    response.setheader("pragma", "-1"); 
    captchautil util = captchautil.instance(); 
    // 将验证码输入到session中,用来验证 
    string code = util.getstring(); 
    request.getsession().setattribute(“code”, code); 
    // 输出打web页面 
    imageio.write(util.getimage(), "jpg", response.getoutputstream()); 
  } 

jsp:显示验证码

<img id="img" src="<%=basepath%>user/check.jpg" onclick="refresh()"> 
function refresh() { 
  var url = $("#basepath").val() + "user/check.jpg?number="+math.random(); 
  $("#img").attr("src",url); 
} 

验证:

获取session中的code与前台传回的code是否一致

/** 
   * 验证码验证 
   * 
   * @param session 
   * @param code 
   */ 
  private void checkcode(httpsession session, string code) { 
    string codesession = (string) session.getattribute(“code”); 
    if (stringutils.isempty(codesession)) { 
      log.error("没有生成验证码信息"); 
      throw new illegalstateexception("err-01000"); 
    } 
    if (stringutils.isempty(code)) { 
      log.error("未填写验证码信息"); 
      throw new bussinessexception("err-06018"); 
    } 
    if (codesession.equalsignorecase(code)) { 
      // 验证码通过 
    } else { 
      log.error("验证码错误"); 
      throw new bussinessexception("err-06019"); 
    } 
  } 

工具类:

import java.awt.color; 
import java.awt.font; 
import java.awt.graphics; 
import java.awt.image.bufferedimage; 
import java.util.random; 
/** 
 * 验证码生成工具 
 * @author hxl 
 * 
 */ 
public class captchautil { 
  private bufferedimage image;// 图像 
  private string str;// 验证码 
  private static char code[] = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz123456789".tochararray(); 
 
  public static final string session_code_name="code"; 
   
  private captchautil() { 
    init();// 初始化属性 
  } 
 
  /* 
   * 取得randomnumutil实例 
   */ 
  public static captchautil instance() { 
    return new captchautil(); 
  } 
 
  /* 
   * 取得验证码图片 
   */ 
  public bufferedimage getimage() { 
    return this.image; 
  } 
 
  /* 
   * 取得图片的验证码 
   */ 
  public string getstring() { 
    return this.str; 
  } 
 
  private void init() { 
    // 在内存中创建图象 
    int width = 85, height = 20; 
    bufferedimage image = new bufferedimage(width, height, bufferedimage.type_int_rgb); 
    // 获取图形上下文 
    graphics g = image.getgraphics(); 
    // 生成随机类 
    random random = new random(); 
    // 设定背景色 
    g.setcolor(getrandcolor(200, 250)); 
    g.fillrect(0, 0, width, height); 
    // 设定字体 
    g.setfont(new font("times new roman", font.plain, 18)); 
    // 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到 
    g.setcolor(getrandcolor(160, 200)); 
    for (int i = 0; i < 155; 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); 
    } 
    // 取随机产生的认证码(4位数字) 
    string srand = ""; 
    for (int i = 0; i < 4; i++) { 
      string rand = string.valueof(code[random.nextint(code.length)]); 
      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.str = srand; 
 
    // 图象生效 
    g.dispose(); 
    // bytearrayinputstream input = null; 
    // bytearrayoutputstream output = new bytearrayoutputstream(); 
    // try { 
    // imageoutputstream imageout = imageio.createimageoutputstream(output); 
    // imageio.write(image, "jpeg", imageout); 
    // imageout.close(); 
    // input = new bytearrayinputstream(output.tobytearray()); 
    // } catch (exception e) { 
    // system.out.println("验证码图片产生出现错误:" + e.tostring()); 
    // } 
    // this.image = input 
    this.image = image;/* 赋值图像 */ 
  } 
 
  /* 
   * 给定范围获得随机颜色 
   */ 
  private color getrandcolor(int fc, int bc) { 
    random random = new random(); 
    if (fc > 255) 
      fc = 255; 
    if (bc > 255) 
      bc = 255; 
    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); 
  } 
} 

最后说明一点: 登录拦截器必须要放行生成验证码路径! 登录拦截器必须要放行生成验证码路径!! 登录拦截器必须要放行生成验证码路径!!!重要的事情说三遍~~

最后展示:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网