vue+springboot實(shí)現(xiàn)登錄功能
本文實(shí)例為大家分享了vue+springboot實(shí)現(xiàn)登錄功能的具體代碼,供大家參考,具體內(nèi)容如下
1. 登錄功能的實(shí)現(xiàn)實(shí)現(xiàn)提交表單的代碼如下:
async submitForm(user) {this.$refs[user].validate((valid) => { if(valid){alert('user');this.$axios.post('http:localhost:8087/user/login?code='+this.code,user).then(res => { alert('success') if(res.data.state){ alert(res.data.msg+'登錄成功,即將跳轉(zhuǎn)到主頁......'); } else{ alert(res.data.msg); }}); } else{return false; } });},
當(dāng)頭一棒,腦瓜嗡嗡的。
這東西嗡嗡了好幾天最終被我用比較愚蠢的代碼實(shí)現(xiàn)了,具體思路如下:
首先我現(xiàn)在后臺(tái)獲取到當(dāng)前生成驗(yàn)證碼圖片的真正驗(yàn)證碼,傳遞到前端:
if (valid) {console.log(this.user); this.$axios.get('http://localhost:8087/user/getCode').then(res => { let tcode = res.data.toLowerCase(); if (tcode == this.code) {verify(this.user); } else {alert(’驗(yàn)證碼錯(cuò)誤!’); }}); }
中間的verify就是我驗(yàn)證用戶登錄的用戶名和密碼的地方,驗(yàn)證碼首先生成四位驗(yàn)證碼然后轉(zhuǎn)化為base64格式的字符串,最后傳遞到前端,后端返回字符串的代碼。
@GetMapping('/getCode') @ApiOperation(value='獲取驗(yàn)證碼',notes='從后端獲取驗(yàn)證碼發(fā)送到前端') public String getCode(HttpServletRequest request){String key = (String)request.getServletContext().getAttribute('code');log.info('key:[{}]',key);return key; }
我分析登錄模塊前端給后端傳值不成功的原因是因?yàn)榍岸酥挥杏脩裘兔艽a,然后我錯(cuò)認(rèn)為只有用戶名和密碼的表單可以組成一個(gè)對(duì)象導(dǎo)致一直將表單強(qiáng)制轉(zhuǎn)化為對(duì)象去傳遞給后端,這樣就一直造成了死循環(huán),在這個(gè)問題卡了好久好久。之前也是將用戶名密碼和驗(yàn)證碼傳遞給后端一直卡在那里。我先從后端獲取驗(yàn)證碼在前端比較正確與否,然后將用戶輸入的用戶名和密碼傳遞給后端在數(shù)據(jù)庫中查找對(duì)應(yīng)用戶名的用戶,若可以查找得到則說明此用戶存在,否則用戶存在。接下來比較用戶輸入的密碼是否和數(shù)據(jù)庫存入的密碼一致,如果一致則返回真,登錄成功,其他情況都不成功。具體的實(shí)現(xiàn)代碼如下:
//UserController @PostMapping('/login') @ApiOperation(value = '登錄系統(tǒng)', notes = '登錄員工管理系統(tǒng)') public Map<String,Object> login(@RequestParam String Name,@RequestParam String Pwd){System.out.println(Name+' '+Pwd);Map<String,Object> map = new HashMap<>();try{ User userdb = userService.login(Name,Pwd); map.put('state',true); map.put('msg','登錄成功'); map.put('user',userdb);}catch(Exception e){ e.printStackTrace(); map.put('state',false); map.put('msg',e.getMessage());}log.info('[{}]',map.toString());return map; }
//UserServiceImpl @Override public User login(String Name,String Pwd) {User userDB = userMapper.selectByName(Name);if(!ObjectUtils.isEmpty(userDB)){ if(userDB.getPwd().equals(Pwd)){return userDB; } else{throw new RuntimeException('密碼輸入不正確'); }}else{ throw new RuntimeException('用戶不存在');} }
//UserMapper.javaUser selectByName(String name);
<!--UserMapper.xml--> <select parameterType='String' resultType='com.sunset.system.entity.User'>select Id,Name,Age,Sex,Pwd,Dept,Salaryfrom user where Name = #{name}</select>
在編碼過程中,還遇到一個(gè)小插曲 就是 where Name = “#{name}” 導(dǎo)致在數(shù)據(jù)庫查找中出錯(cuò),希望看此文章的人能避開這個(gè)坑。這樣后端的邏輯就實(shí)現(xiàn)完成,下來是前端邏輯:
async function verify(userinfo) { const {data: res} = await verifyUser(userinfo); console.log(res); if (res.state == true) { _this.$message({ title: '驗(yàn)證成功', message: '歡迎進(jìn)入員工管理系統(tǒng)', type: 'success' }); window.location.; //await _this.$router.push('http://www.baidu.com'); } else { _this.$message({ title: '驗(yàn)證失敗', message: res.msg, type: 'error' }) return false; }}
這里使用axios的post請(qǐng)求,具體的路徑在projectName.src.api 新建一個(gè)user.js的文件
export const verifyUser = (user) =>{ return request({url: '/user/login',method: ’post’,params: { Name: user.Name, Pwd: user.Pwd} })}
此外還需要配置request.js,文件路徑 projectName.src.utils
import axios from ’axios’const instance = axios.create({ baseURL: ’http://localhost:8080’, //后端項(xiàng)目的端口 timeout: 10000, headers: {’X-Custom-Header’: ’foobar’}});export default instance;
要是有其他邏輯問題,歡迎討論交流。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向2. Python importlib動(dòng)態(tài)導(dǎo)入模塊實(shí)現(xiàn)代碼3. android studio 打包自動(dòng)生成版本號(hào)與日期,apk輸入路徑詳解4. 利用promise及參數(shù)解構(gòu)封裝ajax請(qǐng)求的方法5. 淺談python出錯(cuò)時(shí)traceback的解讀6. 在Android中使用WebSocket實(shí)現(xiàn)消息通信的方法詳解7. .NET中l(wèi)ambda表達(dá)式合并問題及解決方法8. Nginx+php配置文件及原理解析9. python matplotlib:plt.scatter() 大小和顏色參數(shù)詳解10. JSP數(shù)據(jù)交互實(shí)現(xiàn)過程解析
