程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> java適用驗證碼的完成代碼

java適用驗證碼的完成代碼

編輯:關於JAVA

java適用驗證碼的完成代碼。本站提示廣大學習愛好者:(java適用驗證碼的完成代碼)文章只能為提供參考,不一定能成為您想要的結果。以下是java適用驗證碼的完成代碼正文


本文為年夜家分享了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>

以上就是本文的全體內容,願望對年夜家的進修有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved