자바 랜 덤 생 성 인증 코드 이미지 구현

6847 단어 자바인증번호
인증번호
인증 코드(CAPTCHA)는'Complete Automated Public Turing test to tell Computers and Humans Apart'(컴퓨터 와 인간 을 자동 으로 구분 하 는 툴 링 테스트)의 약자 다.컴퓨터 가 CAPTCHA 의 질문 을 풀 수 없 기 때문에 질문 에 답 한 사용 자 는 인간 으로 볼 수 있다.
역할.
악의 적 으로 비밀 번 호 를 풀 거나 표를 긁 거나 포럼 에 물 을 붓 거나 페이지 를 긁 는 것 을 방지 하 다.
특정한 해커 가 특정한 등록 사용자 에 게 특정한 프로그램 폭력 으로 해결 하 는 방식 으로 끊 임 없 는 로그 인 시 도 를 하 는 것 을 효과적으로 방지 할 수 있다.실제로 인증 코드 를 사용 하 는 것 은 현재 많은 사이트 에서 통용 되 는 방식(예 를 들 어 상업 유치 은행 의 인터넷 개인 은행,바 이 두 커 뮤 니 티)이다.우 리 는 비교적 간단 한 방식 으로 이 기능 을 실현 했다.로그 인 은 귀 찮 지만 네티즌 들 의 비밀번호 안전 에 도 필요 하고 중요 하 다.그러나 우 리 는 여러분 에 게 자신의 비밀 번 호 를 잘 보호 하고 숫자,자모,기 호 를 포함 한 6 자리 이상 의 비밀 번 호 를 사용 하 라 고 일 깨 워 드 립 니 다.1234 와 같은 간단 한 비밀번호 나 사용자 이름과 같 고 유사 한 비밀 번 호 를 사용 하지 마 세 요.당신 의 계 정 이 다른 사람 에 게 도용 되 어 불필요 한 번 거 로 움 을 가 져 오지 않도록 해 야 합 니 다.
인증 코드 는 보통 일부 라인 과 불규칙 한 문자 로 구성 되 는데 주요 역할 은 일부 해커 들 이 비밀 번 호 를 데이터 화 하 는 것 을 방지 하기 위해 서 이다.
분류 하 다.
현재 흔히 볼 수 있 는 인증 코드 는 주로
gif 인증 코드
핸드폰 문자 인증 코드
핸드폰 음성 인증번호
비디오 인증번호
실례
여 기 는 자바 로 인증 코드 를 만 든 그림 일 뿐 웹 페이지 에서 작 동 하지 않 습 니 다.

package com.xn;
 
import javax.imageio.ImageIO; 
import java.awt.*; 
import java.awt.image.BufferedImage; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.OutputStream; 
import java.util.Date; 
import java.util.Random; 
 
/** 
 *         
 * 
 * @leo 
 */ 
public class ValidateCode { 
 //      。 
 private int width = 160; 
 //      。 
 private int height = 40; 
 //         
 private int codeCount = 5; 
 //         
 private int lineCount = 150; 
 //     
 private String code = null; 
 //      Buffer 
 private BufferedImage buffImg = null; 
 
 //      ,  0(  ) O(  )     (   1 L     ,     ) 
 private char[] codeSequence = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 
   'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 
   'X', 'Y', 'Z', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; 
 
 /** 
  *       ,       
  */ 
 public ValidateCode() { 
  this.createCode(); 
 } 
 
 /** 
  * @param width     
  * @param height     
  */ 
 public ValidateCode(int width, int height) { 
  this.width = width; 
  this.height = height; 
  this.createCode(); 
 } 
 
 /** 
  * @param width      
  * @param height     
  * @param codeCount      
  * @param lineCount       
  */ 
 public ValidateCode(int width, int height, int codeCount, int lineCount) { 
  this.width = width; 
  this.height = height; 
  this.codeCount = codeCount; 
  this.lineCount = lineCount; 
  this.createCode(); 
 } 
 
 public void createCode() { 
  int x = 0, fontHeight = 0, codeY = 0; 
  int red = 0, green = 0, blue = 0; 
 
  x = width / (codeCount + 2);//       (         ) 
  fontHeight = height - 2;//      
  codeY = height - 4; 
 
  //   buffer 
  buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 
  Graphics2D g = buffImg.createGraphics();  
   
  /*//            
  g.setColor(Color.WHITE); 
  g.fillRect(0, 0, width, height);*/
  //              
  buffImg = g.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT); 
  g.dispose(); 
  g = buffImg.createGraphics(); 
  //          
   
  //   BasicStroke JDK            ,           ,    drawPanel             。 
  g.setColor(new Color(255, 0, 0)); 
  g.setStroke(new BasicStroke(1f)); 
  g.fillRect(128, 128, width, height);
  
  //       
  Random random = new Random(); 
      //      、    、     
    Font font = new Font("    ",Font.PLAIN, fontHeight);
 
  g.setFont(font); 
 
  for (int i = 0; i < lineCount; i++) { 
   //             
   int xs = random.nextInt(width);//x     
   int ys = random.nextInt(height);//y     
   int xe = xs + random.nextInt(width / 8);//x     
   int ye = ys + random.nextInt(height / 8);//y     
 
   //         ,                 。 
   red = random.nextInt(255); 
   green = random.nextInt(255); 
   blue = random.nextInt(255); 
   g.setColor(new Color(red, green, blue)); 
   g.drawLine(xs, ys, xe, ye); 
  } 
 
  // randomCode           
  StringBuffer randomCode = new StringBuffer(); 
  //     codeCount       。 
  for (int i = 0; i < codeCount; i++) { 
   String strRand = String.valueOf(codeSequence[random.nextInt(codeSequence.length)]); 
   //         ,                。 
   red = random.nextInt(255); 
   green = random.nextInt(255); 
   blue = random.nextInt(255); 
   //      
   //g.setColor(new Color(252, 145, 83));
   g.setColor(new Color(red, green, blue)); 
   g.drawString(strRand, (i + 1) * x, codeY); 
   //               。 
   randomCode.append(strRand); 
  } 
  //             Session 。 
  code = randomCode.toString(); 
 } 
 
 public void write(String path) throws IOException { 
  OutputStream sos = new FileOutputStream(path); 
  this.write(sos); 
 } 
 
 public void write(OutputStream sos) throws IOException { 
  ImageIO.write(buffImg, "png", sos); 
  sos.close(); 
 } 
 
 public BufferedImage getBuffImg() { 
  return buffImg; 
 } 
 
 public String getCode() { 
  return code; 
 } 
 
 /** 
  *     ,     d  
  * @param args 
  */ 
 public static void main(String[] args) { 
  ValidateCode vCode = new ValidateCode(160,40,5,150); 
  try { 
   String path="D:/"+new Date().getTime()+".png"; 
   System.out.println(vCode.getCode()+" >"+path); 
   vCode.write(path); 
  } catch (IOException e) { 
   e.printStackTrace(); 
  } 
 } 
} 
테스트:


인증 코드 에 관 한 더 많은 글 은 클릭 하여 보 세 요《자바 인증 코드》
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기