SpringBoot如何使用Kaptcha实现验证码的生成与验证功能

来源:互联网转载和整理 2024-05-02 15:29:50

captcha验证码怎么输入

当我们在项目中登录使用验证码的时候,不妨试试Kaptcha生成验证码,非常简单

1、首先,我们在pom.xml文件中引入kaptcha的maven依赖

<!--kaptcha验证码--><dependency><groupId>com.github.penggle</groupId><artifactId>kaptcha</artifactId><version>2.3.2</version></dependency>

2、然后,我们编写kaptcha的配置类:KaptchaConfig.java

packagecom.lzzy.meet.common.kaptcha;importcom.google.code.kaptcha.impl.DefaultKaptcha;importcom.google.code.kaptcha.util.Config;importlombok.extern.slf4j.Slf4j;importorg.springframework.context.annotation.Bean;importorg.springframework.stereotype.Component;importjava.util.Properties;/***@ClassNameKaptchaConfig*kaptcha配置类*@Author*@Date2019-09-0513:50:50*@Version1.0**/@Slf4j@ComponentpublicclassKaptchaConfig{@BeanpublicDefaultKaptchagetKaptcheCode(){DefaultKaptchadefaultKaptcha=newDefaultKaptcha();Propertiesproperties=newProperties();properties.setProperty("kaptcha.border","no");properties.setProperty("kaptcha.textproducer.font.color","black");properties.setProperty("kaptcha.image.width","100");properties.setProperty("kaptcha.image.height","36");properties.setProperty("kaptcha.textproducer.font.size","30");properties.setProperty("kaptcha.obscurificator.impl","com.google.code.kaptcha.impl.ShadowGimpy");properties.setProperty("kaptcha.session.key","code");properties.setProperty("kaptcha.noise.impl","com.google.code.kaptcha.impl.NoNoise");properties.setProperty("kaptcha.background.clear.from","232,240,254");properties.setProperty("kaptcha.background.clear.to","232,240,254");properties.setProperty("kaptcha.textproducer.char.length","4");properties.setProperty("kaptcha.textproducer.font.names","彩云,宋体,楷体,微软雅黑");Configconfig=newConfig(properties);defaultKaptcha.setConfig(config);returndefaultKaptcha;}}

3、接下来,我们编写kaptcha的控制层:KaptchaController.java

packagecom.lzzy.meet.common.kaptcha;importcom.google.code.kaptcha.Constants;importcom.google.code.kaptcha.Producer;importlombok.extern.slf4j.Slf4j;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.RequestMapping;importjavax.imageio.ImageIO;importjavax.servlet.ServletOutputStream;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjava.awt.image.BufferedImage;/***@ClassNameKaptchaController*kaptcha调用*@Author*@Date2019-09-0513:59:59*@Version1.0**/@Slf4j@Controller@RequestMapping("kaptcha")publicclassKaptchaController{@AutowiredprivateProducerproducer;@GetMapping("kaptcha-image")publicvoidgetKaptchaImage(HttpServletRequestrequest,HttpServletResponseresponse)throwsException{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");StringcapText=producer.createText();log.info("******************当前验证码为:{}******************",capText);//将验证码存于session中request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY,capText);BufferedImagebi=producer.createImage(capText);ServletOutputStreamout=response.getOutputStream();//向页面输出验证码ImageIO.write(bi,"jpg",out);try{//清空缓存区out.flush();}finally{//关闭输出流out.close();}}}

4、然后,我们就可以在前端调用katpcha的接口生成验证码了:

<imgth:src="@{/kaptcha/kaptcha-image}"class="ver_btn"onclick="this.src=this.src+'?c='+Math.random();"/>

由于我这里使用的是 thymeleaf 模板引擎,所以路径名称会有点奇怪,生成的验证码样式如图所示:

5、最后,我们将用户在客户端登陆时输入的验证码传送到服务端进行验证:

/***验证验证码*@param*@return正确:true/错误:false*/publicstaticbooleanvalidate(StringregisterCode){//获取Session中验证码Objectcaptcha=ServletUtils.getAttribute(Constants.KAPTCHA_SESSION_KEY);//判断验证码是否为空if(StringUtils.isEmpty(registerCode)){returnfalse;}//校验验证码的正确与否booleanresult=registerCode.equalsIgnoreCase(captcha.toString());if(result){//正确了后,将验证码从session中删掉ServletUtils.getRequest().getSession().removeAttribute(Constants.KAPTCHA_SESSION_KEY);}//返回验证结果returnresult;}

到此,相信大家对“SpringBoot如何使用Kaptcha实现验证码的生成与验证功能”有了更深的了解,不妨来实际操作一番吧!这里是本站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!