springboot中实现生成验证码和登录校验功能

您所在的位置:网站首页 js实现验证码功能的方法有哪些 springboot中实现生成验证码和登录校验功能

springboot中实现生成验证码和登录校验功能

2024-03-28 11:33| 来源: 网络整理| 查看: 265

前言

网上实现生成验证码的方式有很多,我这里只记录下使用 kaptcha 生成验证码的方式。

实现思路

1、整合 kaptcha ,创建 kaptcha 的工具类。 2、编写接口,在接口中使用 kaptcha 工具类来生成验证码图片(验证码信息)并返回。 3、登录时从 session 中获取验证码进行校验。 4、测试获取验证码图片(验证码信息)接口。

代码实现 一、整合 kaptcha ,创建 kaptcha 的工具类

1、pom 引入 kaptcha 依赖

com.github.penggle kaptcha 2.3.2

2、创建 KaptchaConfig 工具类(可直接复制粘贴使用)

package com.boc.ljh.utils.code; import com.google.code.kaptcha.impl.DefaultKaptcha; import com.google.code.kaptcha.util.Config; import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Component; import java.util.Properties; /** * @Author: ljh * @ClassName KaptchaConfig * @Description TODO * @date 2022/11/3 20:23 * @Version 1.0 */ @Component public class KaptchaConfig { @Bean public DefaultKaptcha getDefaultKaptcha(){ com.google.code.kaptcha.impl.DefaultKaptcha defaultKaptcha = new com.google.code.kaptcha.impl.DefaultKaptcha(); Properties properties = new Properties(); properties.put("kaptcha.border", "no"); //是否有边框 yes有 no没有 properties.put("kaptcha.textproducer.font.color", "blue"); //验证码字体颜色 properties.put("kaptcha.image.width", "150"); //验证码图片的宽 properties.put("kaptcha.image.height", "40"); //验证码图片的高 properties.put("kaptcha.textproducer.font.size", "30"); //验证码字体大小 properties.put("kaptcha.session.key", "verifyCode"); //存储在session中值的key properties.put("kaptcha.textproducer.char.length", "5"); //验证码字符个数 Config config = new Config(properties); defaultKaptcha.setConfig(config); return defaultKaptcha; } } 二、编写接口,在接口中使用 kaptcha 工具类来生成验证码图片(验证码信息)并返回。

1、编写获取验证码图片或者验证码信息功能接口(二选一或者全写都可以,可直接复制粘贴使用)

package com.boc.ljh.controller; import com.boc.ljh.utils.Result; import com.google.code.kaptcha.impl.DefaultKaptcha; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.imageio.ImageIO; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; /** * @Author: ljh * @ClassName VerificationCodeController * @Description TODO * @date 2022/11/3 20:25 * @Version 1.0 */ @RestController @Api(tags = "验证码管理") @RequestMapping("/code") public class VerificationCodeController { @Autowired private DefaultKaptcha captchaProducer; @ApiOperation("获取验证码图片") @GetMapping("/getVerificationCodePhoto") public void getVerificationCodePhoto(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception { byte[] captchaOutputStream = null; ByteArrayOutputStream imgOutputStream = new ByteArrayOutputStream(); try { //生成验证码 String verifyCode = captchaProducer.createText(); //验证码字符串保存到session中 httpServletRequest.getSession().setAttribute("verifyCode", verifyCode); BufferedImage challenge = captchaProducer.createImage(verifyCode); //设置写出图片的格式 ImageIO.write(challenge, "jpg", imgOutputStream); } catch (IllegalArgumentException e) { httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND); return; } captchaOutputStream = imgOutputStream.toByteArray(); httpServletResponse.setHeader("Cache-Control", "no-store"); httpServletResponse.setHeader("Pragma", "no-cache"); httpServletResponse.setDateHeader("Expires", 0); httpServletResponse.setContentType("image/jpeg"); ServletOutputStream responseOutputStream = httpServletResponse.getOutputStream(); responseOutputStream.write(captchaOutputStream); responseOutputStream.flush(); responseOutputStream.close(); } @ApiOperation("获取验证码") @GetMapping("/getVerificationCode") public Result getVerificationCode(HttpServletRequest request) { Result result = new Result(); String verifyCode = captchaProducer.createText(); request.getSession().setAttribute("verifyCode", verifyCode); result.setData(verifyCode); return result; } } 三、登录时从 session 中获取验证码进行校验

注意:从session中获取验证码时,key值一定要和前面生成验证码时存储的key保持一致。 在这里插入图片描述

四、测试获取验证码图片(验证码信息)接口

测试获取验证码图片功能接口 在这里插入图片描述 测试获取验证码字符串功能接口 在这里插入图片描述



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3