java-通过类实现验证码(一)

[java]  view plain copy print ?
  1. package com.jadyer.util;  
  2.   
  3. import java.awt.Color;  
  4. import java.awt.Font;  
  5. import java.awt.Graphics;  
  6. import java.awt.image.BufferedImage;  
  7. import java.io.File;  
  8. import java.io.IOException;  
  9. import java.util.Random;  
  10.   
  11. import javax.imageio.ImageIO;  
  12.   
  13. /** 
  14.  * 验证码生成器 
  15.  * @see -------------------------------------------------------------------------------------------------------------- 
  16.  * @see 可生成数字、大写、小写字母及三者混合类型的验证码 
  17.  * @see 支持自定义验证码字符数量,支持自定义验证码图片的大小,支持自定义需排除的特殊字符,支持自定义干扰线的数量,支持自定义验证码图文颜色 
  18.  * @see -------------------------------------------------------------------------------------------------------------- 
  19.  * @see 另外,给Shiro加入验证码有多种方式,也可以通过继承修改FormAuthenticationFilter类,通过Shiro去验证验证码 
  20.  * @see 而这里既然使用了SpringMVC,也为了简化操作,就使用此工具生成验证码,并在Controller中处理验证码的校验 
  21.  * @see -------------------------------------------------------------------------------------------------------------- 
  22.  * @create Sep 29, 2013 4:23:13 PM 
  23.  * @author 玄玉<http://blog.csdn.net/jadyer> 
  24.  */  
  25. public class VerifyCodeUtil {  
  26.     /** 
  27.      * 验证码类型为仅数字,即0~9 
  28.      */  
  29.     public static final int TYPE_NUM_ONLY = 0;  
  30.   
  31.     /** 
  32.      * 验证码类型为仅字母,即大小写字母混合 
  33.      */  
  34.     public static final int TYPE_LETTER_ONLY = 1;  
  35.   
  36.     /** 
  37.      * 验证码类型为数字和大小写字母混合 
  38.      */  
  39.     public static final int TYPE_ALL_MIXED = 2;  
  40.   
  41.     /** 
  42.      * 验证码类型为数字和大写字母混合 
  43.      */  
  44.     public static final int TYPE_NUM_UPPER = 3;  
  45.   
  46.     /** 
  47.      * 验证码类型为数字和小写字母混合 
  48.      */  
  49.     public static final int TYPE_NUM_LOWER = 4;  
  50.   
  51.     /** 
  52.      * 验证码类型为仅大写字母 
  53.      */  
  54.     public static final int TYPE_UPPER_ONLY = 5;  
  55.   
  56.     /** 
  57.      * 验证码类型为仅小写字母 
  58.      */  
  59.     public static final int TYPE_LOWER_ONLY = 6;  
  60.   
  61.     private VerifyCodeUtil(){}  
  62.       
  63.     /** 
  64.      * 生成随机颜色 
  65.      */  
  66.     private static Color generateRandomColor() {  
  67.         Random random = new Random();  
  68.         return new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255));  
  69.     }  
  70.       
  71.       
  72.     /** 
  73.      * 生成图片验证码 
  74.      * @param type           验证码类型,参见本类的静态属性 
  75.      * @param length         验证码字符长度,要求大于0的整数 
  76.      * @param excludeString  需排除的特殊字符 
  77.      * @param width          图片宽度(注意此宽度若过小,容易造成验证码文本显示不全,如4个字符的文本可使用85到90的宽度) 
  78.      * @param height         图片高度 
  79.      * @param interLine      图片中干扰线的条数 
  80.      * @param randomLocation 每个字符的高低位置是否随机 
  81.      * @param backColor      图片颜色,若为null则表示采用随机颜色 
  82.      * @param foreColor      字体颜色,若为null则表示采用随机颜色 
  83.      * @param lineColor      干扰线颜色,若为null则表示采用随机颜色 
  84.      * @return 图片缓存对象 
  85.      */  
  86.     public static BufferedImage generateImageCode(int type, int length, String excludeString, int width, int height, int interLine, boolean randomLocation, Color backColor, Color foreColor, Color lineColor){  
  87.         String textCode = generateTextCode(type, length, excludeString);  
  88.         return generateImageCode(textCode, width, height, interLine, randomLocation, backColor, foreColor, lineColor);  
  89.     }  
  90.       
  91.   
  92.     /** 
  93.      * 生成验证码字符串 
  94.      * @param type          验证码类型,参见本类的静态属性 
  95.      * @param length        验证码长度,要求大于0的整数 
  96.      * @param excludeString 需排除的特殊字符(无需排除则为null) 
  97.      * @return 验证码字符串 
  98.      */  
  99.     public static String generateTextCode(int type, int length, String excludeString){  
  100.         if(length <= 0){  
  101.             return "";  
  102.         }  
  103.         StringBuffer verifyCode = new StringBuffer();  
  104.         int i = 0;  
  105.         Random random = new Random();  
  106.         switch(type){  
  107.             case TYPE_NUM_ONLY:  
  108.                 while(i < length){  
  109.                     int t = random.nextInt(10);  
  110.                     //排除特殊字符  
  111.                     if(null==excludeString || excludeString.indexOf(t+"")<0) {  
  112.                         verifyCode.append(t);  
  113.                         i++;  
  114.                     }  
  115.                 }  
  116.             break;  
  117.             case TYPE_LETTER_ONLY:  
  118.                 while(i < length){  
  119.                     int t = random.nextInt(123);  
  120.                     if((t>=97 || (t>=65&&t<=90)) && (null==excludeString||excludeString.indexOf((char)t)<0)){  
  121.                         verifyCode.append((char)t);  
  122.                         i++;  
  123.                     }  
  124.                 }  
  125.             break;  
  126.             case TYPE_ALL_MIXED:  
  127.                 while(i < length){  
  128.                     int t = random.nextInt(123);  
  129.                     if((t>=97 || (t>=65&&t<=90) || (t>=48&&t<=57)) && (null==excludeString||excludeString.indexOf((char)t)<0)){  
  130.                         verifyCode.append((char)t);  
  131.                         i++;  
  132.                     }  
  133.                 }  
  134.             break;  
  135.             case TYPE_NUM_UPPER:  
  136.                 while(i < length){  
  137.                     int t = random.nextInt(91);  
  138.                     if((t>=65 || (t>=48&&t<=57)) && (null==excludeString || excludeString.indexOf((char)t)<0)){  
  139.                         verifyCode.append((char)t);  
  140.                         i++;  
  141.                     }  
  142.                 }  
  143.             break;  
  144.             case TYPE_NUM_LOWER:  
  145.                 while(i < length){  
  146.                     int t = random.nextInt(123);  
  147.                     if((t>=97 || (t>=48&&t<=57)) && (null==excludeString || excludeString.indexOf((char)t)<0)){  
  148.                         verifyCode.append((char)t);  
  149.                         i++;  
  150.                     }  
  151.                 }  
  152.             break;  
  153.             case TYPE_UPPER_ONLY:  
  154.                 while(i < length){  
  155.                     int t = random.nextInt(91);  
  156.                     if((t >= 65) && (null==excludeString||excludeString.indexOf((char)t)<0)){  
  157.                         verifyCode.append((char)t);  
  158.                         i++;  
  159.                     }  
  160.                 }  
  161.             break;  
  162.             case TYPE_LOWER_ONLY:  
  163.                 while(i < length){  
  164.                     int t = random.nextInt(123);  
  165.                     if((t>=97) && (null==excludeString||excludeString.indexOf((char)t)<0)){  
  166.                         verifyCode.append((char)t);  
  167.                         i++;  
  168.                     }  
  169.                 }  
  170.             break;  
  171.         }  
  172.         return verifyCode.toString();  
  173.     }  
  174.   
  175.     /** 
  176.      * 已有验证码,生成验证码图片 
  177.      * @param textCode       文本验证码 
  178.      * @param width          图片宽度(注意此宽度若过小,容易造成验证码文本显示不全,如4个字符的文本可使用85到90的宽度) 
  179.      * @param height         图片高度 
  180.      * @param interLine      图片中干扰线的条数 
  181.      * @param randomLocation 每个字符的高低位置是否随机 
  182.      * @param backColor      图片颜色,若为null则表示采用随机颜色 
  183.      * @param foreColor      字体颜色,若为null则表示采用随机颜色 
  184.      * @param lineColor      干扰线颜色,若为null则表示采用随机颜色 
  185.      * @return 图片缓存对象 
  186.      */  
  187.     public static BufferedImage generateImageCode(String textCode, int width, int height, int interLine, boolean randomLocation, Color backColor, Color foreColor, Color lineColor){  
  188.         //创建内存图像  
  189.         BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);  
  190.         //获取图形上下文  
  191.         Graphics graphics = bufferedImage.getGraphics();  
  192.         //画背景图  
  193.         graphics.setColor(null==backColor ? generateRandomColor() : backColor);  
  194.         graphics.fillRect(00, width, height);  
  195.         //画干扰线  
  196.         Random random = new Random();  
  197.         if(interLine > 0){  
  198.             int x = 0, y = 0, x1 = width, y1 = 0;  
  199.             for(int i=0; i<interLine; i++){  
  200.                 graphics.setColor(null==lineColor ? generateRandomColor() : lineColor);  
  201.                 y = random.nextInt(height);  
  202.                 y1 = random.nextInt(height);  
  203.                 graphics.drawLine(x, y, x1, y1);  
  204.             }  
  205.         }  
  206.         //字体大小为图片高度的80%  
  207.         int fsize = (int)(height * 0.8);  
  208.         int fx = height - fsize;  
  209.         int fy = fsize;  
  210.         //设定字体  
  211.         graphics.setFont(new Font("Default", Font.PLAIN, fsize));  
  212.         //写验证码字符  
  213.         for(int i=0; i<textCode.length(); i++){  
  214.             fy = randomLocation ? (int)((Math.random()*0.3+0.6)*height) : fy;  
  215.             graphics.setColor(null==foreColor ? generateRandomColor() : foreColor);  
  216.             //将验证码字符显示到图象中  
  217.             graphics.drawString(textCode.charAt(i)+"", fx, fy);  
  218.             fx += fsize * 0.9;  
  219.         }  
  220.         graphics.dispose();  
  221.         return bufferedImage;  
  222.     }  
  223. }  

