Springboot+MybatisPlus如何实现带验证码的登录功能

实现带验证码的登录功能由两部分组成::1、验证码的获取 2、登录(进行用户名、密码和验证码的判断)

获取验证码

获取验证码需要使用HuTool中的CaptchaUtil.createLineCaptcha()来定义验证码的长度、宽度、验证码位数以及干扰线条数

同时也要使用到HttpSession对象和HeepServletResponse对象 session:拿到验证码需要放入session中,response用来返回页面

1、获取验证码对象

复制代码
LineCaptcha linCaptcha=CaprchaUtil.createLineCaptcha(116,40,4,10);

2、放入session

复制代码
session.setAttribute("code",lineCaptcha.getCode);

3、输出

复制代码
ServletOutputStream stream=response.getOutputStream();
lineCaptcha.write(stream);

4、关闭

复制代码
stream.close();

登录

登录功能首先要去判断验证码,若验证码为空或者不匹配,直接返回错误;否则再去进行用户名和密码的对比

1、判断验证码

复制代码
String sessioncode=(String)session.getAttribute("code");
if(code!=null && code.equals(sessioncode)
{

}

2、若验证码存在并且象征吗匹配成功则去数据库比对用户名和密码(这里我们需要创建一个新的sql语句 select * from user where username=#{username} && password=#{password})如何去创建一个新的sql语句请查看http://t.csdnimg.cn/y7rWT

复制代码
String sessioncode=(String)session.getAttribute("code");
if(code!=null && code.equals(sessioncode)
{
   User user=userService.login(username,password);
   if(user!=null)
   {
      //登录成功
      session.setAttribute("user",user);
   }
   else
   {  //登录失败,用户名或密码有误}
}
else
{  //登录失败,验证码有误}

创建一个新的sql语句 userService.login(username,password)

(1)Mapper中

复制代码
@Select("select * from user where username=#{uesrname} && password=#{password}
public User login(String username,String password);

(2)Service中

复制代码
public User login(Sting username,String password);

(3)ServiceImpl中

复制代码
@Autowird 
UserMapper userMapper

@Overried
public User login(String username,String password)
{
   return userMapper.login(username,password);
}
相关推荐
leonidZhao1 分钟前
Java25新特性:加密对象的PEM编码
java
计算机安禾4 分钟前
【c++面向对象编程】第21篇:运算符重载基础:语法、规则与不可重载的运算符
java·前端·c++
fox_lht4 分钟前
12.3.使用生命周期使引用一直有用
开发语言·后端·rust
萧曵 丶6 分钟前
JUC 实际业务高频面试题浅谈
java·juc·aqs·lock
开发者联盟league6 分钟前
在cursor中配置c/c++开发环境
c语言·开发语言·c++
初圣魔门首席弟子7 分钟前
bug 2026.05.15(以前能运行的java springboot项目突然间不能运行后台数据了)
java·开发语言·bug
求知也求真佳8 分钟前
S19|MCP 与插件:多 Agent 平台 —— 外部能力总线,让外部工具安全接入
开发语言·agent
测试员周周14 分钟前
【Appium 系列】第07节-API测试封装 — BaseAPI 的设计与实现
开发语言·人工智能·功能测试·测试工具·appium·自动化·测试用例
古怪今人15 分钟前
项目和模块 一个目录下创建多个项目 IDEA Multi-Project Workspace插件
java·ide·intellij-idea
加号323 分钟前
【C#】WPF基于Halcon 的HWindowControlWPF 控件实现图像缩放、移动
开发语言·c#·wpf