html注册页面带验证码,登陆注册-带图片的验证码

html注册页⾯带验证码登陆注册-带图⽚的验证码
登陆注册时的验证码。 使⽤的是SpringMVC。 你如果⽤的其他框架,只需要getCode这个⽅法就⾏。
业务逻辑:加载登陆注册页,⽣成验证码,把⽣成的验证码返回到前台页⾯展⽰,⽤户输⼊验证码进⾏登陆操作,把前台⽤户输⼊的验证码和后台⽣成的验证码进⾏⽐对。 成功进⾏登陆,失败返回错误和新的验证码。
控制器代码:
package com.shopping.ls;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import v.JModelAndView;
import com.shopping.foundation.service.ISysConfigService;
import com.shopping.foundation.service.IUserConfigService;
/**
* 登陆注册图⽚验证码
* @author ⿊夜
* 现在都是⽤短信,已经很少有⼈会⽤这种了。
*/
@Controller
public class CodeController {
//配置【可以去掉】
@Autowired
private ISysConfigService configService;
//当前⽤户【可以去掉】
@Autowired
private IUserConfigService userConfigService;
// 定义图⽚的width
private int width = 90;
// 定义图⽚的height
private int height = 40;
// 定义图⽚上显⽰验证码的个数,改的话要把宽度加长
private int codeCount = 4;
private int xx = 15;
private int fontHeight = 35;
private int codeY = 30;
char[] codeSequence = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
@RequestMapping("/code.htm")
public void getCode(HttpServletRequest req, HttpServletResponse resp) throws IOException {
// 定义图像buffer
BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
验证码自动输入
Graphics gd = Graphics();
// 创建⼀个随机数⽣成器类
Random random = new Random();
// 将图像填充为⽩⾊
gd.setColor(Color.WHITE);
gd.fillRect(0, 0, width, height);
// 创建字体,字体的⼤⼩应该根据图⽚的⾼度来定。
Font font = new Font("Fixedsys", Font.BOLD, fontHeight);
// 设置字体。
gd.setFont(font);
// 画边框。
gd.setColor(Color.BLACK);
gd.drawRect(0, 0, width - 1, height - 1);
/
/ 随机产⽣40条⼲扰线,使图象中的认证码不易被其它程序探测到。
gd.setColor(Color.BLACK);
for (int i = 0; i < 20; i++) {
int x = Int(width);
int y = Int(height);
int xl = Int(12);
int yl = Int(12);
gd.drawLine(x, y, x + xl, y + yl);
}
// randomCode⽤于保存随机产⽣的验证码,以便⽤户登录后进⾏验证。StringBuffer randomCode = new StringBuffer();
int red = 0, green = 0, blue = 0;
// 随机产⽣codeCount数字的验证码。
for (int i = 0; i < codeCount; i++) {
// 得到随机产⽣的验证码数字。
String code = String.valueOf(Int(36)]);
// 产⽣随机的颜⾊分量来构造颜⾊值,这样输出的每位数字的颜⾊值都将不同。red = Int(255);
green = Int(255);
blue = Int(255);
// ⽤随机产⽣的颜⾊将验证码绘制到图像中。
gd.setColor(new Color(red, green, blue));
gd.drawString(code, (i + 1) * xx, codeY);
// 将产⽣的四个随机数组合在⼀起。
randomCode.append(code);
}
// 将四位数字的验证码保存到Session中。
HttpSession session = Session();
System.out.println(randomCode);
session.setAttribute("code", String());
// 禁⽌图像缓存。
resp.setHeader("Pragma", "no-cache");
resp.setHeader("Cache-Control", "no-cache");
resp.setDateHeader("Expires", 0);
resp.setContentType("image/jpeg");
/
/ 将图像输出到Servlet输出流中。
ServletOutputStream sos = OutputStream();
ImageIO.write(buffImg, "jpeg", sos);
sos.close();
}
//跳转到登陆页
@RequestMapping({"/log4j.htm"})
public ModelAndView log4j(HttpServletRequest request, HttpServletResponse response) throws IOException {
ModelAndView mv = new JModelAndView("log4j.html",
UserConfig(), 1, request, response);
return mv;
}
}
前台页⾯代码:log4j.html
⽤户名
name="userName" placeholder="⽤户名" class="form-username form-control"
id="userName" maxlength="20" required>
密码
type="password" name="password" placeholder="密码" required
class="form-password form-control" id="password" maxlength="16">
class="form-password form-control" id="code">
οnclick="this.src='$!webPath/code.htm?'+Math.random()" />
登录

本文发布于:2024-09-22 06:44:45,感谢您对本站的认可!

本文链接:https://www.17tex.com/tex/4/386771.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:验证码   登陆   字体
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议