在登录页面和各种页面,会看到有验证码输入,这样做的目的是为了防止密码猜测工具破解密码,保护了用户密码安全,验证码只能使用一次,这样就给密码猜测工具带来了很大的困难,基本上阻断了密码猜测工具的使用。

可以使用session获得一次性验证码。先看一下登录页面,即显示验证码的页面,代码为:

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3. <head>
  4. <title>CheckCode.html</title>
  5. <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  6. </head>
  7. <body>
  8. <form action="/learnJS/servlet/LoginFormServlet" method="post">
  9. 用户名:<input type="text" name="name"/><br/>
  10. 密     码:<input type="password" name="pass"><br/>
  11. 验证码:<input type="text" name="check_code"/>
  12. <img src="/learnJS/servlet/CheckCodeServlet"/><br/>
  13. <input type="submit" name="登录"/>
  14. </form>
  15. </body>
  16. </html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>CheckCode.html</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head> <body>

<form action="/learnJS/servlet/LoginFormServlet" method="post">

用户名:<input type="text" name="name"/><br/>

密 码:<input type="password" name="pass"><br/>

验证码:<input type="text" name="check_code"/>

<img src="/learnJS/servlet/CheckCodeServlet"/><br/>

<input type="submit" name="登录"/>

</form>

</body>

</html>

验证码存放在一张图片上,那图片是通过servlet产生的,在servlet中先产生验证码存放到session中,供以后验证使用,然后在画一张图片,将验证码无规则的放在图片上,在图片上画上干扰字符,然后就可以啦。代码如下:

  1. package com.you.servlet;
  2. import java.awt.Color;
  3. import java.awt.Font;
  4. import java.awt.Graphics;
  5. import java.awt.image.BufferedImage;
  6. import java.io.ByteArrayOutputStream;
  7. import java.io.IOException;
  8. import javax.imageio.ImageIO;
  9. import javax.servlet.ServletException;
  10. import javax.servlet.ServletOutputStream;
  11. import javax.servlet.http.HttpServlet;
  12. import javax.servlet.http.HttpServletRequest;
  13. import javax.servlet.http.HttpServletResponse;
  14. import javax.servlet.http.HttpSession;
  15. public class CheckCodeServlet extends HttpServlet {
  16. private static int WIDTH = 60;
  17. private static int HEIGHT = 20;
  18. public void doGet(HttpServletRequest request, HttpServletResponse response)
  19. throws ServletException, IOException {
  20. HttpSession session = request.getSession();
  21. response.setContentType("image/jpeg");
  22. ServletOutputStream sos = response.getOutputStream();
  23. //设置浏览器不要缓存此图片
  24. response.setHeader("Pragma", "No-cache");
  25. response.setHeader("Cache-Control", "no-cache");
  26. response.setDateHeader("Expires", 0);
  27. //创建内存图像并获得其图形上下文
  28. BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
  29. Graphics g = image.getGraphics();
  30. //产生随机的验证码
  31. char[] rands = generateCheckCode();
  32. //产生图像
  33. drawBackground(g);
  34. drawRands(g, rands);
  35. //结束图像的绘制过程,完成图像
  36. g.dispose();
  37. //将图像输出到客户端
  38. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  39. ImageIO.write(image, "JPEG", bos);
  40. byte[] buf = bos.toByteArray();
  41. response.setContentLength(buf.length);
  42. sos.write(buf);
  43. bos.close();
  44. sos.close();
  45. //将当前验证码存入到session中
  46. session.setAttribute("check_code", new String(rands));
  47. }
  48. public void doPost(HttpServletRequest request, HttpServletResponse response)
  49. throws ServletException, IOException {
  50. doGet(request, response);
  51. }
  52. private char[] generateCheckCode() {
  53. //定义验证码的字符集
  54. String chars = "0123456789abcdefghigklmnopqrstuvwxyz";
  55. char[] rands = new char[4];
  56. for(int i = 0; i < 4; i++) {
  57. int rand = (int)(Math.random() * 36);
  58. rands[i] = chars.charAt(rand);
  59. }
  60. return rands;
  61. }
  62. private void drawRands(Graphics g, char[] rands) {
  63. g.setColor(Color.BLACK);
  64. g.setFont(new Font(null,Font.ITALIC|Font.BOLD,18));
  65. //在不同的高度上输出验证码的每个字符
  66. g.drawString("" + rands[0], 1, 17);
  67. g.drawString("" + rands[1], 16, 15);
  68. g.drawString("" + rands[2], 31, 18);
  69. g.drawString("" + rands[3], 46, 16);
  70. System.out.println(rands);
  71. }
  72. private void drawBackground(Graphics g) {
  73. //画背景
  74. g.setColor(new Color(0xDCDCDC));
  75. g.fillRect(0, 0, WIDTH, HEIGHT);
  76. //随机产生120个干扰点
  77. for(int i = 0; i < 120; i++) {
  78. int x = (int)(Math.random() * WIDTH);
  79. int y = (int)(Math.random() * HEIGHT);
  80. int red = (int)(Math.random() * 255);
  81. int green = (int)(Math.random() * 255);
  82. int blue = (int)(Math.random() * 255);
  83. g.setColor(new Color(red, green, blue));
  84. g.drawOval(x, y, 1, 0);
  85. }
  86. }
  87. }
