Background Java code:
Package Net.cloudsun.base.controller; Import Java.awt.Color;
Import Java.awt.Font;
Import Java.awt.Graphics2D;
Import Java.awt.image.BufferedImage;
Import java.io.IOException;
Import Java.io.PrintWriter;
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;
Import Net.cloudsun.base.service.CheckCodeService;
Import Net.cloudsun.base.service.UserService;
Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.stereotype.Controller;
Import org.springframework.web.bind.annotation.PathVariable;
Import org.springframework.web.bind.annotation.RequestMapping;
Import Org.springframework.web.bind.annotation.RequestMethod;
Import Org.springframework.web.bind.annotation.ResponseBody;
/**
* @author Bear Wave
*
*/
@Controller
@RequestMapping (value = "/xuan")
public class Verifycodeservlet extends HttpServlet {
Private static final long serialversionuid = 1L;
@Autowired
Private Checkcodeservice Checkcodeservice;
@Autowired
Private UserService UserService;
The width of the verification code picture.
private int width = 55;
The height of the verification code picture.
private int height = 30;
Number of verification code 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 ', ' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 '};
Char[] Codesequence_tele = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ',
' 9 '};
/**
* Initialize validation picture properties
*/
public void Initxuan () throws Servletexception {
Get the initial information from the Web.xml
Width
String strwidth = "70";
Height
String strheight = "30";
Number of characters
String Strcodecount = "4";
Converts the configured information to a numeric value
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 = "/verifycode/{id}", method = Requestmethod.get)
Public @ResponseBody Void Service (@PathVariable ("id") int ID,
HttpServletRequest req, HttpServletResponse resp)
Throws Servletexception, Java.io.IOException {
String Type_tele = Req.getparameter ("Type_tele");
Initxuan ();
Define Image Buffer
BufferedImage buffimg = new BufferedImage (width, height,
BUFFEREDIMAGE.TYPE_INT_RGB);
Graphics2D g = buffimg.creategraphics ();
Create a random number generator class
Random Random = new Random ();
To fill an image with white
G.setcolor (New Color (255, 193, 37));
G.fillrect (0, 0, width, height);
To create a font, the size of the font should be based on the height of the picture.
Font font = new Font ("Fixedsys", Font.Bold, Fontheight);
Sets the font.
G.setfont (font);
Draw a border.
G.setcolor (Color.Black);
G.drawrect (0, 0, width-1, height-1);
Randomly generated 160 lines of interference, so that the image of the authentication code is not easily detected by other programs.
G.setcolor (Color.Black);
for (int i = 0; i < i++) {
int x = random.nextint (width);
int y = random.nextint (height);
int xl = Random.nextint (5);
int yl = Random.nextint (5);
G.drawline (x, y, X + xl, y + yl);
}
Randomcode is used to save randomly generated CAPTCHA code so that the user can authenticate after logging in.
StringBuffer Randomcode = new StringBuffer ();
int red = 0, green = 0, blue = 0;
A validation code that randomly generates CODECOUNT numbers.
for (int i = 0; i < Codecount; i++) {
The number of validated codes generated randomly.
String Strrand = "";
if (' 1 ' = = Type_tele.charat (0)) {
Strrand = string.valueof (Codesequence[random.nextint (30)]);
}
if (' 2 ' = = Type_tele.charat (0)) {
Strrand = string.valueof (Codesequence[random.nextint (30)]);
}
Produces a random color component to construct a color value, so that the color values of each digit of the output will be different.
Red = random.nextint (255);
Green = Random.nextint (255);
Blue = Random.nextint (255);
Draws the validation code into the image with a randomly generated color.
G.setcolor (New Color (red, green, blue));
g.DrawString (Strrand, I * x, Codey);
The resulting four random numbers are grouped together.
Randomcode.append (Strrand);
}
Saves a four-bit number of Authenticode to the session.
HttpSession session = Req.getsession ();
Session.setattribute ("Validatecode" + ID, randomcode.tostring ());
Disables image caching.
Resp.setheader ("Pragma", "No-cache");
Resp.setheader ("Cache-control", "No-cache");
Resp.setdateheader ("Expires", 0);
Resp.setcontenttype ("Image/jpeg");
Outputs the image to the servlet output stream.
Servletoutputstream SOS = Resp.getoutputstream ();
Imageio.write (buffimg, "JPEG", SOS);
Sos.close ();
}
@RequestMapping (value = "/valicode", method = Requestmethod.post)
public void DoPost (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
Response.setcontenttype ("Text/html;charset=utf-8");
String id = request.getparameter ("id");
String Validatec = (string) request.getsession (). getattribute (
"Validatecode" + ID);//Get the verification code stored in session
String Verycode = Request.getparameter ("C"). toLowerCase ()//Get the verification code of the foreground input
PrintWriter out = Response.getwriter ();
if (Verycode = null | | "". Equals (Verycode)) {
Out.println ("1");
} else {
if (Validatec = = null) {
Out.println ("3");
} else {
if (Validatec.tolowercase (). Equals (Verycode)) {
Out.println ("0");
} else {
Out.println ("2");
}
}
}
Out.flush ();
Out.close ();
}
}
Foreground JSP code:
js the check is not said.
Effect Chart: