Package Com.sec.control;
Import Java.awt.Color;
Import Java.awt.Font;
Import Java.awt.Graphics2D;
Import Java.awt.geom.AffineTransform;
Import Java.awt.image.BufferedImage;
Import java.io.IOException;
Import Java.util.Random;
Import Javax.imageio.ImageIO;
Import javax.servlet.ServletException;
Import Javax.servlet.ServletOutputStream;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import javax.servlet.http.HttpSession;
public class Verifycodeservlet extends HttpServlet {
Private static final long serialversionuid = 1668221467610842602L;
//Verify the width of the code picture
Private int width =;
The height of the captcha picture is
private int height =;
Number of CAPTCHA characters
Private int codecount = 4;
private int x = 0;
//font height
private int fontheight;
private int Codey;
Char[] codesequence = {' 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 '};
public void Initxuan () throws Servletexception {
Getting initial information from Web. xml
Width
String strwidth = "80";
Height
String strheight = "30";
Number of characters
String Strcodecount = "4";
Converting configuration information to numeric values
try {
if (strwidth! = null && strwidth.length ()! = 0) {
width = integer.parseint (strwidth);
}
if (strheight! = null && strheight.length ()! = 0) {
Height = integer.parseint (strheight);
}
if (strcodecount! = null && strcodecount.length ()! = 0) {
Codecount = Integer.parseint (Strcodecount);
}
} catch (NumberFormatException e) {
}
x = width/(codecount + 1);
Fontheight = height-2;
Codey = height-4;
}
@RequestMapping (value= "Xuan/verifycode", Method=requestmethod.get)//get request
@Override
protected void Service (HttpServletRequest req, HttpServletResponse resp)
Throws Servletexception, IOException {
Initxuan ();
Defining the image Buffer
BufferedImage buffimg = new BufferedImage (width, height,
BUFFEREDIMAGE.TYPE_INT_RGB);
Graphics2D g = buffimg.creategraphics ();
Create a random number generation class
Random random = new random ();
Set a random background color
Color color = new Color (random.nextint (255), Random.nextint (255),
Random.nextint (255));
Fill a dark background
G.setcolor (Color.darker ());
Fills a brighter background
G.setcolor (Color.brighter ());
//Fill the image with pink
//g.setcolor (color.pink);
White
G.setcolor (color.white);
Rectangle
G.fillrect (0, 0, width, height);
To create a font, the size of the font should depend on the height of the image.
//font font = new Font ("Fixedsys", font.plain,fontheight);
Font font = new Font ("Fixedsys", Font.Bold, Fontheight);
//Set font
G.setfont (font);
Draw Border
G.setcolor (Color.gray);
G.drawrect (0, 0, width-1, height-1);
//Randomly generate 40 points and 40 interference lines, so that the image authentication code is not easily detected by other programs
G.setcolor (Color.light_gray);
for (int i = 0; i < i++) {
int x = random.nextint (width);
int y = random.nextint (height);
int xl = Random.nextint (50);
int yl = Random.nextint (50);
//Dots
G.drawoval (x, Y, 1, 1);
Line
G.drawline (x, y, X + xl, y + yl);
}
The
//Randomcode is used to save randomly generated verification codes for user login verification
StringBuffer Randomcode = new StringBuffer ();
int red = 0, green = 0, blue = 0;
//Random generation of Codecount number verification code
for (int i = 0; i < Codecount; i++) {
//randomly generated CAPTCHA number
//String Strrand =
//Strin G.valueof (Codesequence[random.nextint (codesequence.length-1));
String Strrand = string.valueof (Codesequence[random.nextint (62)]);
//text warp settings
AffineTransform fontat = new AffineTransform ();
int rotate = Random.nextint (25);
Fontat.rotate (Random.nextboolean ()? Math.toradians (rotate):-math
. Toradians (ROTATE/2));
Font FX = new Font (new string[] {"Times New Roman", "Verdana",
"Arial"}[random.nextint (2)], Random.nextint (5),
20 + random.nextint (+)). Derivefont (Fontat);
G.setfont (FX);
Generates a random color component to construct a color value so that each digit of the output has a different color value
Red = random.nextint (255);
Green = Random.nextint (255);
Blue = Random.nextint (255);
Draws a randomly generated color to the image of the CAPTCHA
G.setcolor (New Color (red, green, blue));
g.DrawString (Strrand, (i + 1) * x-8, Codey);
Combine the resulting four random numbers
Randomcode.append (Strrand);
}
Save the four-digit verification code to the session
HttpSession session = Req.getsession ();
Session.setattribute ("Validatecode", randomcode.tostring ());
Disable Image Caching
Resp.setheader ("Pragma", "No-cache");
Resp.setheader ("Cache-control", "No-cache");
Resp.setdateheader ("Expires", 0);
Resp.setcontenttype ("Imag/jpeg");
outputting an image to the servlet output stream
Servletoutputstream SOS = Resp.getoutputstream ();
Imageio.write (buffimg, "JPEG", SOS);
Sos.close ();
}
}
Java verification Code increases the difficulty and interference effect of user reading verification code