码迷,mamicode.com
首页 > 其他好文 > 详细

简单实现验证码

时间:2015-11-01 13:52:21      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:

import java.awt.Color;  
import java.awt.Font;  
import java.awt.Graphics;  
import java.awt.image.BufferedImage;  
import java.util.Random;  
  
/** 
 * 验证码生成器 
 * @see -------------------------------------------------------------------------------------------------------------- 
 * @see 可生成数字、大写、小写字母及三者混合类型的验证码 
 * @see 支持自定义验证码字符数量,支持自定义验证码图片的大小,支持自定义需排除的特殊字符,支持自定义干扰线的数量,支持自定义验证码图文颜色 
 * @see -------------------------------------------------------------------------------------------------------------- 
 * @see 另外,给Shiro加入验证码有多种方式,也可以通过继承修改FormAuthenticationFilter类,通过Shiro去验证验证码 
 * @see 而这里既然使用了SpringMVC,也为了简化操作,就使用此工具生成验证码,并在Controller中处理验证码的校验 
 * @see -------------------------------------------------------------------------------------------------------------- 
 * @create Sep 29, 2013 4:23:13 PM 
 * @author 玄玉<http://blog.csdn.net/jadyer> 
 */  
public class VerifyCodeUtil {  
    /** 
     * 验证码类型为仅数字,即0~9 
     */  
    public static final int TYPE_NUM_ONLY = 0;  
  
    /** 
     * 验证码类型为仅字母,即大小写字母混合 
     */  
    public static final int TYPE_LETTER_ONLY = 1;  
  
    /** 
     * 验证码类型为数字和大小写字母混合 
     */  
    public static final int TYPE_ALL_MIXED = 2;  
  
    /** 
     * 验证码类型为数字和大写字母混合 
     */  
    public static final int TYPE_NUM_UPPER = 3;  
  
    /** 
     * 验证码类型为数字和小写字母混合 
     */  
    public static final int TYPE_NUM_LOWER = 4;  
  
    /** 
     * 验证码类型为仅大写字母 
     */  
    public static final int TYPE_UPPER_ONLY = 5;  
  
    /** 
     * 验证码类型为仅小写字母 
     */  
    public static final int TYPE_LOWER_ONLY = 6;  
  
    private VerifyCodeUtil(){}  
      