package com.you.servlet;

import java.awt.Color;

import java.awt.Font;

import java.awt.Graphics;

import java.awt.image.BufferedImage;

import java.io.ByteArrayOutputStream;

import java.io.IOException; import javax.imageio.ImageIO;

import javax.servlet.ServletException;

import javax.servlet.ServletOutputStream;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession; public class CheckCodeServlet extends HttpServlet {

private static int WIDTH = 60;

private static int HEIGHT = 20;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
response.setContentType("image/jpeg");
ServletOutputStream sos = response.getOutputStream(); //设置浏览器不要缓存此图片
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(); //产生随机的验证码
char[] rands = generateCheckCode(); //产生图像
drawBackground(g);
drawRands(g, rands); //结束图像的绘制过程,完成图像
g.dispose(); //将图像输出到客户端
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ImageIO.write(image, "JPEG", bos);
byte[] buf = bos.toByteArray();
response.setContentLength(buf.length);
sos.write(buf);
bos.close();
sos.close(); //将当前验证码存入到session中
session.setAttribute("check_code", new String(rands)); } public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { doGet(request, response);
} private char[] generateCheckCode() {
//定义验证码的字符集
String chars = "0123456789abcdefghigklmnopqrstuvwxyz";
char[] rands = new char[4];
for(int i = 0; i &lt; 4; i++) {
int rand = (int)(Math.random() * 36);
rands[i] = chars.charAt(rand);
}
return rands;
} private void drawRands(Graphics g, char[] rands) {
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);
System.out.println(rands);
} private void drawBackground(Graphics g) {
//画背景
g.setColor(new Color(0xDCDCDC));
g.fillRect(0, 0, WIDTH, HEIGHT);
//随机产生120个干扰点
for(int i = 0; i &lt; 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);
}
}

}

然后是登录之后处理用户是否登录成功的servlet,在这个servlet中通过比较session中存放的验证码和用户输入的验证码,如果匹配则进行下一步判断,如果不匹配直接输出验证码不匹配的问题。代码为:

  1. package com.you.servlet;
  2. import java.io.IOException;
  3. import java.io.PrintWriter;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import javax.servlet.http.HttpSession;
  9. public class LoginFormServlet extends HttpServlet {
  10. public void doGet(HttpServletRequest request, HttpServletResponse response)
  11. throws ServletException, IOException {
  12. response.setContentType("text/html;charset=utf-8");
  13. PrintWriter out = response.getWriter();
  14. HttpSession session = request.getSession(false);
  15. if(session == null) {
  16. out.print("验证码处理问题");
  17. return;
  18. }
  19. String saveCode = (String)session.getAttribute("check_code");
  20. if(saveCode == null) {
  21. out.print("验证码处理问题");
  22. return;
  23. }
  24. String checkCode = request.getParameter("check_code");
  25. if(!saveCode.equals(checkCode)) {
  26. out.print("验证码无效!");
  27. return;
  28. }
  29. session.removeAttribute("check_code");
  30. out.print("验证码通过,服务器正在校验用户名和密码!");
  31. }
  32. public void doPost(HttpServletRequest request, HttpServletResponse response)
  33. throws ServletException, IOException {
  34. doGet(request, response);
  35. }
  36. }
package com.you.servlet;

import java.io.IOException;

import java.io.PrintWriter; import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession; public class LoginFormServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter(); HttpSession session = request.getSession(false);
if(session == null) {
out.print("验证码处理问题");
return;
} String saveCode = (String)session.getAttribute("check_code");
if(saveCode == null) {
out.print("验证码处理问题");
return;
} String checkCode = request.getParameter("check_code");
if(!saveCode.equals(checkCode)) {
out.print("验证码无效!");
return;
} session.removeAttribute("check_code");
out.print("验证码通过,服务器正在校验用户名和密码!"); } public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { doGet(request, response);
}

}

