javaweb利用servlet实现图片验证码
验证码是登录验证的技术,可以一定程度防止恶意脚本刷网站,造成服务器压力。
具体实现思想很简单:
首先利用servlet产生一个随机的验证码,
保存在session中,
然后将它画作图片,输出到浏览器
在登录时对输入的验证码判断即可。
**注意事项:
1.由于struts2会过滤所有action,
在对servlet配置时,不能配置为与action类似的名称,需要为name.ssss;ssss可以是其他,不能是action.
2.对验证码图片的点击刷新,由于直接使用onclick事件对图片的src刷新,是行不通的,
因为浏览器会认为即使你刷新,他也只是将已经生成的图片再一次发送给你,看起来验证码是没有变得。
那么你就需要一个变化的参数跟着src后即可,例如随机数(不好,随后体验,在访问量一定程度后就无法了,因为重叠程度大,不一定会刷新),时间戳等,就好了
**
代码:
参生验证码图片的servlet
步骤
// 图片格式
// 设置浏览器不要缓存此图片
// 创建内存图像并获得图形上下文(BufferedImage)
// 画图片
//输出图片
{ServletOutputStream sos = resp.getOutputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, “JPEG”, baos);
byte[] buffer = baos.toByteArray();
resp.setContentLength(buffer.length);
sos.write(buffer);
baos.close();
sos.close();}
package servlet.checkcode;
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.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet(name = "code", value = "/code.servlet")
public class Code extends HttpServlet {
/**
* @author joker 验证码servlet
*/
private static final long serialVersionUID = -1139461793398415115L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO 自动生成的方法存根
this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO 自动生成的方法存根
HttpSession session=req.getSession(true);
resp.setContentType("image/jpeg");// 图片格式
// 设置浏览器不要缓存此图片
resp.setHeader("Pragma", "No-cache");
resp.setHeader("Cache-Control", "no-cache");
resp.setDateHeader("Expires", 0);
// 创建内存图像并获得图形上下文
int width = 85;
int height = 30;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics graphics = image.getGraphics();
// 验证码
String table = "0123456789QWERTYUIOPASDFGHJKLZXCVBNMzxcvbnmasdfghjklqwertyuiop";
char code[] = new char[4];
for (int i = 0; i < 4; i++) {
int rand = (int) (Math.random() * (table.length() - 1));
code[i] = table.charAt(rand);
}
// 画背景
graphics.setColor(Color.getHSBColor(211, 211, 211));
graphics.fillRect(0, 0, width, height);
graphics.setColor(Color.BLACK);
graphics.setFont(new Font("", 1, 20));
graphics.drawLine(0, 17, width, 23);
if (code[0] <= 69) {
graphics.setColor(Color.BLUE);
graphics.drawLine(0, 8, width, 28);
} else if (code[2] >= 90) {
graphics.setColor(Color.RED);
graphics.drawLine(0, 28, width, 15);
}else if (code[3] >= 111) {
graphics.setColor(Color.black);
graphics.drawLine(0, 28, width, 15);
}
// 画验证码
graphics.drawString("" + code[0], 2, 20);
graphics.setColor(Color.YELLOW);
graphics.drawString(" " + code[1], 20, 23);
graphics.setColor(Color.RED);
graphics.drawString(" " + code[2], 40, 20);
graphics.setColor(Color.BLACK);
graphics.drawString(" " + code[3], 60, 25);
// 将图像传到客户端
ServletOutputStream sos = resp.getOutputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "JPEG", baos);
byte[] buffer = baos.toByteArray();
resp.setContentLength(buffer.length);
sos.write(buffer);
baos.close();
sos.close();
// 验证
session.setAttribute("truecode", ""+code[0]+code[1]+code[2]+code[3]);
}
@Override
public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException {
// TODO 自动生成的方法存根
super.service(arg0, arg1);
}
}
前端:
<s:textfield name=“code” title=“请输入验证码”></s:textfield>
登录验证的action:
package controller.login;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
import model.linkdbm.JDBC_DBM;
public class Login_db extends ActionSupport {
/**
* @author joker
*
* action
* 验证登录
*/
private static final long serialVersionUID = 6346619400337792820L;
public String user;
public String pass;
public String code;//验证码
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public String execute() throws Exception {
String truecode=(String) ServletActionContext.getRequest().getSession().getAttribute("truecode");
//登录验证码验证
if(!code.equals(truecode)) {
this.addFieldError("user", "输入的验证码有误!!!");
return INPUT;
}
boolean oginflag=true;
if (loginflag) {
this.addFieldError("user", "登录成功");
return SUCCESS;
} else {
this.addFieldError("user", "账号或密码错误!!!");
return INPUT;
}
}
@Override
public void validate() {
// TODO 自动生成的方法存根
if (user == null || user.trim().equals("")) {
this.addFieldError("user", "用户名不能为空");
return;
}
if (pass == null || pass.trim().equals("")) {
this.addFieldError("pass", "密码不能为空");
}
}
}