Controller类中主要方法:

<span style="font-size:12px;">/** 
     * 获取验证码图片和文本(验证码文本会保存在HttpSession中) 
     */  
    @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_ALL_MIXED, 4, null);  
        //将验证码放到HttpSession里面  
        request.getSession().setAttribute("verifyCode", verifyCode);  
        System.out.println("本次生成的验证码为[" + verifyCode + "],已存放到HttpSession中");  
        //设置输出的内容的类型为JPEG图像  
        response.setContentType("image/jpeg");  
        BufferedImage bufferedImage = VerifyCodeUtil.generateImageCode(verifyCode, 90, 25, 4, true, Color.WHITE, Color.BLACK, null);  
        //写给浏览器  
        ImageIO.write(bufferedImage, "JPEG", response.getOutputStream());  
    }  
	
	/**
	 * @param request
	 * @return
	 * 登录验证
	 */
	@RequestMapping("/toLogin")
	public String toLogin(HttpServletRequest request)
	{
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		
		//返回地址
		String returnUrl="/login";
		//User user =userService.queryUserByUserName(username);
		
		//获取HttpSession验证码
		String verifyCode =(String) request.getSession().getAttribute("verifyCode");
		//获取用户输入的验证码
		String submitCode = WebUtils.getCleanParam(request,"verifyCode");
		
		System.out.println("用户输入的验证码是:"+submitCode+";系统生成的验证码是:"+verifyCode);
		
		if(StringUtils.isEmpty(submitCode)|| !StringUtils.equalsIgnoreCase(verifyCode, submitCode))
		{
			request.setAttribute("login_msg","验证码错误" );
			return returnUrl;
		}
		
	
		//根据获取的用户名和密码封装成Token
		UsernamePasswordToken token =new UsernamePasswordToken(username,password);
		//是否记住用户
		token.setRememberMe(true);
		//获取当前的subject
		Subject subject =SecurityUtils.getSubject();
		
		
		
		try {
			System.out.println("对用户:["+username+"]进行登录验证,验证开始...");
			//在调用了login方法后,SecurityManager会收到AuthenticationToken,并将其发送给已配置的Realm执行必须的认证检查  
			//每个Realm都能在必要时对提交的AuthenticationTokens作出反应  
			//所以这一步在调用login(token)方法时,它会走到MyRealm.doGetAuthenticationInfo()方法中,具体验证方式详见此方法  
			subject.login(token);
			System.out.println("对用户:["+username+"]进行登录验证,验证通过!");
			returnUrl="/main";
			
		}catch (UnknownAccountException e) {
			
			System.out.println("对用户:["+username+"]进行登录验证,验证未通过!错误:未知账号");
			request.setAttribute("login_msg","未知账号");
			
		}catch (IncorrectCredentialsException e) {
			System.out.println("对用户:["+username+"]进行登录验证,验证未通过!错误:密码错误");
			request.setAttribute("login_msg", "密码错误");
			
		}catch (LockedAccountException e) {
			System.out.println("对用户:["+username+"]进行登录验证,验证未通过!错误:账号被锁定");
			request.setAttribute("login_msg", "账号被锁定");
			
		}catch (ExcessiveAttemptsException e) {
			System.out.println("对用户:["+username+"]进行登录验证,验证未通过!错误:错误次数过多");
			request.setAttribute("login_msg", "密码或用户名输入错误次数过多");
			
		}catch (AuthenticationException e) {
			System.out.println("对用户:["+username+"]进行登录验证,验证未通过!错误:用户名或密码不正确");
			request.setAttribute("login_msg", "身份认证失败,用户名或密码不正确");
		}
		
		//验证是否登录成功
		if(subject.isAuthenticated())
		{
			System.out.println("用户:["+username+"]进行登录验证通过");
		}else
		{
			token.clear();
		}
		return returnUrl;
	}</span>


