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>