Springboot+MybatisPlus實現(xiàn)帶驗證碼的登錄
實現(xiàn)帶驗證碼的登錄功能由兩部分組成::1、驗證碼的獲取 2、登錄(進(jìn)行用戶名、密碼和驗證碼的判斷)
獲取驗證碼
獲取驗證碼需要使用HuTool中的CaptchaUtil.createLineCaptcha()來定義驗證碼的長度、寬度、驗證碼位數(shù)以及干擾線條數(shù)
同時也要使用到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、關(guān)閉
stream.close();
登錄
登錄功能首先要去判斷驗證碼,若驗證碼為空或者不匹配,直接返回錯誤;否則再去進(jìn)行用戶名和密碼的對比
1、判斷驗證碼
String sessioncode=(String)session.getAttribute("code"); if(code!=null && code.equals(sessioncode) { }
2、若驗證碼存在并且象征嗎匹配成功則去數(shù)據(jù)庫比對用戶名和密碼(這里我們需要創(chuàng)建一個新的sql語句 select * from user where username=#{username} && password=#{password})如何去創(chuàng)建一個新的sql語句請查看http://www.dbjr.com.cn/program/321214aq6.htm
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 { //登錄失敗,驗證碼有誤}
創(chuàng)建一個新的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); }
到此這篇關(guān)于Springboot+MybatisPlus實現(xiàn)帶驗證碼的登錄的文章就介紹到這了,更多相關(guān)Springboot MybatisPlus驗證碼登錄內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
LibrarySystem圖書管理系統(tǒng)開發(fā)(一)
這篇文章主要為大家詳細(xì)介紹了LibrarySystem圖書管理系統(tǒng)開發(fā),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05MyBatis?Plus?導(dǎo)入IdType失敗的解決
這篇文章主要介紹了MyBatis?Plus?導(dǎo)入IdType失敗的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12Java基于虹軟實現(xiàn)人臉識別、人臉比對、活性檢測等
本文主要介紹了Java基于虹軟實現(xiàn)人臉識別、人臉比對、活性檢測等,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02Java如何獲取當(dāng)前進(jìn)程ID以及所有Java進(jìn)程的進(jìn)程ID
本篇文章主要介紹了Java如何獲取當(dāng)前進(jìn)程ID以及所有Java進(jìn)程的進(jìn)程ID,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06