Generation and use of graphical verification code for Java Web Development

This paper describes the generation and use of graphic verification code in Java Web development. Share with you for your reference. The details are as follows:

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

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

① 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 the servlet, the basic process of generation in JavaBean or JSP is the same. Design the following process:

① Set the document type of response; ② Generate random code; ③ Save the random code into the session; ④ Generate pictures; ⑤ Draw the random code on the memory picture; ⑥ Send the memory picture to the client;

1.1 set the document type of 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 method 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 the composition of random code and the length of random code can be set.

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

Random code character list:

Code for generating random code:

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 the session. In the servlet, you need to obtain the session object before using it.

The following is the reference code:

1.4 generating pictures

Use the bufferedimage class to create an object, and then use the drawing object to draw. The following is the reference code:

1.6 send the generated picture to the client

Reference codes are as follows:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>