项目简单验证码
一:作用
我们使用验证码,是确保系统的使用必须要进行登录成功之后,才能使用,避免用户直接在地址栏
中输入要访问的页面也就是说,使用验证码,就强制用户用户必须先从登录界面登录
二:验证实现方式
用到两个关键类,这两个类跟图片的输出是有关系的
BufferedImage im = new BufferedImage(60,20,BufferedImage.TYPE_INT_RGB);
//第一个参数im表示一个图片对象
//JPG表示图片输出类型
//response.getOutputStream()代表一个响应的输出流,也就是说,你访问这个servlet.该
servlet就会图片显示给你
ImageIO.write(im, “JPG”,response.getOutputStream());
三.实现步骤
1.使用BufferedImage产生一个图片,然后使用ImageIO输出,并指定为JPG格式
BufferedImage im = new BufferedImage(60,20,BufferedImage.TYPE_INT_RGB);
//第一个参数im表示一个图片对象
//JPG表示图片输出类型
//response.getOutputStream()代表一个响应的输出流,也就是说,你访问这个servlet.该servlet就会图片显示给你
ImageIO.write(im, “JPG”,response.getOutputStream());
2.获取图片绘图对象
Graphics g = im.getGraphics();
3.填充绘图区域
Random rm = new Random();
Color c = new Color(rm.nextInt(255),rm.nextInt(255),rm.nextInt(255));
g.setColor(c);
//填充整个图片的颜色
g.fillRect(0, 0, 60, 20);
4.向图片中输出数字
g.setColor(new Color(rm.nextInt(255),rm.nextInt(255),rm.nextInt(255)));
g.setFont(new Font(“华文隶书”,Font.BOLD|Font.ITALIC,28));
g.drawString(“8”, 1, 18);
5.随机4位数字
//随机产生4位数字
for(int i=0;i<4;i++){
g.setColor(new Color(rm.nextInt(255),rm.nextInt(255),rm.nextInt(255)));
g.setFont(new Font(“Gungsuh”,Font.BOLD|Font.ITALIC,22));
g.drawString(“”+rm.nextInt(10), (i*15)+2, 18);
}
6.随机产生中文
String str = “胸有激雷而面如平湖者可拜上将军”;
for(int i=0;i<4;i++){
g.setColor(new Color(rm.nextInt(255),rm.nextInt(255),rm.nextInt(255)));
g.setFont(new Font(“Gungsuh”,Font.BOLD|Font.ITALIC,15));
g.drawString(“”+str.charAt(rm.nextInt(str.length())), (i*15)+2, 18);
}
7.在页面中如何来引入该验证码:
8.保存数字,以便进行登录比较
//将得到的四个数字保存到session中,以便当用户登录的时候,用来比较
request.getSession().setAttribute(“piccode”, sbf.toString());
9.登录验证
首先,需要验证该用户在数据库中是否存在,如果存在,还需要验证输入的验证码是否一致.
验证成功后,需要转发到相关的操作页面.
代码实例:
/** * @author: John * @related module:登录 * @desc: 验证码图片 */ @Controller
@RequestMapping("/code")
public class SecCodeController {
@RequestMapping
public void generate(HttpServletResponse response) {
ByteArrayOutputStream output = new ByteArrayOutputStream();
String code = drawImg(output);
Subject currentUser = SecurityUtils.getSubject();
Session session = currentUser.getSession();
//将生成的数保存到session
session.setAttribute(Const.SESSION_SECURITY_CODE, code);
try {
ServletOutputStream out = response.getOutputStream();
output.writeTo(out);
} catch (IOException e) {
e.printStackTrace();
}
}
private String drawImg(ByteArrayOutputStream output) {
String code = "";
for (int i = 0; i < 4; i++) {
code += randomChar();
}
int width = 70;
int height = 25;
//在内存中创建一张图片
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
Font font = new Font("Times New Roman", Font.PLAIN, 20);
//创建一个图形(画布)
Graphics2D g = bi.createGraphics();
//设置写入数据的字体,颜色
g.setFont(font);
Color color = new Color(66, 2, 82);
g.setColor(color);
g.setBackground(new Color(226, 226, 240));
g.clearRect(0, 0, width, height);
FontRenderContext context = g.getFontRenderContext();
Rectangle2D bounds = font.getStringBounds(code, context);
double x = (width - bounds.getWidth()) / 2;
double y = (height - bounds.getHeight()) / 2;
double ascent = bounds.getY();
double baseY = y - ascent;
//向图片上写数据
g.drawString(code, (int) x, (int) baseY);
g.dispose();
try {
//把写好的数据的图片输出给浏览器
ImageIO.write(bi, "jpg", output);
} catch (IOException e) {
e.printStackTrace();
}
return code;
}
private char randomChar() {
Random r = new Random();
String s = "ABCDEFGHJKLMNPRSTUVWXYZ0123456789";
return s.charAt(r.nextInt(s.length()));
}
}
//基于规则表达式的替换,在用,mbfw,隔开返回数组
//前端var code = "qq123456789mbfw"+loginname+",mbfw,"+password+"QQ123456789mbfw"+",mbfw,"+$("#code").val();
String KEYDATA[] = pd.getString("KEYDATA").replaceAll("qq123456789mbfw", "").replaceAll("QQ123456789mbfw", "").split(",mbfw,");
if (null != KEYDATA && KEYDATA.length == 3) {
// shiro管理的session
Subject currentUser = SecurityUtils.getSubject();
Session session = currentUser.getSession();
String sessionCode = (String) session.getAttribute(Const.SESSION_SECURITY_CODE); // 获取session中的验证码
//获取session中的验证码
String code = KEYDATA[2];
if (null == code || "".equals(code)) {
errInfo = "nullcode"; // 验证码为空
} else {
String USERNAME = KEYDATA[0];
String PASSWORD = KEYDATA[1];
pd.put("USERNAME", USERNAME);
if (Tools.notEmpty(sessionCode) && sessionCode.equalsIgnoreCase(code)) {
String passwd = new SimpleHash("SHA-1", USERNAME, PASSWORD).toString(); // 密码加密
pd.put("PASSWORD", passwd);
pd = userService.getUserByNameAndPwd(pd);
if (pd != null) {
pd.put("LAST_LOGIN", DateUtil.getTime().toString());
userService.updateLastLogin(pd);
User user = new User();
user.setUSER_ID(pd.getString("USER_ID"));
user.setUSERNAME(pd.getString("USERNAME"));
user.setPASSWORD(pd.getString("PASSWORD"));
session.removeAttribute(Const.SESSION_SECURITY_CODE);
// shiro加入身份验证
Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken(USERNAME, PASSWORD);
try {
subject.login(token);
} catch (AuthenticationException e) {
errInfo = "身份验证失败!";
}
} else {
errInfo = "usererror"; // 用户名或密码有误
}
} else {
errInfo = "codeerror"; // 验证码输入有误
}
还没有评论,来说两句吧...