Jfinal Add Jcaptcha Verification Code implementation method _java

Source: Internet
Author: User

Copy Code code as follows:

Package Com.g.core.common.jcaptcha;

Import Java.awt.Color;
Import Java.awt.Font;

Import Com.octo.captcha.component.image.backgroundgenerator.BackgroundGenerator;
Import Com.octo.captcha.component.image.backgroundgenerator.FileReaderRandomBackgroundGenerator;
Import Com.octo.captcha.component.image.color.RandomListColorGenerator;
Import Com.octo.captcha.component.image.fontgenerator.FontGenerator;
Import Com.octo.captcha.component.image.fontgenerator.RandomFontGenerator;
Import Com.octo.captcha.component.image.textpaster.DecoratedRandomTextPaster;
Import Com.octo.captcha.component.image.textpaster.TextPaster;
Import Com.octo.captcha.component.image.textpaster.textdecorator.TextDecorator;
Import Com.octo.captcha.component.image.wordtoimage.ComposedWordToImage;
Import Com.octo.captcha.component.image.wordtoimage.WordToImage;
Import Com.octo.captcha.component.word.wordgenerator.RandomWordGenerator;
Import Com.octo.captcha.component.word.wordgenerator.WordGenerator;
Import Com.octo.captcha.engine.image.ListImageCaptchaEngine;
Import Com.octo.captcha.image.gimpy.GimpyFactory;

/**
* Generate Verification Code picture
*/

public class Jcaptchaengine extends Listimagecaptchaengine {

public static final String Image_captcha_key = "Imagecaptcha";//Imagecaptcha object stored in session KEY
public static final String captcha_input_name = "J_captcha";//Authentication code Enter form name
public static final String Captcha_image_url = "/captcha.jpg";//Verify Code picture URL
private static final Integer Min_word_length = 4;//Verification Code Minimum length
private static final Integer Max_word_length = 4;//Verification Code Maximum length
private static final Integer Image_height = 28;//verification code Picture height
private static final Integer image_width = 80;//verification Code picture width
private static final Integer min_font_size = 16;//Authenticode Minimum font
private static final Integer max_font_size = 16;//authentication Code Max font
private static final String Random_word = "abcdefghijklmnopqrstuvwxyz";//random character
private static final String image_path = "./captcha/";//random background picture path

Verification Code Random Font
private static final font[] Random_font = new font[] {
New Font ("Nyala", Font.Bold, Min_font_size),
New Font ("Arial", Font.Bold, Min_font_size),
New Font ("Bell MT", Font.Bold, Min_font_size),
New Font ("Credit Valley", Font.Bold, Min_font_size),
New Font ("Impact", Font.Bold, Min_font_size)
};

Validation code random Color
private static final color[] Random_color = new color[] {
New Color (255, 255, 255),
New Color (255, 220, 220),
New Color (220, 255, 255),
New Color (220, 220, 255),
New Color (255, 255, 220),
New Color (220, 255, 220)
};

Generate Verification Code
@Override
protected void Buildinitialfactories () {

Randomlistcolorgenerator randomlistcolorgenerator = new Randomlistcolorgenerator (Random_color);

Backgroundgenerator backgroundgenerator = new Filereaderrandombackgroundgenerator (image_width, IMAGE_HEIGHT, IMAGE_ PATH);

Wordgenerator wordgenerator = new Randomwordgenerator (Random_word);

Fontgenerator fontgenerator = new Randomfontgenerator (min_font_size, Max_font_size, Random_font);

textdecorator[] Textdecorator = new textdecorator[] {};

Textpaster textpaster = new Decoratedrandomtextpaster (min_word_length, Max_word_length, RandomListColorGenerator, Textdecorator);

Wordtoimage wordtoimage = new Composedwordtoimage (Fontgenerator, Backgroundgenerator, Textpaster);

Addfactory (New Gimpyfactory (Wordgenerator, wordtoimage));
}

}

Copy Code code as follows:

package Com.g.core.common.jcaptcha;

Import Com.octo.captcha.service.captchastore.FastHashMapCaptchaStore;
Import Com.octo.captcha.service.image.DefaultManageableImageCaptchaService;
Import Com.octo.captcha.service.image.ImageCaptchaService;

Public class Captchaservicesingleton {

 private static  imagecaptchaservice  instance =null;< /p>

 public Captchaservicesingleton () {
 }

 //use synchronized keyword to resolve thread unsafe
 public Synchronized static Imagecaptchaservice getinstance () {
  if (instance = null) {
    Instance = new Defaultmanageableimagecaptchaservice (new Fasthashmapcaptchastore (), New Jcaptchaengine (), 180, 
                 100000, 75000);
  }
  return instance;
 }

}

Copy Code code as follows:

Package com.g.core.render;

Import Java.awt.image.BufferedImage;
Import java.io.IOException;

Import Javax.imageio.ImageIO;
Import Javax.servlet.ServletOutputStream;

Import Com.g.core.common.jcaptcha.captchaservicesingleton;
Import Com.jfinal.kit.StringKit;
Import Com.jfinal.render.Render;

public class Jcaptcharender extends Render {

Private String Randomcodekey;
Public Jcaptcharender (String randomcodekey) {
if (Stringkit.isblank (Randomcodekey))
throw new IllegalArgumentException ("Randomcodekey can not be blank");
This.randomcodekey = Randomcodekey;
}
@Override
public void render () {
Response.setheader ("Cache-control", "No-store");
Response.setheader ("Pragma", "No-cache");
Response.setdateheader ("Expires", 0);
Response.setcontenttype ("Image/jpeg");
Servletoutputstream SOS = null;
try {
SOS = Response.getoutputstream ();
String Captchaid = Request.getsession (True). GetId ();
BufferedImage challenge = (bufferedimage) captchaservicesingleton.getinstance (). Getchallengeforid (RandomCodeKey, Request.getlocale ());
Imageio.write (Challenge, "JPG", SOS);
Sos.flush ();
catch (Exception e) {
throw new RuntimeException (e);
}
finally {
if (SOS!= null)
try {sos.close ();} catch (IOException e) {e.printstacktrace ();}
}
}

}

Copy Code code as follows:

public void Random_code () {
Render (New Jcaptcharender (GetSession (). GetId ()));
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.