图片验证

本文详细介绍了如何在Java中生成并验证验证码图片,包括使用Spring框架的Controller层进行验证码生成,以及在登录验证过程中检查验证码的正确性。此外,还展示了如何在JSP页面上显示验证码图片,并提供了完整的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.Controller package com.ryx.action;

import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.util.Random;

import javax.imageio.ImageIO; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping;

@Controller @RequestMapping("/images") public class PicController { private int width = 120; private int height = 40; private String source = "123456789";

@RequestMapping("pic")
public void showPic(HttpSession session, HttpServletResponse resp) throws Exception {
	BufferedImage buffer = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
	Graphics g = buffer.getGraphics();
	g.setColor(Color.ORANGE);
	g.fillRect(0, 0, width, height);
	g.setColor(Color.BLACK);
	g.drawRect(2, 2, width - 6, height - 6);
	String checkcode = this.generateCheckcode(4);
	session.setAttribute("checkcode", checkcode);
	g.setFont(new Font("宋体", Font.BOLD, 28));
	g.setColor(new Color(158, 50, 75));
	g.drawString(checkcode, 10, height - 10);
	// 注意实际上这里还需要绘制杂点或者线,以避免使用OCR识别图片
	g.dispose();
	// 清除缓存
	resp.setHeader("pragma", "no-cache");
	resp.setHeader("cache-control", "no-cache");
	resp.setDateHeader("expires", -1);
	// 说明打开图片类型
	resp.setContentType("image/jpeg");
	resp.resetBuffer();// 清空resp
	ServletOutputStream sos = resp.getOutputStream();
	ImageIO.write(buffer, "jpg", sos);//二维图片打印成指定格式
	sos.flush();
	sos.close();
}

// 生成验证码
private String generateCheckcode(int len) {
	char[] res = new char[len];
	Random r = new Random();
	for (int i = 0; i < len; i++)
		res[i] = source.charAt(r.nextInt(source.length()));
	return new String(res);
}
复制代码

} 2.调用Controller层 @RequestMapping(value = "login", method = RequestMethod.POST) public String login(@Validated(UserGroup.LoginGroup.class) @ModelAttribute("user") UserBean user, HttpSession session,Errors errors, Model model) throws Exception { Object obj = session.getAttribute("checkcode"); String code = user.getCheckcode(); if(code != null && !code.equals(obj)) errors.rejectValue("checkcode", null, null, "验证码输入错误"); if (errors.hasErrors()) { model.addAttribute("msg", "登录失败!请重新登录"); return "user/login"; } boolean bb = userService.login(user); if (bb) { return "user/info"; } else { model.addAttribute("msg", "登录失败!请重新登录"); return "user/login"; }

3.Jsp页面层

<form:form action="{pageContext.request.contextPath}/login.do" modelAttribute="user">   <table>    <caption>     <span class="error">{msg} <form:label path="username">账号:</form:label> <form:input path="username"/> <form:errors path="username" cssClass="error" /> <form:label path="password">密码:</form:label> <form:password path="password" /> <form:errors path="password" cssClass="error" /> <form:label path="checkcode">校验码:</form:label> <form:input path="checkcode" /> <form:errors path="checkcode" cssClass="error" /> 点击刷新 </form:form>

转载于:https://juejin.im/post/5c07bdc26fb9a049a42ed52c

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值