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

可以使用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. 系列文章:云原生Kubernetes日志落地方案

    在Logging这块做了几年,最近1年来越来越多的同学来咨询如何为Kubernetes构建一个日志系统或者是来求助在这过程中遇到一系列问题如何解决,授人以鱼不如授人以渔,于是想把我们这些年积累的经验以 ...

  2. 回首2018 | 分析型数据库AnalyticDB: 不忘初心 砥砺前行

    题记 分析型数据库AnalyticDB(下文简称ADB),是阿里巴巴自主研发.唯一经过超大规模以及核心业务验证的PB级实时数据仓库.截止目前,现有外部支撑客户既包括传统的大中型企业和政府机构,也包括众 ...

  3. SpringCloud学习笔记(一):SpringCloudt相关面试题

    什么是微服务? 看笔记二 微服务之间是如何独立通讯的? 服务与服务间采⽤轻量级的通信机制互相协作(通常是基于HTTP协议的RESTful API) SpringCloud和Dubbo有什么区别? Du ...

  4. LinkedList集合 实现栈和队列

    LinkedList集合的底层是链表结构实现的,所以可以模拟栈(先进后出)和队列(先进先出). 方法: addFirst() //添加元素到列表的起始位置 addLast() //添加元素到列表的结束 ...

  5. java主函数参数传递args

    a.javaJava应用程序的主入口方法main(String[] args),表示该方法需要接收一个字符串数组类型的参数, 如果该参数不指定,agrs接收的是null. 程序:   public C ...

  6. Python学习day39-并发编程(各种锁)

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

  7. Win7下SQLServer访问虚拟机上的MySQL

    一.确保Win7能telnet通MySQL端口,防火墙设置可参考http://www.cnblogs.com/ShanFish/p/6519950.html二.配置系统DSN1.在Win7上安装MyS ...

  8. 搭建一个Semantic-ui项目

    一.进入到项目目录 npm init 二.安装semantic-ui npm install semantic-ui --save 三.编译输出semantic-ui cd  ./semantic g ...

  9. linux服务器之间传输文件

    转载:https://www.jb51.net/article/82608.htm 1. scp(最近就使用了scp) [优点]简单方便,安全可靠:支持限速参数 [缺点]不支持排除目录[用法]scp就 ...

  10. Ubuntu下samba的安装和配置

    samba是Linux系统上的一种文件共享协议,可以实现Windows系统访问Linux系统上的共享资源,现在介绍一下如何在Ubuntu 14.04上安装和配置samba一. 一.更新源列表 打开&q ...