Generation and Application of Graphic Verification Code Developed by Java Web

  • 2021-07-24 11:35:13
  • OfStack

This paper gives an example of how to generate and use the graphic verification code developed by Java and Web. Share it for your reference. The details are as follows:

The main purpose of graphic verification code is to enhance the security and increase the difficulty for users to crack passwords by traversing all possibilities.

The use of graphic verification code includes the following three parts:

(1) Generation of graphic verification code;
② Use in the page;
Verification;

1. Generation of graphic verification code

Assuming that the graphic verification code is generated in Servlet, the basic process of generating it in JavaBean or JSP is the same. Design the following process:

(1) Set the document type of response;
Generating random code;
The random code is stored in session;
Generating pictures;
Drawing the random code on the memory picture;
6. Send the memory picture to the client;

1.1 Set the document type of the response

When responding to users, you need to set the document type. To generate pictures, the document type can be set to image/gif.
The setting mode in Servlet is: response. setContentType ("image/gif");
If used in JSP pages, you need to use: < %@ page contentType="image/gif"% >

1.2 Generate random code

It can be generated according to various random number generation strategies, and it is possible to set which characters the random code consists of and the length of the random code.

The random code characters given in this paper are all letters and numbers. The random code generation strategy used is provided by Random object. The reference code is as follows:

List of random code characters:


public static final char[] code = {'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',
  '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'};

Code to generate random code:


StringBuffer checkcode=new StringBuffer();
//  Per cycle 1 Second, generate 1 Bit 
for(int i=0;i<code_length;i++)
{
  int generated=(new Random()).nextInt(62);
  checkcode.append(code[generated]);
}

1.3 Save random code to session

In order to verify after the user submits the verification code, you need to save the generated verification code, which can be saved in session. In Servlet, you need to get the session object before using it.

Here is the reference code:


//  Save the generated verification code to session Medium 
HttpSession  session=request.getSession(true);
session.setAttribute("checkCode",checkcode.toString());

1.4 Generating Pictures

Use the BufferedImage class to create an object, and then use the drawing object to draw a picture. Here is the reference code:


//  Create a memory picture with the size and type of the picture as parameters 
BufferedImage image = new  BufferedImage(49,14,BufferedImage.TYPE_INT_RGB);
//  Get Graphics Handle 
Graphics  g = image.getGraphics();
//  Set the brush color 
// g.setColor(Color.yellow);
//  Painting background 
g.fillRect(0,1,49,12);
1.5  Display random codes on pictures 
 Refer to the following code: 
//  Set font color 
g.setColor(Color.black);
//  Draw verification code 
g.drawString(checkcode.toString(),4,11);
//  Image validation 
g.dispose();

1.6 Send the generated picture to the client

The reference code is as follows:

ImageIO.write(image, "JPEG", response.getOutputStream());

In this way, the generation of dynamic graphic verification code is completed.

2. Use graphic codes in the page:

In the same way as other graphics, use the < img src = "…" > Label. Assuming that the url-pattern value of the Servlet that generated the picture is checkcode, the code to load the picture in the page is as follows:

<img border=0 src="checkcode">

3. Verify

Obtain the verification code entered by the user, and then obtain the saved verification code from session, and compare it to determine whether it is the same, thus completing the verification.

I hope this article is helpful to everyone's JSP programming.


Related articles: