vue + springboot如何实现登录验证码

  介绍

这篇文章主要介绍vue + springboot如何实现登录验证码,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

先看效果图

 vue + springboot如何实现登录验证码

在登录页面添加验证码html

 vue + springboot如何实现登录验证码

在后端pom文件添加kaptcha依赖

& lt; dependency>   ,,,,& lt; groupId> com.github.penggle   ,,,,& lt; artifactId> kaptcha   ,,,,& lt; version> 2.3.2   & lt;/dependency>

创建KaptchaConfig工具类

package  com.brilliance.module.system.controller.util;   ,   import  com.google.code.kaptcha.impl.DefaultKaptcha;   import  com.google.code.kaptcha.util.Config;   import  org.springframework.context.annotation.Bean;   import  org.springframework.context.annotation.Configuration;   ,   import  java.util.Properties;   ,   @ configuration   public  class  KaptchaConfig  {   ,,@ bean   ,,,public  DefaultKaptcha  getDefaultKaptcha (), {   ,,,,,,,DefaultKaptcha  DefaultKaptcha =, new  DefaultKaptcha ();   ,,,,,,,Properties  Properties =, new 属性();   ,,,,,,,//,图片宽   ,,,,,,,properties.setProperty (“kaptcha.image.width",,“180“);   ,,,,,,,//,图片高   ,,,,,,,properties.setProperty (“kaptcha.image.height",,“50”);   ,,,,,,,//,图片边框   ,,,,,,,properties.setProperty (“kaptcha.border",,“yes");   ,,,,,,,//,边框颜色   ,,,,,,,properties.setProperty (“kaptcha.border.color",,“105179年,90年“);   ,,,,,,,//,字体颜色   ,,,,,,,properties.setProperty (“kaptcha.textproducer.font.color",,“blue");   ,,,,,,,//,字体大小   ,,,,,,,properties.setProperty (“kaptcha.textproducer.font.size",,“40“);   ,,,,,,,//session 关键   ,,,,,,,properties.setProperty (“kaptcha.session.key",,“code");   ,,,,,,,//,验证码长度   ,,,,,,,properties.setProperty (“kaptcha.textproducer.char.length",,“4“);   ,,,,,,,//,字体   ,,,,,,,properties.setProperty (“kaptcha.textproducer.font.names",,“宋体,楷体,微软雅黑“);   ,,,,,,,Config  Config =, new 配置(属性);   ,,,,,,,defaultKaptcha.setConfig(配置);   ,,,,,,,return  defaultKaptcha;   ,,,}   }

控制器中,生成的验证码存储在了复述中,用于以后作校验(复述的配置以及依赖自行百度)

@RestController   @RequestMapping (“/api/user")   @Api (tags =,“系统用户管理“)   public  class  SysUserController  extends  AbstractController {   ,@ autowired   ,private  SysUserService  sysUserService;   ,@ autowired   ,private  DefaultKaptcha  defaultKaptcha;   ,   ,@ autowired   ,RedisTemplate  redisTemplate;   ,   ,@GetMapping (“/createImageCode")   ,public  void  createImageCode (HttpServletRequest 请求,,HttpServletResponse 响应),throws  IOException  {   response.setHeader才能(“Cache-Control",,“不是商店,,no-cache");   response.setContentType才能(“图像/jpeg");//,才能生成文字验证码   String 才能;text =, defaultKaptcha.createText ();//,才能生成图片验证码   BufferedImage 才能;image =, defaultKaptcha.createImage(文本);//才能,这里我们使用复述,缓存验证码的值,并设置过期时间为60秒   redisTemplate.opsForValue才能()这里(“imageCode",文本,60岁,TimeUnit.SECONDS);   ServletOutputStream 才能;out =, response.getOutputStream ();   ImageIO.write才能(图片,,“jpg",,);   out.flush才能();   out.close才能();   null   null   null   null   null   null   null   null   null   null   null   null

vue + springboot如何实现登录验证码