struts2实现验证码还是很方便的。struts确实给我们带来了很多的方便之处。
1:AuthCode.java是用来生成验证码图片的:
public class AuthCode { private ByteArrayInputStream input; private ByteArrayOutputStream output; private String code;// 验证码 private int codeNum;// 验证码字符数量 private int width; private int height; // 构造器 private AuthCode(int width, int height, int codeNum) { this.width = width; this.height = height; this.codeNum = codeNum; if (width < 15 * codeNum + 6) { this.width = 13 * codeNum + 6; } if (height < 20) { this.height = 20; } buildImage(); } // 以字符串形式返回验证码 public String getCode() { return code; } // 以输入流的形式返回验证图片 public ByteArrayInputStream getIamgeAsInputStream() { return input; } // 以输出流的形式返回验证图片 public ByteArrayOutputStream getImageAsOuputStream() { return output; } // 创建默认大小的验证码 public static AuthCode createInstance() { return new AuthCode(85, 20, 4); } // 创建指定大小的验证码 public static AuthCode createInstance(int width, int height, int codeNum) { return new AuthCode(width, height, codeNum); } // 生成验证码图片 private void buildImage() { BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // 获取图形上下文 Graphics g = image.getGraphics(); // 生成随机类 Random random = new Random(); // 设定背景色 g.setColor(getRandColor(200, 250)); g.fillRect(0, 0, width, height); // 设定字体 g.setFont(new Font("Times New Roman", Font.PLAIN, 18)); // 随机产生150条干扰线,使图象中的认证码不易被其它程序探测到 g.setColor(getRandColor(160, 200)); for (int i = 0; i < 150; i++) { int x = random.nextInt(width); int y = random.nextInt(height); int xl = random.nextInt(12); int yl = random.nextInt(12); g.drawLine(x, y, x + xl, y + yl); } // 取随机产生的认证码 String codes = "ABCDEFGHJKLMNOPQRSTUVWXYZ23456789"; String sRand = ""; for (int i = 0; i < codeNum; i++) { String rand = codes.charAt(random.nextInt(codes.length())) + ""; sRand += rand; // 将认证码显示到图象中 g.setColor(new Color(20 + random.nextInt(110), 20 + random .nextInt(110), 20 + random.nextInt(110))); // 将字符串绘制到图片上 g.drawString(rand, i * (width / codeNum) + 6, (int)((height+12)/2)); } /* 验证码赋值 */ this.code = sRand; // 图象生效 g.dispose(); try { output = new ByteArrayOutputStream(); ImageOutputStream imageOut = ImageIO .createImageOutputStream(output); ImageIO.write(image, "JPEG", imageOut); imageOut.close(); input = new ByteArrayInputStream(output.toByteArray()); } catch (Exception e) { System.out.println("验证码图片产生出现错误:" + e.toString()); } } // 获取随机颜色 private Color getRandColor(int fc, int bc) { Random random = new Random(); if (fc > 255) fc = 255; if (bc > 255) bc = 255; int r = fc + random.nextInt(bc - fc); int g = fc + random.nextInt(bc - fc); int b = fc + random.nextInt(bc - fc); return new Color(r, g, b); } }
下面的是action中的调用方法:
public InputStream getInputStream(){ AuthCode code = AuthCode.createInstance(); HttpServletRequest request = ServletActionContext.getRequest(); HttpSession seesion = request.getSession(); // session.put("authCode", code.getCode()); seesion.setAttribute("authcode", code.getCode()); return code.getIamgeAsInputStream(); } public String execute(){ return "success"; }
上面代码将生成的code放入session中了,这样在验证的时候就可以进行equals了。在action对应的.xml文件中得设置struts提供的stream模式来处理result
<action name="authcode" class="project.action.ImageAction" method="execute"> <result name="success" type="stream"> <param name="contentType">image/jpeg</param> </result> </action>
然后还要在使用验证码图片的页面上添加链接:
<tr> <td width="13%" height="35" ><span class="login_txt">验证码</span></td> <td height="35" colspan="2" class="top_hui_text"> <input class="wenbenkuang" name="authCode" type="text" value="" maxLength="4" size="10"> <img src="authcode.action" alt="验证码" style="cursor:hand" title="看不清楚?换一张" onclick="changeImage(this)"/> <label class="login_txt_bt">${authcode_msg }</label> </td> </tr>
这样就可以显示随机验证码图片了。
当然在做登录的时候需要验证码图片,这个用struts2真是so easy啊:下面的是登录的action,因为写的比较赶,就很随便了:
public String login(){ String flag = ""; HttpServletRequest request = ServletActionContext.getRequest(); HttpSession session = request.getSession(); String code = (String) session.getAttribute("authcode"); if(authCode != null & authCode.toUpperCase().equals(code)){ if(pd.queryLogin(power.getUserName(), power.getPassword()) == true){ // session.put("power",power); power = pd.queryByName(power.getUserName()); session.setAttribute("power", power); System.out.println(pd.queryByName(power.getUserName()).getPowerset()); if((pd.queryByName(power.getUserName()).getPowerset()).equals("1111")){ flag = "admin"; System.out.println(flag); }else{ flag = "adminlimit"; System.out.println(flag); } }else{ flag = "fail"; } }else{ authcode_msg = "验证码错误"; flag = "error"; } return flag; }
相关推荐
Struts+jsp版的随机验证和servlet+jsp版的随机验证码
生成随机验证码 可在struts中使用
众所周知,验证码在我们的生活中都是非常常见的,很多公司都在各种折腾各种各样的验证码,这里简要的用一个小案例来实现验证码的功能(ps:其实我挺讨厌验证码这个东西的)。 今天分享的是通过ajax来动态的验证验证码...
Struts 2.0技术综合应用,包括上传图片功能,明文加密算法SHA-1和MD5,上传用户头像,根据action随机生成验证码,用链接实现数据的分页处理,以及拦截器和Logger日志框架的引入,总之相当强大,学习Struts 2.0,把这...
随机彩色防识别验证码 内置管理员账户,可以创建、删除、修改用户,并查看用户列表和单个用户详情 普通用户账户,记录用户名和密码在SQL数据库中,支持用户头像上传 普通用户能实现发新帖、回帖、浏览帖子,但不...
20.5.4 生成随机验证码的imgNum类 358 20.5.5 用户登录页面index.jsp 359 20.5.6 验证用户登录信息的Servlet类login 360 20.6 显示宠物信息模块的实现 363 20.6.1 对应宠物的实体类User 363 20.6.2 定义对宠物信息...
提供用户登录功能,随机生成验证码验证 4 后台管理模块 提供日志的查询、删除、修改、新增功能 提供分页功能 1.3. 运行环境 1. 软件环境 分类 名称 版本 语种 操作系统 Windows XP 简体中文 数据库平台 Oracle10g ...
SSH 小例子,一个论坛用户登录的例子,可以生成随机验证码,附带PDF教程,需要根据教程创建Mysql表,然后发布到Tomcat,就可以运行了
这是使用spring、struts2、hibernate整合后的网上考试系统的全部代码,包括管理员登录、学生登录以及登录验证与验证码验证,试卷随机生成,试卷保存与查看等内容。想要了解更多的内容,请赶紧下载!
利用随机函数抽取幸运数字 简单 EJB的真实世界模型(源代码) 15个目标文件 摘要:Java源码,初学实例,基于EJB的真实世界模型 基于EJB的真实世界模型,附源代码,部分功能需JSP配合完成。 J2ME优化压缩PNG文件 4个...
利用随机函数抽取幸运数字 简单 EJB的真实世界模型(源代码) 15个目标文件 摘要:Java源码,初学实例,基于EJB的真实世界模型 基于EJB的真实世界模型,附源代码,部分功能需JSP配合完成。 J2ME优化压缩PNG文件 4个...