1. Overview of picture verification code:
Many websites have the implementation
effect:
To improve system security
With the verification code, we can ask the user to enter the text on the picture after entering username, password and other information. After the user submits, the system will first extract the verification code just generated from the session and compare it with the verification code entered by the user. If the comparison is equal, it means that the user logged in from the login interface. Otherwise, it means that the user is illegal. We use the verification code to ensure that the system must be logged in successfully before using it, so as to avoid the user directly entering the page to be accessed in the address bar.
In other words, using the verification code, the user must first log in from the login interface.
2. Verification implementation method
Two key classes are used, and these two classes are related to the output of the picture.
BufferedImage im = new BufferedImage(60,20,BufferedImage.TYPE_INT_RGB);//The first parameter im represents an image object//JPG represents an image output type//response.getOutputStream() represents a response output stream, that is, if you access this servlet. The servlet will display the image to you 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 format
BufferedImage im = new BufferedImage(60,20,BufferedImage.TYPE_INT_RGB);//The first parameter im represents an image object//JPG represents an image output type//response.getOutputStream() represents a response output stream, that is, if you access this servlet. The servlet will display the image to you 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 the color of the entire picture g.fillRect(0, 0, 60, 20);
4. Output numbers to the picture
g.setColor(new Color(rm.nextInt(255),rm.nextInt(255),rm.nextInt(255)));g.setFont(new Font("Chinese Lishu",Font.BOLD|Font.ITALIC,28));g.drawString("8", 1, 18);5. Random 4 digits
// Randomly generate 4-digit numbers 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
String str = "Those who have thunder in their chest and faces like a flat lake can be called a general"; 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 this verification code in the page:
<img src="/ImageServlet">
8. Save the numbers for login comparison
//Save the obtained four numbers into the session so that when the user logs in, it is used to compare request.getSession().setAttribute("piccode", sbf.toString());9. Login verification
First, it is necessary to verify whether the user exists in the database. If it exists, it is also necessary to verify whether the entered verification code is consistent.
After the verification is successful, it needs to be forwarded to the relevant operation page.
Code example:
boolean b_exist = login.validate(username,passwd);//If the user exists if(b_exist){ String pic = ""+request.getSession().getAttribute("piccode"); //Compare verification code if(!pic.equals("") && pic.equals(code)){ //Storage user information into the session for other use request.getSession().setAttribute("username", username); response.sendRedirect("index.jsp"); }}The above is all the content of this article. I hope that the content of this article will be of some help to everyone’s study or work. I also hope to support Wulin.com more!