Java implementation of random verification code function instance code

  • 2020-04-01 02:13:27
  • OfStack

Now many systems of registration, login or release information modules are added to the random code function, is to avoid the use of automatic registration procedures or automatic release procedures.
Verification code is actually a random selection of some characters in the form of pictures to be displayed on the page, if the tomato garden xp system download and submit the operation at the same time need to submit the characters on the picture at the same time, if the submitted characters and server session to save different, the submission information is considered invalid. In order to avoid the automatic program to analyze the image, it usually generates some random interference lines on the image or distorts the characters, which increases the difficulty of automatic recognition.


package com.servlet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class ValidateCodeServlet extends HttpServlet
{
private static final long serialVersionUID = 1L;
//Width of captcha image.
private int width=60;
//Captcha image height.
private int height=20;
//Number of verification code characters
private int codeCount=4;
private int x=0;
//The font height
private int fontHeight;
private int codeY;
char[] codeSequence = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

public void init (a)  throws ServletException
{
//Get the initial information from web.xml
//The width of the
String strWidth=this.getInitParameter ( "width" ); 
//highly
String strHeight=this.getInitParameter ( "height" ); 
//The number of characters
String strCodeCount=this.getInitParameter ( "codeCount" ); 
//Converts the configured information to a numeric value
try
{
if ( strWidth!=null && strWidth.length ()! =0 ) 
{
width=Integer.parseInt ( strWidth ); 
}
if ( strHeight!=null && strHeight.length ()! =0 ) 
{
height=Integer.parseInt ( strHeight ); 
}
if ( strCodeCount!=null && strCodeCount.length ()! =0 ) 
{
codeCount=Integer.parseInt ( strCodeCount ); 
}
}
catch ( NumberFormatException e ) 
{}
x=width/ ( codeCount+1 ); 
fontHeight=height-2;
codeY=height-4;
}
protected void service ( HttpServletRequest req, HttpServletResponse resp ) 
throws ServletException, java.io.IOException {
//Define image buffer
BufferedImage buffImg = new BufferedImage ( 
width, height,BufferedImage.TYPE_INT_RGB ); 
Graphics2D g = buffImg.createGraphics (a); 
//Create a random number generator class
Random random = new Random (a); 
//Fill the image with white
g.setColor ( Color.WHITE ); 
g.fillRect ( 0, 0, width, height ); 
// Create the font, the font size should be based on the picture highly To order. 
Font font = new Font ( "Fixedsys", Font.PLAIN, fontHeight ); 
//Set the font.
g.setFont ( font ); 
//Picture frame.
g.setColor ( Color.BLACK ); 
g.drawRect ( 0, 0, width - 1, height - 1 ); 
//Random generation of 160 interference lines, so that the authentication code in the image is not easy to be detected by other programs.


Related articles: