zoukankan      html  css  js  c++  java
  • Java验证码

    下面这段代码可用于Jsp+Servle+JavaBean中做验证码:

      1 <%@ page contentType="image/jpeg" import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*" %>
      2 <%!
      3 Color getRandColor(int fc,int bc){//给定范围获得随机颜色  
      4         Random random = new Random();
      5         if(fc>255) fc=255;
      6         if(bc>255) bc=255;
      7         int r=fc+random.nextInt(bc-fc);
      8         int g=fc+random.nextInt(bc-fc);
      9         int b=fc+random.nextInt(bc-fc);
     10         return new Color(r,g,b);
     11         }
     12 %>
     13 <%
     14 //设置页面不缓存  
     15 response.setHeader("Pragma","No-cache");
     16 response.setHeader("Cache-Control","no-cache");
     17 response.setDateHeader("Expires", 0);
     18 
     19 // 在内存中创建图象  
     20 int width=60, height=20;
     21 BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
     22 
     23 // 获取图形上下文  
     24 Graphics g = image.getGraphics();
     25 
     26 //生成随机类  
     27 Random random = new Random();
     28 
     29 // 设定背景色  
     30 g.setColor(getRandColor(200,250));
     31 g.fillRect(0, 0, width, height);
     32 
     33 //设定字体  
     34 g.setFont(new Font("Times New Roman",Font.PLAIN,18));
     35 
     36 //画边框  
     37 //g.setColor(new Color());  
     38 //g.drawRect(0,0,width-1,height-1);  
     39 
     40 
     41 // 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到  
     42 g.setColor(getRandColor(160,200));
     43 for (int i=0;i<155;i++)
     44 {
     45     int x = random.nextInt(width);
     46     int y = random.nextInt(height);
     47         int xl = random.nextInt(12);
     48         int yl = random.nextInt(12);
     49     g.drawLine(x,y,x+xl,y+yl);
     50 }
     51 // 取随机产生的认证码(4位数字)  
     52 //String rand = request.getParameter("rand");  
     53 //rand = rand.substring(0,rand.indexOf("."));  
     54 String sRand="";
     55 for (int i=0;i<4;i++){
     56     String rand=String.valueOf(random.nextInt(10));
     57     sRand+=rand;
     58     // 将认证码显示到图象中  
     59     g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));//调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成  
     60     g.drawString(rand,13*i+6,16);
     61 }
     62 // 将认证码存入SESSION  
     63 session.setAttribute("ccode",sRand);
     64 // 图象生效  
     65 g.dispose();
     66 // 输出图象到页面  
     67 ImageIO.write(image, "JPEG", response.getOutputStream());
     68 out.clear();
     69 out = pageContext.pushBody();
     70 %>

    下面是通过Struts2做的验证码。其实原理是一样的,只是列在这里好随时看看:

    1、login.jsp页面程序

      1 <script type="text/javascript">
      2 function changeValidateCode(obj) {
      3 //获取当前的时间作为参数,无具体意义    
      4 var timenow = new Date().getTime();
      5 //每次请求需要一个不同的参数,否则可能会返回同样的验证码    
      6 //这和浏览器的缓存机制有关系,也可以把页面设置为不缓存,这样就不用这个参数了。    
      7 obj.src="rand.action?d="+timenow;
      8 }
      9 </script>

    在表单中添加下面这句话:

      1 <s:text name="random"></s:text>  2 <s:textfield name="rand" size="5"></s:textfield>
      3 <img src="rand.action"  onclick="changeValidateCode(this)" title="点击图片刷新验证码"/>

    2:RandomNumUtil.java 生成验证码的类文件


      1 public class RandomNumUtil {
      2 private ByteArrayInputStream image;//图像    
      3 private String str;//验证码    
      4 
      5 private RandomNumUtil(){
      6 init();//初始化属性    
      7 }
      8 /*
      9 * 取得RandomNumUtil实例
     10 */
     11 public static RandomNumUtil Instance(){
     12 return new RandomNumUtil();
     13 }
     14 /*
     15 * 取得验证码图片
     16 */
     17 public ByteArrayInputStream getImage(){
     18 return this.image;
     19 }
     20 /*
     21 * 取得图片的验证码
     22 */
     23 public String getString(){
     24 return this.str;
     25 }
     26 
     27 private void init() {
     28 // 在内存中创建图象    
     29 int width=85, height=20;
     30 BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
     31 // 获取图形上下文    
     32 Graphics g = image.getGraphics();
     33 // 生成随机类    
     34 Random random = new Random();
     35 // 设定背景色    
     36 g.setColor(getRandColor(200,250));
     37 g.fillRect(0, 0, width, height);
     38 // 设定字体    
     39 g.setFont(new Font("Times New Roman",Font.PLAIN,18));
     40 // 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到    
     41 g.setColor(getRandColor(160,200));
     42 for (int i=0;i<155;i++)
     43 {
     44 int x = random.nextInt(width);
     45 int y = random.nextInt(height);
     46 int xl = random.nextInt(12);
     47 int yl = random.nextInt(12);
     48 g.drawLine(x,y,x+xl,y+yl);
     49 }
     50 // 取随机产生的认证码(6位数字)    
     51 String sRand="";
     52 for (int i=0;i<6;i++){
     53 String rand=String.valueOf(random.nextInt(10));
     54 sRand+=rand;
     55 // 将认证码显示到图象中    
     56 g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));
     57 // 调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成    
     58 g.drawString(rand,13*i+6,16);
     59 }
     60 //赋值验证码   
     61 this.str=sRand;
     62 
     63 //图象生效    
     64 g.dispose();
     65 ByteArrayInputStream input=null;
     66 ByteArrayOutputStream output = new ByteArrayOutputStream();
     67 try{
     68 ImageOutputStream imageOut = ImageIO.createImageOutputStream(output);
     69 ImageIO.write(image, "JPEG", imageOut);
     70 imageOut.close();
     71 input = new ByteArrayInputStream(output.toByteArray());
     72 }catch(Exception e){
     73 System.out.println("验证码图片产生出现错误:"+e.toString());
     74 }
     75 
     76 this.image=input;/* 赋值图像 */
     77 }
     78 /*
     79 * 给定范围获得随机颜色
     80 */
     81 private Color getRandColor(int fc,int bc){
     82 Random random = new Random();
     83 if(fc>255) fc=255;
     84 if(bc>255) bc=255;
     85 int r=fc+random.nextInt(bc-fc);
     86 int g=fc+random.nextInt(bc-fc);
     87 int b=fc+random.nextInt(bc-fc);
     88 return new Color(r,g,b);
     89 }
     90 }

    3:RandomAction.java  生成验证码的action程序

      1 public class RandomAction extends ActionSupport{
      2 private ByteArrayInputStream inputStream;
      3 public String execute() throws Exception{
      4 RandomNumUtil rdnu=RandomNumUtil.Instance();
      5 this.setInputStream(rdnu.getImage());//取得带有随机字符串的图片    
      6 ActionContext.getContext().getSession().put("random", rdnu.getString());//取得随机字符串放入HttpSession    
      7 return SUCCESS;
      8 }
      9 public void setInputStream(ByteArrayInputStream inputStream) {
     10 this.inputStream = inputStream;
     11 }
     12 public ByteArrayInputStream getInputStream() {
     13 return inputStream;
     14 }
     15 }


    4:LoginAction.java 验证验证码的action

      1 private String rand; //表单中的rand   
      2 public String getRand() {
      3 return rand;
      4 }
      5 public void setRand(String rand) {
      6 this.rand = rand;
      7 }
      8 //从session中取出RandomAction.java 中生成的验证码random   
      9 String arandom=(String)(ActionContext.getContext().getSession().get("random"));
     10 
     11 //下面就是将session中保存验证码字符串与客户输入的验证码字符串对比了   
     12 if(arandom.equals(this.getRand())) {
     13 ActionContext.getContext().getSession().put("user", this.getUsername());
     14 return SUCCESS;
     15 }
     16 else {
     17 return ERROR;
     18 }


    5:配置文件


      1 <!-- Random验证码 -->
      2 <action name="rand" class="com.mxl.rand.RandomAction">
      3        <result type="stream">
      4             <param name="contentType">image/jpeg</param>
      5             <param name="inputName">inputStream</param>
      6        </result>
      7    </action>





    注:本文来源于:cnblogs:牛奶、不加糖

  • 相关阅读:
    【NLP CS224N笔记】汇总
    【NLP CS224N笔记】Lecture 2
    论文摘记 2017.5
    FAST UA API
    FAST Hello World
    NetMagic Simple Overview
    论文摘记 2017.4.25-4.30
    LLDP协议、STP协议 笔记
    FAST:通过Floodlight控制器下发流表
    FAST:NetMagic交换机 与 Floodlight控制器 连接实战
  • 原文地址:https://www.cnblogs.com/ios9/p/7475721.html
Copyright © 2011-2022 走看看