    /** 
     * 生成随机颜色 
     */  
    private static Color generateRandomColor() {  
        Random random = new Random();  
        return new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255));  
    }  
      
      
    /** 
     * 生成图片验证码 
     * @param type           验证码类型,参见本类的静态属性 
     * @param length         验证码字符长度,要求大于0的整数 
     * @param excludeString  需排除的特殊字符 
     * @param width          图片宽度(注意此宽度若过小,容易造成验证码文本显示不全,如4个字符的文本可使用85到90的宽度) 
     * @param height         图片高度 
     * @param interLine      图片中干扰线的条数 
     * @param randomLocation 每个字符的高低位置是否随机 
     * @param backColor      图片颜色,若为null则表示采用随机颜色 
     * @param foreColor      字体颜色,若为null则表示采用随机颜色 
     * @param lineColor      干扰线颜色,若为null则表示采用随机颜色 
     * @return 图片缓存对象 
     */  
    public static BufferedImage generateImageCode(int type, int length, String excludeString, int width, int height, int interLine, boolean randomLocation, Color backColor, Color foreColor, Color lineColor){  
        String textCode = generateTextCode(type, length, excludeString);  
        return generateImageCode(textCode, width, height, interLine, randomLocation, backColor, foreColor, lineColor);  
    }  
      
  
    /** 
     * 生成验证码字符串 
     * @param type          验证码类型,参见本类的静态属性 
     * @param length        验证码长度,要求大于0的整数 
     * @param excludeString 需排除的特殊字符(无需排除则为null) 
     * @return 验证码字符串 
     */  
    public static String generateTextCode(int type, int length, String excludeString){  
        if(length <= 0){  
            return "";  
        }  
        StringBuffer verifyCode = new StringBuffer();  
        int i = 0;  
        Random random = new Random();  
        switch(type){  
            case TYPE_NUM_ONLY:  
                while(i < length){  
                    int t = random.nextInt(10);  
                    //排除特殊字符  
                    if(null==excludeString || excludeString.indexOf(t+"")<0) {  
                        verifyCode.append(t);  
                        i++;  
                    }  
                }  
            break;  
            case TYPE_LETTER_ONLY:  
                while(i < length){  
                    int t = random.nextInt(123);  
                    if((t>=97 || (t>=65&&t<=90)) && (null==excludeString||excludeString.indexOf((char)t)<0)){  
                        verifyCode.append((char)t);  
                        i++;  
                    }  
                }  
            break;  
            case TYPE_ALL_MIXED:  
                while(i < length){  
                    int t = random.nextInt(123);  
                    if((t>=97 || (t>=65&&t<=90) || (t>=48&&t<=57)) && (null==excludeString||excludeString.indexOf((char)t)<0)){  
                        verifyCode.append((char)t);  
                        i++;  
                    }  
                }  
            break;  
            case TYPE_NUM_UPPER:  
                while(i < length){  
                    int t = random.nextInt(91);  
                    if((t>=65 || (t>=48&&t<=57)) && (null==excludeString || excludeString.indexOf((char)t)<0)){  
                        verifyCode.append((char)t);  
                        i++;  
                    }  
                }  
            break;  
            case TYPE_NUM_LOWER:  
                while(i < length){  
                    int t = random.nextInt(123);  
                    if((t>=97 || (t>=48&&t<=57)) && (null==excludeString || excludeString.indexOf((char)t)<0)){  
                        verifyCode.append((char)t);  
                        i++;  
                    }  
                }  
            break;  
            case TYPE_UPPER_ONLY:  
                while(i < length){  
                    int t = random.nextInt(91);  
                    if((t >= 65) && (null==excludeString||excludeString.indexOf((char)t)<0)){  
                        verifyCode.append((char)t);  
                        i++;  
                    }  
                }  
            break;  
            case TYPE_LOWER_ONLY:  
                while(i < length){  
                    int t = random.nextInt(123);  
                    if((t>=97) && (null==excludeString||excludeString.indexOf((char)t)<0)){  
                        verifyCode.append((char)t);  
                        i++;  
                    }  
                }  
            break;  
        }  
        return verifyCode.toString();  
    }  
  
    /** 
     * 已有验证码,生成验证码图片 
     * @param textCode       文本验证码 
     * @param width          图片宽度(注意此宽度若过小,容易造成验证码文本显示不全,如4个字符的文本可使用85到90的宽度) 
     * @param height         图片高度 
     * @param interLine      图片中干扰线的条数 
     * @param randomLocation 每个字符的高低位置是否随机 
     * @param backColor      图片颜色,若为null则表示采用随机颜色 
     * @param foreColor      字体颜色,若为null则表示采用随机颜色 
     * @param lineColor      干扰线颜色,若为null则表示采用随机颜色 
     * @return 图片缓存对象 
     */  
    public static BufferedImage generateImageCode(String textCode, int width, int height, int interLine, boolean randomLocation, Color backColor, Color foreColor, Color lineColor){  
        //创建内存图像  
        BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);  
        //获取图形上下文  
        Graphics graphics = bufferedImage.getGraphics();  
        //画背景图  
        graphics.setColor(null==backColor ? generateRandomColor() : backColor);  
        graphics.fillRect(0, 0, width, height);  
        //画干扰线  
        Random random = new Random();  
        if(interLine > 0){  
            int x = 0, y = 0, x1 = width, y1 = 0;  
            for(int i=0; i<interLine; i++){  
                graphics.setColor(null==lineColor ? generateRandomColor() : lineColor);  
                y = random.nextInt(height);  
                y1 = random.nextInt(height);  
                graphics.drawLine(x, y, x1, y1);  
            }  
        }  
        //字体大小为图片高度的80%  
        int fsize = (int)(height * 0.8);  
        int fx = height - fsize;  
        int fy = fsize;  
        //设定字体  
        graphics.setFont(new Font("Default", Font.PLAIN, fsize));  
        //写验证码字符  
        for(int i=0; i<textCode.length(); i++){  
            fy = randomLocation ? (int)((Math.random()*0.3+0.6)*height) : fy;  
            graphics.setColor(null==foreColor ? generateRandomColor() : foreColor);  
            //将验证码字符显示到图象中  
            graphics.drawString(textCode.charAt(i)+"", fx, fy);  
            fx += fsize * 0.9;  
        }  
        graphics.dispose();  
        return bufferedImage;  
    }  
}  

  

/**
 * 小工具类
 * @author Administrator
 *
 */
@RestController
public class UtilController {

