Struts2:登录验证码(随机字符串)的实现
来源:互联网 发布:武汉大学网络教学平台 编辑:程序博客网 时间:2024/05/16 11:47
页面代码
<!doctype html><html><head><meta charset="utf-8"><title>验证码实现</title><style> img{width:100px;}</style><script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script><script type="text/javascript">$(function(){ $("#securityCode-img").attr("src","getSecurityCodeImg.action?rmd="+new Date().getTime()) .click(function(){ $(this).attr("src","getSecurityCodeImg.action?rmd="+new Date().getTime()); }); });</script></head><body><span>验证码:</span><img id="securityCode-img"/></body></html>
Action代码
public class SecurityCodeAction extends ActionSupport{ private int imgWidth;//图片宽度 private int imgHeight;//图片高度 private int codeCount;//验证码个数 private String securityCode; private Random random=new Random(); public int getImgWidth() { return imgWidth; } public void setImgWidth(int imgWidth) { this.imgWidth = imgWidth; } public int getImgHeight() { return imgHeight; } public void setImgHeight(int imgHeight) { this.imgHeight = imgHeight; } public int getCodeCount() { return codeCount; } public void setCodeCount(int codeCount) { this.codeCount = codeCount; } //注意返回类型为void public void getSecurityCodeImg() throws IOException{ securityCode=this.getRandomStr(this.getCodeCount());//获取随机验证码 ActionContext.getContext().getSession().put("securityCode", securityCode);//将验证码存入session,以便登录验证时取出比较 HttpServletResponse response=ServletActionContext.getResponse(); response.setDateHeader("Expires", 1L); response.setHeader("Cache-Control", "no-cache, no-store, max-age=0"); response.addHeader("Pragma", "no-cache"); response.setContentType("image/jpeg"); this.imgRender(securityCode,response.getOutputStream());//根据生成的随机字符串生成图片 } //获取随机字符串 private String getRandomStr(int count){ String[] strs={"0","1","2","3","4","5","6","7","8","9", "a","b","c","d","e","f","g","h","i","j", "k","l","m","n","p","q","r","s","t","u", "v","w","x","y","z","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"}; String result=""; for(int i=0;i<this.getCodeCount();i++){ result+=strs[random.nextInt(strs.length)]; } return result; } //获取随机颜色 private Color getRandomColor(){ Color[] colors=new Color[5]; colors[0]=new Color(32, 158, 25); colors[1]=new Color(218, 42, 19); colors[2]=new Color(31, 75, 208); colors[3]=new Color(0, 102, 182); colors[4]=new Color(171, 0, 85); return colors[random.nextInt(5)]; } //获取随机字体 private Font getRandomFont(){ Font fonts[]=new Font[5]; fonts[0] = new Font("Aharoni", Font.PLAIN, 35); fonts[1] = new Font("Book Antiqua", Font.PLAIN, 40); fonts[2] = new Font("Calibri", Font.PLAIN, 37); fonts[3] = new Font("Lucida Console", Font.PLAIN,39); fonts[4] = new Font("DilleniaUPC", Font.PLAIN, 34); return fonts[random.nextInt(5)]; } //生成验证码图片 private void imgRender(String securityCode,OutputStream out) throws IOException{ BufferedImage bufferedImage=new BufferedImage( this.getImgWidth(),this.getImgHeight(),BufferedImage.TYPE_BYTE_INDEXED); Graphics2D g=(Graphics2D)bufferedImage.getGraphics(); g.setColor(Color.WHITE); g.fillRect(0, 0, this.getImgWidth(), this.getImgHeight()); //画出随机字符串 for(int i=0;i<securityCode.length();i++){ int y=0; char c=securityCode.charAt(i); if(i==random.nextInt(this.getCodeCount())){ y=30-random.nextInt(6); } else{ y=30+random.nextInt(6); } g.setColor(this.getRandomColor()); g.setFont(this.getRandomFont()); g.drawString(String.valueOf(c),25*i+15, y); } //随机增加100个点 for(int i=0;i<100;i++){ g.setColor(new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255))); g.drawOval(random.nextInt(this.getImgWidth()), random.nextInt(this.getImgHeight()), 0, 0); } //随机增加10条线 for(int i=0;i<20;i++){ g.setColor(new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255))); g.drawLine(random.nextInt(this.getImgWidth()), random.nextInt(this.getImgWidth()), random.nextInt(this.getImgHeight()), random.nextInt(this.getImgHeight())); } g.dispose(); ImageIO.write(bufferedImage, "jpg", out); }}
struts配置文件
<package name="security" extends="struts-default"> <action name="getSecurityCodeImg" class="com.sanwenyu.action.SecurityCodeAction" method="getSecurityCodeImg"> <param name="imgWidth">130</param> <param name="imgHeight">36</param> <param name="codeCount">4</param> </action> </package>
阅读全文
0 0
- Struts2:登录验证码(随机字符串)的实现
- Extjs4---登录验证码的实现+struts2
- 实现登录注册时随机生成的验证码
- web登录:随机验证码的设计和实现
- 基于struts2+spring+ibatis彩色随机验证码的实现
- 登录随机验证码
- 一个利用struts2实现的登录验证
- struts2的实现登录验证方法一
- 网站上登录时生成随机的验证字符串!!
- 带有随机验证码的登录页面
- struts2登录验证码
- Struts Servlet简单实现登录随机验证码
- 移动开发android自动登录+验证码+随机码的逻辑实现
- struts2+hibernate 实现B2C电子商务网站的登录 、注册中验证码的实现(源码)
- struts2+hibernate 实现B2C电子商务网站的登录 、注册中验证码的实现(源码)
- struts2+hibernate 实现B2C电子商务网站的登录 、注册中验证码的实现(源码)
- struts2+hibernate 实现B2C电子商务网站的登录 、注册中验证码的实现(源码)
- 登录验证码的实现
- 软件开发全过程
- 新闻客户端
- CSDN机器学习笔记五 Numpy简单操作
- MySql如何进行数据迁移
- 《JavaScript语言精粹》笔记(2)--对象
- Struts2:登录验证码(随机字符串)的实现
- cookie&session
- 内联函数总结
- RecyclerView
- ArrayList源码学习
- SQL模糊查询排序问题
- 关于人工智能的一些介绍与看法
- keepalived+nginx的配置
- MySQL优化系列(七)--视图应用详解