Image Verification Code overview and implementation steps, verification code overview steps

Source: Internet
Author: User

Image Verification Code overview and implementation steps, verification code overview steps

I. Image Verification Code Overview:
Many websites have this implementation.

Purpose:
To improve system security
With the verification code, we can ask the user to enter the user name, password, and other information, at the same time enter the text on the image, the user submits

The system will first extract the generated verification code from the session and compare it with the verification code entered by the user.

Equivalent, indicating that the user logs on from the logon interface. Otherwise, the user is invalid.


The verification code is used to ensure that the system can be used only after successful logon, so that users can directly access the address bar.

Enter the page to access

That is to say, to use the verification code, the user must first log on to


Ii. Verification Implementation Method
Two key classes are used, which are related to the output of images.


BufferedImage im = new BufferedImage (60, 20, BufferedImage. TYPE_INT_RGB );
// The first parameter im represents an image object
// JPG indicates the image output type
// Response. getOutputStream () represents the output stream of a response, that is, you access this servlet.

Servlet will show you an image
ImageIO. write (im, "JPG", response. getOutputStream ());



3. Implementation steps
1. Use BufferedImage to generate an image, then use ImageIO output, and specify it as JPG
BufferedImage im = new BufferedImage (60, 20, BufferedImage. TYPE_INT_RGB );
// The first parameter im represents an image object
// JPG indicates the image output type
// Response. getOutputStream () represents the output stream of a response, that is, you access this servlet.

Servlet will show you an image
ImageIO. write (im, "JPG", response. getOutputStream ());



2. Get the image Drawing Object
Graphics g = im. getGraphics ();

3. Fill in the drawing Area
Random rm = new Random ();
Color c = new Color (rm. nextInt (255), rm. nextInt (255), rm. nextInt (255 ));
G. setColor (c );
// Fill in the color of the entire image
G. fillRect (0, 0, 60, 20 );



4. Output numbers to the image
G. setColor (new Color (rm. nextInt (255), rm. nextInt (255), rm. nextInt (255 )));
G. setFont (new Font (" 文 ", Font. BOLD | Font. ITALIC, 28 ));
G. drawString ("8", 1, 18 );

5. Random 4-digit number

// Four digits are randomly generated.
For (int I = 0; I <4; I ++ ){
G. setColor (new Color (rm. nextInt (255), rm. nextInt (255), rm. nextInt (255 )));
G. setFont (new Font ("Gungsuh", Font. BOLD | Font. ITALIC, 22 ));
G. drawString ("" + rm. nextInt (10), (I * 15) + 2, 18 );
}


6. randomly generate Chinese Characters
String str = "the chest is thunderous and faces such as Pinghu can worship the generals ";
For (int I = 0; I <4; I ++ ){
G. setColor (new Color (rm. nextInt (255), rm. nextInt (255), rm. nextInt (255 )));
G. setFont (new Font ("Gungsuh", Font. BOLD | Font. ITALIC, 15 ));
G. drawString ("" + str. charAt (rm. nextInt (str. length (), (I * 15) + 2, 18 );
}


7. How to introduce the verification code on the page:


8. Save the number for Logon comparison
// Save the four digits to the session for comparison when the user logs on
Request. getSession (). setAttribute ("piccode", sbf. toString ());


9. logon Verification
First, verify that the user exists in the Database. If yes, verify that the entered verification code is consistent.
After the verification is successful, you need to forward it to the relevant operation page.

Code example:

Boolean B _exist = login. validate (username, passwd );
// If this user exists
If (B _exist ){
String pic = "" + request. getSession (). getAttribute ("piccode ");
// Compare the verification code
If (! Pic. equals ("") & pic. equals (code )){
// Store user information into the session for other purposes
Request. getSession (). setAttribute ("username", username );

Response. sendRedirect ("index. jsp ");
}
}
Iv. References:
Http://blog.163.com/jackie_howe/blog/static/199491347201272410322219/






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.