Joke Collection Website - Blessing messages - How to realize verification code with java

How to realize verification code with java

At present, many registration, login or information release modules of the system have added the function of random verification code to avoid using automatic registration programs or automatic release programs.

The verification code is actually to randomly select some characters and display them on the page in the form of pictures. If the characters on the picture need to be submitted at the same time, if the submitted characters are different from those stored in the server session, the submitted information is considered invalid. In order to avoid the automatic analysis of pictures by programs, some interference lines or distorted characters are usually randomly generated on pictures to increase the difficulty of automatic identification of verification codes.

Here we use java to realize the verification code.

& lt% @ page content type = image/JPEG import = Java awt * Java awt image * Java util * javax imageio * % & gt;

& lt%!

Color get rand color (int fc int BC) {//Get random colors in a given range.

Random Random = new Random();

if(fc & gt; )fc =;

if(BC & gt; )BC =;

int r = fc+random nextInt(BC fc);

int g = fc+random nextInt(BC fc);

int b = fc+random nextInt(BC fc);

Return the new color (r gb);

}

% & gt

& lt%

//Set the page not to be cached.

response set header(Pragma No cache);

Response setHeader (cache control without cache);

Response setDateHeader (expired);

//Create an image in memory

int width = height =

buffered image image = new buffered image(width height buffered image TYPE _ INT _ RGB);

//Get the graphic context

graphics g = image get graphics();

//Generate a random class

Random Random = new Random();

//Set the background color

g set color(getRandColor());

G fillRect (width and height);

//Set the font

G setFont (Times New Roman Font PLAIN));;

//Random interference lines make the authentication code in the image difficult to be detected by other programs.

g set color(getRandColor());

for(int I =; I <; i++)

{

int x = random nextInt(width);

int y = random nextInt(height);

int XL = random nextInt();

int yl = random nextInt();

g drawLine(x y x+XL y+yl);

}

//Take the randomly generated authentication code (number)

string codeList = ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz;

string sRand =;

for(int I =; I <; i++){

int a = random nextInt(codeList length());

string rand = codeList substring(a a+);

sRand+= rand;

//Display the verification code in the image.

g set Color(new Color(+random nextInt()+random nextInt()+random nextInt()); //The colors generated by calling the function are the same, probably because the seeds are too close, so they can only be generated directly.

G draw rope (rand * I+);

}

//Store the authentication code in the session.

session set attribute(rand sRand);

//Image takes effect

g dispose();

//Output the image to the page

ImageIO write(image JPEG response get output stream());

out clear();

out = pageContext push body();

Lishi Xinzhi/Article/program/Java/hx/20 13 1 1/25536