JavaWeb 使用Session实现一次性验证码
表单
<form action="loginServlet" method="post">
请输入验证码:<input type="text" name="code" />
<img src="getCodeServlet" /><br />
<button type="submit">提交</button>
</form>
载入页面时,会自动请求getCodeServlet,获取图片(验证码)。
getCodeServlet,产生验证码
@WebServlet("/getCodeServlet")
public class GetCodeServlet extends HttpServlet {
//验证码的宽、高
private static int WIDTH=80;
private static int HEIGHT=25;
//绘制背景
private void drawBg(Graphics g){
//rgb
g.setColor(new Color(128, 128, 128));
//绘制矩形。x,y,wigth,height
g.fillRect(0,0,WIDTH,HEIGHT);
//随机绘制100个干扰点
Random random=new Random();
for (int i=0;i<100;i++){
//产生(0,1)上的小数,*WIDTH|HEIGHT,再取整也行
int x=random.nextInt(WIDTH);
int y=random.nextInt(HEIGHT);
g.drawOval(x,y,1,1);
//干扰点的颜色也可以随机,随机产生red,green,blue即可
//g.setColor(new Color(red,green,blue));
}
}
//绘制验证码
private void drawCode(Graphics g,char[] code){
g.setColor(Color.BLACK);
//字体、样式(多个时竖线分隔)、字号
g.setFont(new Font("serif",Font.ITALIC|Font.BOLD,18));
//在不同位置绘制验证码字符,参数:要绘制的String、横、纵坐标。+""是为了char转String。
g.drawString(code[0]+"",1,17);
g.drawString(code[1]+"",16,15);
g.drawString(code[2]+"",31,18);
g.drawString(code[3]+"",46,16);
}
//随机产生4位验证码
private char[] getCode(){
String chars="0123456789QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm";
char[] code=new char[4];
Random random=new Random();
for (int i=0;i<4;i++){
//[0,62)
int index= random.nextInt(62);
code[i]=chars.charAt(index);
}
return code;
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
HttpSession session = request.getSession();
ServletOutputStream sos = response.getOutputStream();
response.setContentType("image/jpeg");
//设置浏览器不缓存此图片
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires",0);
//创建内存图片
BufferedImage bufferedImage = new BufferedImage(WIDTH, HEIGHT, TYPE_INT_RGB);
Graphics g= bufferedImage.getGraphics();
char[] code=getCode();
//将验证码放到session域中。session对象要在提交响应之前获得
session.setAttribute("code",new String(code));
drawBg(g);
drawCode(g,code);
g.dispose();
//将图片输出到浏览器
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bufferedImage,"JPEG",baos);
baos.writeTo(sos);
baos.close();
sos.close();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
doPost(request,response);
}
}
loginServlet,处理表单
@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
HttpSession session = request.getSession();
String trueCode= (String) session.getAttribute("code");
String code=request.getParameter("code");
if (code.equals(trueCode)){
response.getWriter().write("验证码正确");
}
else {
response.getWriter().write("验证码错误");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}
上面的处理方式要区分验证码的大小写。
不区分大小写:
//先转换为全大写|全小写,再判断
trueCode=trueCode.toLowerCase();
code=code.toLowerCase(); //trueCode=trueCode.toUpperCase();
//code=trueCode.toUpperCase();
JavaWeb 使用Session实现一次性验证码的更多相关文章
- 使用session实现一次性验证码
在登录页面和各种页面,会看到有验证码输入,这样做的目的是为了防止密码猜测工具破解密码,保护了用户密码安全,验证码只能使用一次,这样就给密码猜测工具带来了很大的困难,基本上阻断了密码猜测工具的使用. 可 ...
- 简单的Session案例 —— 一次性验证码
一次性验证码的主要目的就是为了限制人们利用工具软件来暴力猜测密码,其原理与利用Session防止表单重复提交的原理基本一样,只是将表单标识号变成了验证码的形式,并且要求用户将提示的验证码手工填写进一个 ...
- Java Web(四) 一次性验证码的代码实现
其实实现代码的逻辑非常简单,真的超级超级简单. 1.在登录页面上login.jsp将验证码图片使用标签<img src="xxx">将绘制验证码图片的url给它 2.在 ...
- JavaWeb开发之普通图片验证码生成技术与算术表达式验证码生成技术
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6134649.html 另:算术验证码生成的JSP.Servlet实现均已移植github:https:/ ...
- javaweb(九)—— 通过Servlet生成验证码图片
一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedImage类,如下:
- javaweb基础(9)_Servlet生成验证码图片
一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedImage类,如下:
- Session中短信验证码设置有效时间
Session中短信验证码设置有效时间 package com.mozq.boot.kuayu01.controller; import org.springframework.web.bind.an ...
- web开发(四) 一次性验证码的代码实现
在网上看见一篇不错的文章,写的详细. 以下内容引用那篇博文.转载于<http://www.cnblogs.com/whgk/p/6426072.html>,在此仅供学习参考之用. 其实实现 ...
- javaweb笔记09—(session会话及验证码问题)
第一部分+++++++++++1.session会话 定义:session会话——对某个web应用程序的一次整体访问的过程. 由来:无连接的http协议是无状态的,不能保存每个客户端私有信息 a用户和 ...
随机推荐
- 【转载】Innodb中的事务隔离级别和锁的关系
前言 我们都知道事务的几种性质,数据库为了维护这些性质,尤其是一致性和隔离性,一般使用加锁这种方式.同时数据库又是个高并发的应用,同一时间会有大量的并发访问,如果加锁过度,会极大的降低并发处理能力.所 ...
- 卷积,reLu,池化的意义
1.卷积 提取局部特征 2.Relu 留下相关特征,去掉不相关特征,卷积之后的正值越大,说明与卷积核相关性越强,负值越大,不相关性越大. 3.池化 池化的目的: (1)留下最相关的特征,或者说留下最明 ...
- [BZOJ1864][CODEVS2462]三色二叉树
题目描述 Description 一棵二叉树可以按照如下规则表示成一个由0.1.2组成的字符序列,我们称之为“二叉树序列S”: |-0 表示该树没有子节点 S = |-1S1 表示该树有一个子节点, ...
- Apex 中插入更新数据的事件执行顺序
在使用 Apex 代码插入或更新数据的时候,若干事件会被按顺序执行.了解这些顺序可以提高调试程序的效率,也可以避免不必要的错误. 可以参考官方文档. 事件的执行顺序 从数据库中读取要更新的数据记录或初 ...
- <LinkedList> 160
160. Intersection of Two Linked Lists 分别从AB循环两次.如果第一次没循环到,第二次就会在节点相遇. public class Solution { public ...
- zookeeper图形化的客户端工具(ZooInspector)
1.ZooInspector下载地址 https://issues.apache.org/jira/secure/attachment/12436620/ZooInspector.zip 2.解压压缩 ...
- [LeetCode] 152. Maximum Product Subarray 求最大子数组乘积
Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...
- 从GopherChina 2019看当前的go语言
GopherChina 2019大会4月底刚刚结束,大会上使用的PPT也放了出来(大会情况及PPT在https://mp.weixin.qq.com/s/_oVpIcBMVIKVzQn6YrkAJw) ...
- asp.net core ModelState 模型状态验证扩展类
using DMS.Common.BaseResult; using Microsoft.AspNetCore.Mvc.ModelBinding; using System; using System ...
- ping不通服务器的解决方法
参考腾讯云的解决办法: https://cloud.tencent.com/document/product/213/14639#CheckOSSetting 我的服务器是aws的, 解决方法大同小异 ...