jsp页面 login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page isELIgnored="false"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
request.setAttribute("home", path);
%>
 <!DOCTYPE HTML>
<html lang="en-US"> 
<!-- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html> -->
<script type="text/javascript" src="<%=request.getContextPath()%>/res/login/prefixfree.min.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/res/js/jquery-1.11.3.min.js"></script>

<head>
	<meta charset="UTF-8">
	<title>用户登录</title>
	<link rel="stylesheet" href="<%=request.getContextPath()%>/res/login/login.css" type="text/css"></link>
	
	<script type="text/javascript">
		var home ="${home}";
		var msg ="${login_msg }";
		
		
		
		
					    
			$(function(){  //生成验证码         
			    $('#verifyCodeImage').click(function () {  
			    $(this).hide().attr('src', '<%=path%>/member/getVerifyCodeImage?' + Math.floor(Math.random()*100) ).fadeIn(); });      
			});   
			 
			window.onbeforeunload = function(){  
			    //关闭窗口时自动退出  
			    if(event.clientX>360&&event.clientY<0||event.altKey){     
			        alert(parent.document.location);  
			    }  
			};  
			             
			function changeCode() {  //刷新
			    $('#verifyCodeImage').hide().attr('src', '<%=path%>/member/getVerifyCodeImage?' + Math.floor(Math.random()*100) ).fadeIn();  
			    event.cancelBubble=true;  
			} 
			
	if(msg!="")
	{
		alert(msg);
	}
	</script>

	</head>
