您好,欢迎您访问我们燿动吧 – 知识分享,快乐你我,燿动青春!
SpringBoot整合kaptcha生成登录验证码
文章来源:痴柏 时间:2025-03-11
Kaptcha 是1个可下度设置的真用考证码死成对象,可自在建设的选项如:
考证码的字体
考证码字体的年夜小
考证码字体的字体脸色
考证码内乱容的畛域(数字,字母,华文汉字!)
考证码图片的年夜小,边框,边框细细,边框脸色
考证码的滋扰线
考证码的形式(鱼眼款式、3D、平凡隐约、...)
Maven引进kaptcha:
<!--谷歌考证码--><dependency><groupId>com.github.penggle</groupId><artifactId>kaptcha</artifactId><version>2.3.2</version></dependency>起首是经由过程编码指定考证码死成的极少划定规矩
packagecom.example.demo.kaptcha;importcom.谷歌.code.kaptcha.impl.DefaultKaptcha;importcom.谷歌.code.kaptcha.util.Config;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importjava.util.Properties;/**考证码死成设备类*/@ConfigurationpublicclassKaptchaConfig{@Bean(name="captchaProducer")publicDefaultKaptchagetKaptchaBean(){DefaultKaptchadefaultKaptcha=newDefaultKaptcha();Propertiesproperties=newProperties();properties.setProperty("kaptcha.border","yes");properties.setProperty("kaptcha.border.color","105,179,90");properties.setProperty("kaptcha.textproducer.font.color","blue");properties.setProperty("kaptcha.image.width","160");properties.setProperty("kaptcha.image.height","60");properties.setProperty("kaptcha.textproducer.font.size","28");properties.setProperty("kaptcha.session.key","kaptchaCode");properties.setProperty("kaptcha.textproducer.char.spac","35");properties.setProperty("kaptcha.textproducer.char.length","5");properties.setProperty("kaptcha.textproducer.font.names","Arial,Courier");properties.setProperty("kaptcha.noise.color","white");Configconfig=newConfig(properties);defaultKaptcha.setConfig(config);returndefaultKaptcha;}@Bean(name="captchaProducerMath")publicDefaultKaptchagetKaptchaBeanMath(){DefaultKaptchadefaultKaptcha=newDefaultKaptcha();Propertiesproperties=newProperties();properties.setProperty("kaptcha.border","yes");properties.setProperty("kaptcha.border.color","105,179,90");properties.setProperty("kaptcha.textproducer.font.color","blue");properties.setProperty("kaptcha.image.width","100");properties.setProperty("kaptcha.image.height","30");properties.setProperty("kaptcha.textproducer.font.size","30");properties.setProperty("kaptcha.session.key","kaptchaCodeMath");properties.setProperty("kaptcha.textproducer.impl","com.lhf.springboot.support.KaptchaTextCreator");properties.setProperty("kaptcha.textproducer.char.spac","5");properties.setProperty("kaptcha.textproducer.char.length","6");properties.setProperty("kaptcha.textproducer.font.names","Arial,Courier");properties.setProperty("kaptcha.noise.color","white");properties.setProperty("kaptcha.noise.impl","com.谷歌.code.kaptcha.impl.NoNoise");properties.setProperty("kaptcha.obscurificator.impl","com.谷歌.code.kaptcha.impl.ShadowGimpy");Configconfig=newConfig(properties);defaultKaptcha.setConfig(config);returndefaultKaptcha;}}参数诠释以下表格
编写1个数算类死成公用类
packagecom.example.demo.kaptcha;importcom.谷歌.code.kaptcha.text.impl.DefaultTextCreator;importjava.util.Random;publicclassKaptchaTextCreatorextendsDefaultTextCreator{privatestaticfinalString[]CNUMBERS="0,1,2,3,4,5,6,7,8,9,10".split(",");@OverridepublicStringgetText(){Integerresult=0;Randomrandom=newRandom();intx=random.nextInt(10);inty=random.nextInt(10);StringBuildersuChinese=newStringBuilder();intrandomoperands=(int)Math.round(Math.random()*2);if(randomoperands==0){result=x*y;suChinese.append(CNUMBERS[x]);suChinese.append("*");suChinese.append(CNUMBERS[y]);}elseif(randomoperands==1){if(!(x==0)&&y%x==0){result=y/x;suChinese.append(CNUMBERS[y]);suChinese.append("/");suChinese.append(CNUMBERS[x]);}else{result=x+y;suChinese.append(CNUMBERS[x]);suChinese.append("+");suChinese.append(CNUMBERS[y]);}}elseif(randomoperands==2){if(x>=y){result=x-y;suChinese.append(CNUMBERS[x]);suChinese.append("-");suChinese.append(CNUMBERS[y]);}else{result=y-x;suChinese.append(CNUMBERS[y]);suChinese.append("-");suChinese.append(CNUMBERS[x]);}}else{result=x+y;suChinese.append(CNUMBERS[x]);suChinese.append("+");suChinese.append(CNUMBERS[y]);}suChinese.append("=?@"+result);returnsuChinese.toString();}}编写接纳考证码要求的Controller而后输入图片文献淌
packagecom.example.demo.kaptcha;importcom.谷歌.code.kaptcha.Constants;importcom.谷歌.code.kaptcha.Producer;importio.swagger.annotations.Api;importio.swagger.annotations.ApiOperation;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Controller;importorg.springframework.ui.Model;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.servlet.ModelAndView;importjavax.imageio.ImageIO;importjavax.servlet.ServletOutputStream;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjavax.servlet.http.HttpSession;importjava.awt.image.BufferedImage;importjava.io.IOException;/***考证码死成操纵器*/@Controller@Api(value="考证码死成")@RequestMapping("/captcha")publicclassCaptchaController{@AutowiredprivateProducercaptchaProducer;@AutowiredprivateProducercaptchaProducerMath;/***跳转到考证码页里*/@RequestMapping("/home")publicStringhome(Modelmodel){return"img";}/***考证码死成*/@ApiOperation(value="死成谷歌考证码")@GetMapping(value="/captchaImage")publicModelAndViewgetKaptchaImage(HttpServletRequestrequest,HttpServletResponseresponse){ServletOutputStreamout=null;try{HttpSessionsession=request.getSession();response.setDateHeader("Expires",0);response.setHeader("Cache-Control","no-store,no-cache,must-revalidate");response.addHeader("Cache-Control","post-check=0,pre-check=0");response.setHeader("Pragma","no-cache");response.setContentType("image/jpeg");Stringtype=request.getParameter("type");StringcapStr=null;Stringcode=null;//那个便是文原框要输出的了局,为数算时那个便是筹算后的了局BufferedImagebi=null;//考证码为算数8*9榜样if("math".equals(type)){StringcapText=newKaptchaTextCreator().getText();capStr=capText.substring(0,capText.lastIndexOf("@"));code=capText.substring(capText.lastIndexOf("@")+1);bi=captchaProducerMath.createImage(capStr);}elseif("char".equals(type)){//考证码为abcd典型capStr=code=captchaProducer.createText();bi=captchaProducer.createImage(capStr);}session.setAttribute(Constants.KAPTCHA_SESSION_KEY,code);out=response.getOutputStream();ImageIO.write(bi,"jpg",out);System.out.println("bi="+bi);System.out.println("out="+out);out.flush();}catch(Exceptione){e.printStackTrace();}finally{try{if(out!=null){out.close();}}catch(IOExceptione){e.printStackTrace();}}returnnull;}}页里代码以下,注重引进了百度公用JS:
<!DOCTYPEhtml><htmlxmlns:th="http://www.thymeleaf.org"><head><metacharset="UTF-8"><title>Title</title><scripttype="text/javascript"src="http://libs.百度.com/jquery/1.9.0/jquery.min.js"></script></head><body><formth:action="@{/home}"method="post"id="loginForm"><div><divstyle="width:86%;padding-left:8%;padding-top:40px;"><divstyle="float:left;width:100px;height:40px;"><i><imgstyle="height:40px;"id="codeImg1"alt="面打改换"title="面打改换"src="captcha/captchaImage?type=char"/></i></div><br/><br/><br/><divstyle="float:left;width:100px;height:40px;"><i><imgstyle="height:40px;"id="codeImg2"alt="面打改换"title="面打改换"src="captcha/captchaImage?type=math"/></i></div></div></div></form><!--硬键盘控件end--><scripttype="text/javascript">varmessage="[[${message}]]";if(""!=message){layer.msg(message,{time:3000})}$('#codeImg1').click(function(){varurl="captcha/captchaImage?type=char&s="+Math.random();$("#codeImg1").attr("src",url);});$('#codeImg2').click(function(){varurl="captcha/captchaImage?type=math&s="+Math.random();$("#codeImg2").attr("src",url);});functionseverCheck(){$("#loginForm").submit();}//session获得iframe处置$(function(){//判定1停以后是否是干顶层,借使没有是,则干1停顶层页里沉定背if(window!=top){top.location.href=location.href;}});</script></body></html>拜候http://localhost:8080/home 会跳转到呼应页里,那个页里会涌现考证码。
推举您浏览更多相关于“ 考证码kaptchaspringboot谷歌 ”的著作
文章推荐
Copyright © 2024-2025 燿动吧 – 知识分享,快乐你我,燿动青春 http://www.yaodong8.com All Rights Reserved 网站地图