使用session实现一次性验证码的更多相关文章

  1. 简单的Session案例 —— 一次性验证码

    一次性验证码的主要目的就是为了限制人们利用工具软件来暴力猜测密码,其原理与利用Session防止表单重复提交的原理基本一样,只是将表单标识号变成了验证码的形式,并且要求用户将提示的验证码手工填写进一个 ...

  2. JavaWeb 使用Session实现一次性验证码

    表单 <form action="loginServlet" method="post"> 请输入验证码:<input type=" ...

  3. Java Web(四) 一次性验证码的代码实现

    其实实现代码的逻辑非常简单,真的超级超级简单. 1.在登录页面上login.jsp将验证码图片使用标签<img src="xxx">将绘制验证码图片的url给它 2.在 ...

  4. Session中短信验证码设置有效时间

    Session中短信验证码设置有效时间 package com.mozq.boot.kuayu01.controller; import org.springframework.web.bind.an ...

  5. web开发(四) 一次性验证码的代码实现

    在网上看见一篇不错的文章,写的详细. 以下内容引用那篇博文.转载于<http://www.cnblogs.com/whgk/p/6426072.html>,在此仅供学习参考之用. 其实实现 ...

  6. java生成一次性验证码

    1.编写生成验证码的工具类: import java.awt.BasicStroke;import java.awt.Color;import java.awt.Font;import java.aw ...

  7. javaweb笔记09—(session会话及验证码问题)

    第一部分+++++++++++1.session会话 定义:session会话——对某个web应用程序的一次整体访问的过程. 由来:无连接的http协议是无状态的,不能保存每个客户端私有信息 a用户和 ...

  8. session的应用----验证码

    昨天登录功能中叙述了密码 用户名的数据库验证以及转发 那么这篇文章在昨天的基础上 处理验证码的验证功能,今天需要用到session域,session用于一次会话. package cn.lijun.d ...

  9. 关于用户禁用Cookie的解决办法和Session的图片验证码应用

    当用户通过客户端浏览页面初始化了Session之后(如:添加购物车,用户登陆等),服务器会将这些session数据保存在:Windows保存在C:\WINDOWS\Temp的目录下,Linux则是保存 ...

随机推荐

  1. JS 计算时间范围,最近一周、一个月

    //最近一周 getDay(-7) 返回的是距离当前日期的一周后的时间//一月 getDay(-30)//一年 getDay(-365) function getDay(day){ var today ...

  2. 左神算法进阶班4_2累加和为aim的最长子数组

    [题目] 给定一个数组arr,和一个整数aim,求在arr中,累加和等于num的最长子数组的长度 例子: arr = { 7,3,2,1,1,7,7,7 } aim = 7 其中有很多的子数组累加和等 ...

  3. gatekeeper学习概述

    1.概述 该产品部署在网络隔离装置两端,以代理程序的身份,完成两侧设备连接维护,数据转发的功能.场景简化如图所示: 软件核心是一个基于Netty的网络应用程序,考虑到系统的可维可测性,集成了web化的 ...

  4. Python学习day23-面向对象的编程

    figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...

  5. 04-python 学习第四点-装饰器

    装饰器就是一个函数,他是通过不修改某个函数的源代码和调用方式的前提下可以添加新功能的一种函数.在python 中装饰器一般采用高阶函数和嵌套函数达到装饰的作用,下面进行实例讲解: 1.目前有一个网址有 ...

  6. SQL ORM框架

    [LINQ]using (SqlConnection conn = new SqlConnection(conStr)) { string sql = $@"select * from vi ...

  7. Leetcode60. Permutation Sequence第k个排列

    给出集合 [1,2,3,-,n],其所有元素共有 n! 种排列. 按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下: "123" "132&qu ...

  8. [转]C#中的委托和事件(续)

    源码下载:http://www.tracefact.net/SourceCode/MoreDelegate.rar C#中的委托和事件(续) 引言 如果你看过了 C#中的委托和事件 一文,我想你对委托 ...

  9. 威胁快报|Solr dataimport成挖矿团伙新型利用方式

    概述 近日,阿里云安全团队监测到挖矿团伙利用solr dataimport RCE(CVE-2019-0193)作为新的攻击方式对云上主机进行攻击,攻击成功后下载门罗币挖矿程序进行牟利.该团伙使用的恶 ...

  10. HZOI2019序列

    题目链接:https://www.cnblogs.com/Juve/articles/11186805.html(密码是我的一个oj用户名) 题解: 这题我考试打的暴力,只有5分. 一开始理解错题意了 ...