欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java实用验证码的实现代码

程序员文章站 2024-03-09 09:21:35
本文为大家分享了java实用验证码的实现代码,供大家参考,具体内容如下 1、validcode     ...

本文为大家分享了java实用验证码的实现代码,供大家参考,具体内容如下

1、validcode     

package validimg;
 
import java.awt.color;
import java.io.ioexception;
import java.util.random;
import javax.servlet.servletexception;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import javax.servlet.http.httpsession;
import com.github.bingoohuang.patchca.color.colorfactory;
import com.github.bingoohuang.patchca.custom.configurablecaptchaservice;
import com.github.bingoohuang.patchca.filter.predefined.curvesripplefilterfactory;
import com.github.bingoohuang.patchca.filter.predefined.diffuseripplefilterfactory;
import com.github.bingoohuang.patchca.filter.predefined.doubleripplefilterfactory;
import com.github.bingoohuang.patchca.filter.predefined.marbleripplefilterfactory;
import com.github.bingoohuang.patchca.filter.predefined.wobbleripplefilterfactory;
import com.github.bingoohuang.patchca.utils.encoder.encoderhelper;
import com.github.bingoohuang.patchca.word.randomwordfactory;
 
public class validcode extends httpservlet {
  private static configurablecaptchaservice cs = new configurablecaptchaservice();
  private static random random = new random();
  static {
//    cs.setcolorfactory(new singlecolorfactory(new color(25, 60, 170)));
  
    cs.setcolorfactory(new colorfactory() {
      @override
      public color getcolor(int x) {
        int[] c = new int[3];
        int i = random.nextint(c.length);
        for (int fi = 0; fi < c.length; fi++) {
          if (fi == i) {
            c[fi] = random.nextint(71);
          } else {
            c[fi] = random.nextint(256);
          }
        }
        return new color(c[0], c[1], c[2]);
      }
    });
    randomwordfactory wf = new randomwordfactory();
    wf.setcharacters("23456789abcdefghigkmnpqrstuvwxyzabcdefghigklmnpqrstuvwxyz");
    wf.setmaxlength(4);
    wf.setminlength(4);
    cs.setwordfactory(wf);
  }
 
 
  public void doget(httpservletrequest request, httpservletresponse response)
      throws servletexception, ioexception {
 
    switch (random.nextint(5)) {
    case 0:
      cs.setfilterfactory(new curvesripplefilterfactory(cs
          .getcolorfactory()));
      break;
    case 1:
      cs.setfilterfactory(new marbleripplefilterfactory());
      break;
    case 2:
      cs.setfilterfactory(new doubleripplefilterfactory());
      break;
    case 3:
      cs.setfilterfactory(new wobbleripplefilterfactory());
      break;
    case 4:
      cs.setfilterfactory(new diffuseripplefilterfactory());
      break;
    }
    httpsession session = request.getsession(false);
    if (session == null) {
      session = request.getsession();
    }
    setresponseheaders(response);
    string token = encoderhelper.getchallangeandwriteimage(cs, "png",
        response.getoutputstream());
    session.setattribute("validcode", token);
    system.out.println("当前的sessionid=" + session.getid() + ",验证码= ," + token);
 
  }
 
  protected void setresponseheaders(httpservletresponse response) {
    response.setcontenttype("image/png");
    response.setheader("cache-control", "no-cache, no-store");
    response.setheader("pragma", "no-cache");
    long time = system.currenttimemillis();
    response.setdateheader("last-modified", time);
    response.setdateheader("date", time);
    response.setdateheader("expires", time);
  }
 
  public void dopost(httpservletrequest request, httpservletresponse response)
      throws servletexception, ioexception {
 
  }
 
 
}

2.xml代码

<dependency>
  <groupid>com.github.bingoohuang</groupid>
  <artifactid>patchca</artifactid>
  <version>0.0.1</version>
 </dependency>
 
<servlet>
 <servlet-name>validcode</servlet-name>
 <servlet-class>validimg.validcode</servlet-class>
</servlet>
<servlet-mapping>
 <servlet-name>validcode</servlet-name>
 <url-pattern>/validimg.jpg</url-pattern>
</servlet-mapping>

以上就是本文的全部内容,希望对大家的学习有所帮助。