	@Resource
	private UtilDaoService utilDaoService;
	Logger logging=Logger.getLogger(UtilController.class);
	    /** 
	     * 获取验证码图片和文本(验证码文本会保存在HttpSession中) 
	     */  
		/**
		 * 验证码生成类
		 * @param request
		 * @param response
		 * @throws IOException
		 */
	    @RequestMapping("getVerifyCodeImage")  
	    public void getVerifyCodeImage(HttpServletRequest request, HttpServletResponse response) throws IOException {  
	        //设置页面不缓存  
	        response.setHeader("Pragma", "no-cache");  
	        response.setHeader("Cache-Control", "no-cache");  
	        response.setDateHeader("Expires", 0);  
	        String verifyCode = VerifyCodeUtil.generateTextCode(VerifyCodeUtil.TYPE_NUM_ONLY, 4, null);  
	        //将验证码放到HttpSession里面  
	        request.getSession().setAttribute("verifyCode", verifyCode);  
	       logging.info("本次生成的验证码为[" + verifyCode + "],已存放到HttpSession中");  
	        //设置输出的内容的类型为JPEG图像  
	        response.setContentType("image/jpeg");
	        BufferedImage bufferedImage = VerifyCodeUtil.generateImageCode(verifyCode, 90, 30, 3, true, Color.WHITE, Color.BLACK, null);  
	        //写给浏览器  
	        ImageIO.write(bufferedImage, "JPEG", response.getOutputStream());  
	    }

  

  
  
<!DOCTYPE html>
<html>
<head lang="en">
  <meta charset="UTF-8">
  <title>首页</title>
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
  <meta name="format-detection" content="telephone=no">
  <meta name="renderer" content="webkit">
  <meta http-equiv="Cache-Control" content="no-siteapp" />

<link rel="stylesheet"
	href="http://localhost:8089/easeshop/resource/assets/css/amazeui.app.css" />
  <style>
  
    
  </style>
</head>
<body style="
background-image: url(‘http://img1.3lian.com/img2011/w1/101/90/d/105.jpg‘);
">

 
 
 
 <form class="am-form-inline" action="http://localhost:8089/easeshop/admin/login" method="post">
  <div class="am-form-group"  align="center" style="position: absolute;
                margin-top:10%; 
                margin-right:25%;
                width: 30%; 
                height:350px; 
                right:1%;
                top: 50; 
             /*    background-color:buttontext; */
                borderborder: 1px solid gray;
                ">
              
                <div align="center" ><font size="10" >用户登录</font></div>
                <label><span><font size="3" color="red"></font></span></label>
                 <label><span><font size="3" color="red"></font></span></label>
                  <label><span><font size="3" color="red"></font></span></label>
                <div>
                 
                <input type="text" class="am-form-field" minlength="11"  id="username" name="username"  placeholder="手机号码" required/><br/><br/>
               
                <input type="password" class="am-form-field" minlength="6" id="password" name="password" placeholder="密码" required/><br/><br/>
               
                 <input type="yzm" class="am-form-field" minlength="4" id="password" name="yzm" placeholder="输入验证码" required/><br/><br/>
                
                 <img id="code"  src="../getVerifyCodeImage?i=1"  onclick="updateImg()" />
 						<a href="javascript:void(0)" onclick="updateImg()">刷新验证码</a><br/><br/>
 						<input type="checkbox" name="autologin" />下次自动登录
 						
 				<button type="submit" data-am-loading="{loadingText: ‘努力加载中...‘}"  class="am-btn am-btn-primary am-btn-block">登录</button>
                </div>
                
            
  </div> 
  </div>
</form>
 
<!--   <a href="#sidebar" class="am-btn am-btn-sm am-btn-success am-icon-bars am-show-sm-only my-button" data-am-offcanvas><span class="am-sr-only">侧栏导航</span></a> -->
</div>
<!-- <iframe src="" name="renwu">sdfdsf</iframe> -->
<!-- <footer class="my-footer">
  <p>sidebar template<br><small>© Copyright XXX. by the AmazeUI Team.</small></p>
</footer>
 -->
<!--[if lt IE 9]>
<script src="http://libs.baidu.com/jquery/1.11.1/jquery.min.js"></script>
<script src="http://cdn.staticfile.org/modernizr/2.8.3/modernizr.js"></script>
<script src="assets/js/polyfill/rem.min.js"></script>
<script src="assets/js/polyfill/respond.min.js"></script>
<script src="assets/js/amazeui.legacy.js"></script>
<![endif]-->

<!--[if (gte IE 9)|!(IE)]><!-->
<script src="http://localhost:8089/easeshop/resource/assets/js/jquery.min.js"></script>
<script src="http://localhost:8089/easeshop/resource/assets/js/amazeui.min.js"></script>
<script src="http://localhost:8089/easeshop/resource/assets/js/app.js"></script>
<script type="text/javascript">  
 
function updateImg(){
	
    $("#code").attr("src",‘../getVerifyCodeImage?i=‘+Math.random());
	
}
   

</script> 
<!--<![endif]-->
</body>
</html>

  

简单实现验证码

标签:

原文地址:http://www.cnblogs.com/yabushan/p/4927492.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!