项目案例 || 注册中验证码实现

今天,给大家介绍验证码的相关配置问题。

众所周知,对传统验证码而言,便捷和安全长期以来一直是鱼与熊掌的关系。

这可从12306与黄牛的博弈中看出端倪。

最先开始,只是简单的数字英文组合,再后来有了加减法,相信一般人也可以应付。

直到出现一闪一闪的动态验证码和变形字母,第一次体会到眼睛都快看瞎了,还有输不对的情况~

但这些与后来的图片验证码相比,还只是小儿科,不信你看:

网友戏称:不懂娱乐圈,没资格买票回家。

验证码相关配置

不过对于我们一般小型系统而言,那么复杂的验证码确实没必要,简单的英文数字组合肯定够了。

今天就给大家介绍如何配置英文数字组合验证码。首先我们看效果图:

配置比较简单,首先在JSP界面引入验证码

<div class="form-group">

<label for="yanzheng" class="col-sm-2 control-label">验证码</label>

<div class="col-sm-6">

  <input name="inputCode" value=""/>  

  <img border="0" src="${pageContext.request.contextPath }/checkCode" />

     <input type="button" value="换一张" onclick="history.go(0) "/>

</div>

</div>

然后引入工具类CheckCode


public class CheckCodeServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws                    ServletException, IOException {

doPost(request,response);

}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws                             ServletException, IOException {

   response.setContentType("image/jpeg");

   HttpSession session = request.getSession();

   int width = 60;

   int height = 20;

   //设置浏览器不要缓存此图片

   response.setHeader("Pragma", "No-cache");

   response.setHeader("Cache-Control", "no-cache");

   response.setDateHeader("Expires", 0);

   //创建内存图像并获得其图形上下文

   BufferedImage image = new BufferedImage(width,                                   height,BufferedImage.TYPE_INT_RGB);

   Graphics g = image.getGraphics();

    // 产生随机验证码

    //定义验证码的字符表

   String chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

   char[] rands = new char[4];

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

      int rand = (int) (Math.random() * 36);

      rands[i] = chars.charAt(rand);

   }

    // 产生图像

   //画背景

   g.setColor(new Color(0xDCDCDC));

   g.fillRect(0, 0, width, height);

   //随机产生120个干扰点

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

      int x = (int)(Math.random() * width);

      int y = (int)(Math.random() * height);

      int red = (int)(Math.random() * 255);

      int green = (int)(Math.random() * 255);

      int blue = (int)(Math.random() * 255);

      g.setColor(new Color(red, green, blue));

      g.drawOval(x, y, 1, 0);

   }

   g.setColor(Color.BLACK);

   g.setFont(new Font(null, Font.ITALIC|Font.BOLD, 18));

   //在不同的高度上输出验证码的不同字符

   g.drawString("" + rands[0], 1, 17);

   g.drawString("" + rands[1], 16, 15);

   g.drawString("" + rands[2], 31, 18);

   g.drawString("" + rands[3], 46, 16);

   g.dispose();

   //将图像输出到客户端

   ServletOutputStream sos = response.getOutputStream();

   ByteArrayOutputStream baos = new ByteArrayOutputStream();

   ImageIO.write(image, "JPEG", baos);

   byte[] buffer = baos.toByteArray();

   response.setContentLength(buffer.length);

   sos.write(buffer);

   baos.close();

   sos.close();

   //将验证码放到 session 中

   session.setAttribute("checkCode", new String(rands));

}

}

 验证码放到session中后,在Controller中获取session,然后与前台输入对比


 String sc=(String)session.getAttribute("checkCode");

 if(sc.equals(inputCode)){

   //对比验证码即可

 }

以上就是验证码的全部代码实现。如果你有任何问题,欢迎留言,我们共同交流讨论。

还可以微信关注和置顶我的公众号“SL社区”(slshequ),获取源码资源及众多项目案例。

猜你喜欢

转载自blog.csdn.net/wangjunjun_0826/article/details/84971381