Simple implementation Verification Code

Source: Internet
Author: User

Import Java.awt.Color;  Import Java.awt.Font;  Import Java.awt.Graphics;  Import Java.awt.image.BufferedImage;    Import Java.util.Random; /** * Verification Code generator * @see----------------------------------------------------------------------------------------------- ---------------* @see can generate digital, uppercase, lowercase and mixed-type verification code * @see support the number of custom verification code characters, support the size of custom CAPTCHA picture, support custom to exclude special characters, support the number of custom interference lines, Support Custom CAPTCHA color * @see--------------------------------------------------------------------------------------------- -----------------* @see In addition, there are many ways to add a verification code to Shiro, you can also modify the Formauthenticationfilter class through inheritance, Shiro to verify the verification code * @see Since SPRINGMVC is used here, and to simplify the operation, this tool is used to generate the verification code and process the verification * @see of the CAPTCHA in the controller---------------------------------------------- ----------------------------------------------------------------* @create Sep, 4:23:13 PM * @author Xuan Yu 

/** * Gadget class * @author Administrator * */@RestControllerpublic class Utilcontroller {@Resourceprivate utildaoservice Utildao Service;    Logger Logging=logger.getlogger (Utilcontroller.class); /** * Get captcha picture and text (captcha text will be saved in httpsession) *//** * Verification Code Generation CLASS * @param request * @param response * @throws Ioexceptio n */@RequestMapping ("getverifycodeimage") public void Getverifycodeimage (HttpServletRequest request, HTTPSERVLETR          Esponse response) throws IOException {//Settings page does not cache Response.setheader ("Pragma", "No-cache");          Response.setheader ("Cache-control", "No-cache");          Response.setdateheader ("Expires", 0);          String Verifycode = Verifycodeutil.generatetextcode (verifycodeutil.type_num_only, 4, NULL);         Put the verification into httpsession inside Request.getsession (). SetAttribute ("Verifycode", Verifycode);          Logging.info ("This generation of verification code is [" + Verifycode + "], has been stored in httpsession");    Sets the output content type to JPEG image Response.setcontenttype ("Image/jpeg");    BufferedImage bufferedimage = Verifycodeutil.generateimagecode (Verifycode, 3, True, Color.White, Color.BLACK,          NULL);      Written to the browser imageio.write (BufferedImage, "JPEG", Response.getoutputstream ()); }

    <! DOCTYPE html>

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.