<body>

	   <div class="content">
           <form action="<%=request.getContextPath()%>/member/toLogin" method="post" class="login-form">
               <div class="username">
                   <input type="text" name="username" placeholder="[email protected]" autocomplete="on" />
                     <div id="loginMsg"></div>
                    <span class="user-icon icon">u</span>         
               </div>
               <div class="password">
                   <input type="password" name="password" placeholder="*******" />
                  
                   <span class="password-icon icon">p</span>
               </div>
               
               
              <div class="code-div">
                <input type="text" name="verifyCode" placeholder="请输入验证码" />  
		        <img id="verifyCodeImage"  src="<%=request.getContextPath()%>/member/getVerifyCodeImage"/> 
		      <!--   <a href="javascript:void(0)" onclick="changeCode()">看不清?换一张</a>  --> 
              </div> 
              	
               <div class="account-control">
                   <input type="checkbox" name="rememberMe" id="Remember me" value="Remember me" checked="checked" />
                   <label for="Remember me" data-on="c" class="check"></label>
                   <label for="Remember me" class="info">Remember me</label>
                  <!--  <input type="hidden" name="rememberMe" value="true"> -->
                   
                   <button type="submit">Login</button>
               </div>

		            <p class="not-registered">Not a registered user yet?<a>Sign up now!</a></p>
           
           </form>
          
	   </div>

	
</body>
</html>

效果图:

java-通过类实现验证码(一)_第1张图片

来源:http://blog.csdn.net/jadyer/article/details/12185725


main方法小测试一下

[java]  view plain copy print ?
  1. public static void main(String[] args) throws IOException {  
  2.     String verifyCode = generateTextCode(TYPE_NUM_ONLY, 4null);  
  3.     System.out.println("verifyCode=" + verifyCode);  
  4.     BufferedImage bufferedImage = generateImageCode(verifyCode, 903010true, Color.WHITE, Color.BLACK, null);  
  5.     ImageIO.write(bufferedImage, "JPEG"new File("C:/Users/Jadyer/Desktop/aa.jpg"));  
  6. }  

你可能感兴趣的:(java,验证码)