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);
}
相关推荐
砚底藏山河3 分钟前
【量化纯GET实战 #04】批量快照:一次 HTTP GET 取多只股票
java·python·金融·eclipse
君顾16 分钟前
家校托管互通:从需求分析到系统架构的落地实践
java·开发语言·托管
QQ_216962909611 分钟前
【源码编号:project93375】SpringBoot汽车维修管理信息系统:客户车辆、维修预约、工单派发、配件结算全流程实战
java·spring boot·后端·汽车·springboot·需求分析
jimy125 分钟前
c++隐式移动构造、强制拷贝省略、返回具名局部变量
开发语言·c++
xcl092528 分钟前
流浪宠物领养管理系统开发实战:从需求分析到落地的完整指南
java·spring boot·需求分析·宠物
v_for_van39 分钟前
C语言__attribute__
服务器·c语言·开发语言·mcu·嵌入式·嵌入式实时数据库
lhldsg42 分钟前
家校托管互通系统技术架构与实战设计
java·经验分享·小程序·架构
工业一体机老司机1 小时前
Python实现工业一体机Modbus-TCP通信-从协议解析到多设备轮询实战
开发语言·python·tcp/ip
GIS数据转换器1 小时前
智慧林草“一张图“平台
java·大数据·服务器·前端·javascript·数据库·人工智能
fpcc1 小时前
跟我学C++中级篇—static_assert和assert
开发语言·c++