博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
图片验证
阅读量:6094 次
发布时间:2019-06-20

本文共 2888 字,大约阅读时间需要 9 分钟。

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

你可能感兴趣的文章
Hadoop集群搭建的无密登录配置
查看>>
angular使directive让div contenteditable & ng-model生效
查看>>
制作CentOS 6.4 U盘启动安装盘
查看>>
Java try、catch、finally及finally执行顺序详解
查看>>
children childNodes nodeType
查看>>
如何在Ubuntu 16.04上将Redis服务器设置为PHP的会话处理程序
查看>>
固态硬盘价格大跳水,再不入手又要涨了!
查看>>
css隐形的空隙(inline的坑)
查看>>
深圳美景品牌策划机构:美景“快传播”赢得法国最大乳业合作社赞誉
查看>>
nginx服务
查看>>
Android中使用自定义的字体
查看>>
linux 中文件类型和颜色的区分
查看>>
cocosPods 常见使用步骤
查看>>
对你同样重要的非技术贴,8个方法让你的老板认可你
查看>>
MLP、RBF、SVM神经网络比较
查看>>
最常用的命令
查看>>
mysql数据库备份小记录
查看>>
WordPress 手机客户端生成系统 NextApp 配置指南
查看>>
字典 dict
查看>>
iOS 9 sdk bitcode
查看>>