ImportJava.awt.Color;Importjava.io.IOException;ImportJava.util.Random;ImportJavax.servlet.http.HttpServletResponse;Importorg.patchca.color.ColorFactory;Importorg.patchca.filter.predefined.CurvesRippleFilterFactory;Importorg.patchca.filter.predefined.DiffuseRippleFilterFactory;Importorg.patchca.filter.predefined.DoubleRippleFilterFactory;Importorg.patchca.filter.predefined.MarbleRippleFilterFactory;Importorg.patchca.filter.predefined.WobbleRippleFilterFactory;ImportOrg.patchca.service.ConfigurableCaptchaService;ImportOrg.patchca.utils.encoder.EncoderHelper;Importorg.patchca.word.RandomWordFactory; Public classCaptchautil {Private StaticConfigurablecaptchaservice cs =NewConfigurablecaptchaservice (); Private StaticRandom random =NewRandom (); Static{cs.setcolorfactory (Newcolorfactory () {@Override PublicColor GetColor (intx) {int[] C =New int[3]; inti =Random.nextint (c.length); for(intfi = 0; Fi < C.length; fi++) { if(FI = =i) {C[fi]= Random.nextint (71); } Else{C[fi]= Random.nextint (256); } } return NewColor (C[0], c[1], c[2]); } }); Randomwordfactory WF=Newrandomwordfactory (); Wf.setcharacters ("23456789abcdefghigkmnpqrstuvwxyzABCDEFGHIGKLMNPQRSTUVWXYZ"); Wf.setmaxlength (4); Wf.setminlength (4); Cs.setwordfactory (WF); } Public StaticString Generate (HttpServletResponse response)throwsioexception{Switch(Random.nextint (5)) { Case0: Cs.setfilterfactory (Newcurvesripplefilterfactory (Cs.getcolorfactory ())); Break; Case1: Cs.setfilterfactory (Newmarbleripplefilterfactory ()); Break; Case2: Cs.setfilterfactory (Newdoubleripplefilterfactory ()); Break; Case3: Cs.setfilterfactory (Newwobbleripplefilterfactory ()); Break; Case4: Cs.setfilterfactory (Newdiffuseripplefilterfactory ()); Break; } setresponseheaders (response); returnEncoderhelper.getchallangeandwriteimage (CS, "PNG", Response.getoutputstream ()); } Private Static voidsetresponseheaders (httpservletresponse response) {Response.setcontenttype ("Image/png"); Response.setheader ("Cache-control", "No-cache, No-store"); Response.setheader ("Pragma", "No-cache"); LongTime =System.currenttimemillis (); Response.setdateheader ("Last-modified", time); Response.setdateheader ("Date", time); Response.setdateheader ("Expires", time); }}
Java Verification Code CAPTCHA