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);
}
相关推荐
llwszx5 分钟前
【Java/Go后端手撸原生Agent(第九篇):Plan-and-Execute规划模式——从“走一步看一步“到“先谋后动“】
java·python·golang·agent开发·plan模式·规划执行模式
ZJH__GO14 分钟前
网络编程v2--多客户端互通
java·运维·服务器·开发语言·计算机网络
霸道流氓气质26 分钟前
SpringBoot中基于 AES-GCM + KMS 密钥管理的数据加解密 Starter 实践
java·数据库·spring boot
Gofarlic_OMS39 分钟前
NX浮动许可调度黑名单机制,对比两款谁更合理
java·大数据·运维·开源·制造
万岳科技系统开发1 小时前
智慧医院小程序开发推动医疗服务流程全面线上化
大数据·开发语言·人工智能
一只小灿灿1 小时前
C++ 修饰符全面详解
开发语言·c++
马里马里奥-1 小时前
从零搭建AI Agent工具链的技术文章大纲
开发语言·qt
乐观的Terry1 小时前
8、发布系统-完整流水线的核心
java·spring boot·spring·spring cloud
Alexalbb1 小时前
从角色过滤到可审计授权:单体系统数据权限设计复盘与演进
java
万亿少女的梦1681 小时前
基于Spring Boot的游戏交易管理系统设计与实现
java·spring boot·mysql·系统设计·交易管理