The role of the verification code: Effectively prevent this problem from logging in to a specific registered user with a specific program violent cracking method to generate 4 random numbers and messages of the messy background, numbers and background colors change, server side brush ( Use history.go (-1) variable)
------------ Generate a verification code picture ----- image.jsp ------------------------ -------------------
<% @ page contenttype = "image / jpeg" import = "java.awt. *, java.awt.image. *, java.util. *, javax.imageio. *"%> <%! Color getrandcolor (int FC) , int BC) {// given random color random random = new random (); if (fc> 255) fc = 255; if (bc> 255) BC = 255; int R = fc random.nextint BC-FC); int g = fc random.nextint (BC-FC); int b = fc random.nextint (BC-FC); Return New Color (R, G, B);}%> <% / / Set the page does not cache Response.setHeader ("prgma", "no-cache"); response.setHeader ("cache-control", "no-cache"); Response.SetDateHeader ("Expires", 0);
// Create image in memory int width = 60, height = 20; bufferedimage image = new bufferedimage (width, height, bufferedimage.type_int_rgb);
/ / Get graphics context graphics g = image.getgraphics ();
/ / Generate random random random = new random ();
// Set the background color G.SetColor (GetrandColor (200, 250)); G.FillRect (0, 0, Width, Height);
// Set the font G.SetFont (New Font ("Times New Roman", Font.Plain, 18));
// Sea Border //g.SetColor (New Color ()); // g.drawRect (0, 0, Width-1, Height-1);
// Randomly generate 155 interference lines, enabling the authentication code in the image to detect G.SetColor (GetrandColor (160, 200)) in the image; for (int = 0; i <155; i ) {int x = random .Nextint (width); int y = random.nextint (Height); int xl = random.nextint (12); int yl = random.nextint (12); g.drawline (x, y, x xl, y YL);} // Take the randomly generated authentication code (4 digits) string srand = ""; for (int i = 0; i <4; i ) {string rand = string.valueof (random.nextint (10) ); SRAND = rand; // Display the authentication code to the image G.SetColor (New Color (20 random.nextint (110), 20 random.nextint (110), 20 random.nextint (110))) ); // The color of the call function is the same, it may be because the seed is too close, so only G.DrawString (RAND, 13 * i 6, 16);} // Deposit the authenticationSessionSession.setttribute ( "Rand", SRAND); // Image Effective g.dispose (); // Output Image to Page Imageio.write (Image, "JPEG", Response.getOutputStream ());%> ------ --------- Use the verification code picture -------- A.JSP ----------------------- ------------- <% @ page contenttype = "text / html; charset = GB2312"%>