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>
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 手机壳按键很硬怎么办 棉质白衣服染色怎么办 白棉t恤混洗染色怎么办 包包被衣服染色了怎么办 白色衣服染了菜汁怎么办 一加3t屏幕刺眼怎么办 怀孕吃了好多杏怎么办 门破了个洞怎么办 钢圈轮毂刮花了怎么办 瓷砖用刀子划了怎么办 陶瓷洗手台裂了怎么办 洗车泵水管坏了怎么办 印胶浆里面渗入了发泡浆怎么办? 管子断在水管里怎么办 衣服上的织带缩水怎么办 真丝衣服拔缝了怎么办 顾客说衣服太花怎么办 铝和碱反应变黑怎么办 40度高温多肉怎么办 沾到医用蓝药水怎么办? 裤子弄上泡沫胶怎么办 苍蝇纸粘衣服上怎么办 苍蝇胶沾衣服上怎么办 灯带为什么不亮怎么办 苹果6比屏幕变黄怎么办 雷腾键盘锁了怎么办 自吸泵电机不转怎么办 孕38周胎儿偏小怎么办 被火烧黑的铁怎么办 锅被烟熏黑了怎么办 墙壁被烟熏黑了怎么办 壁纸被烟熏黑了怎么办 空调被烟熏黑了怎么办 牙被烟熏黑了怎么办 尖头鞋把尖折了怎么办 腰椎固定手术钢钉断了怎么办 脚被钢钉扎了怎么办 皮帘子有胶了怎么办 12v插口没有电怎么办 吃了一颗聚乙烯醇怎么办 立式注塑机产品粘膜怎么办