vue+springboot實現(xiàn)登錄功能
本文實例為大家分享了vue+springboot實現(xiàn)登錄功能的具體代碼,供大家參考,具體內(nèi)容如下
1. 登錄功能的實現(xiàn)
實現(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)頭一棒,腦瓜嗡嗡的。

這東西嗡嗡了好幾天最終被我用比較愚蠢的代碼實現(xiàn)了,具體思路如下:
首先我現(xiàn)在后臺獲取到當(dāng)前生成驗證碼圖片的真正驗證碼,傳遞到前端:
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('驗證碼錯誤!');
}
});
}
中間的verify就是我驗證用戶登錄的用戶名和密碼的地方,驗證碼首先生成四位驗證碼然后轉(zhuǎn)化為base64格式的字符串,最后傳遞到前端,后端返回字符串的代碼。
@GetMapping("/getCode")
@ApiOperation(value="獲取驗證碼",notes="從后端獲取驗證碼發(fā)送到前端")
public String getCode(HttpServletRequest request){
String key = (String)request.getServletContext().getAttribute("code");
log.info("key:[{}]",key);
return key;
}
我分析登錄模塊前端給后端傳值不成功的原因是因為前端只有用戶名和密碼,然后我錯認(rèn)為只有用戶名和密碼的表單可以組成一個對象導(dǎo)致一直將表單強(qiáng)制轉(zhuǎn)化為對象去傳遞給后端,這樣就一直造成了死循環(huán),在這個問題卡了好久好久。之前也是將用戶名密碼和驗證碼傳遞給后端一直卡在那里。我先從后端獲取驗證碼在前端比較正確與否,然后將用戶輸入的用戶名和密碼傳遞給后端在數(shù)據(jù)庫中查找對應(yīng)用戶名的用戶,若可以查找得到則說明此用戶存在,否則用戶存在。接下來比較用戶輸入的密碼是否和數(shù)據(jù)庫存入的密碼一致,如果一致則返回真,登錄成功,其他情況都不成功。具體的實現(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.java User selectByName(String name);
<!--UserMapper.xml-->
<select id="selectByName" parameterType="String" resultType="com.sunset.system.entity.User">
select Id,Name,Age,Sex,Pwd,Dept,Salary
from user where Name = #{name}
</select>
在編碼過程中,還遇到一個小插曲 就是 where Name = “#{name}” 導(dǎo)致在數(shù)據(jù)庫查找中出錯,希望看此文章的人能避開這個坑。
這樣后端的邏輯就實現(xiàn)完成,下來是前端邏輯:
async function verify(userinfo) {
const {data: res} = await verifyUser(userinfo);
console.log(res);
if (res.state == true) {
_this.$message({
title: "驗證成功",
message: "歡迎進(jìn)入員工管理系統(tǒng)",
type: "success"
});
window.location.;
//await _this.$router.push("http://www.baidu.com");
} else {
_this.$message({
title: "驗證失敗",
message: res.msg,
type: "error"
})
return false;
}
}
這里使用axios的post請求,具體的路徑在projectName.src.api 新建一個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', //后端項目的端口
timeout: 10000,
headers: {'X-Custom-Header': 'foobar'}
});
export default instance;
要是有其他邏輯問題,歡迎討論交流。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- springboot+vue+elementsUI實現(xiàn)分角色注冊登錄界面功能
- 基于SpringBoot和Vue3的博客平臺的用戶注冊與登錄功能實現(xiàn)
- springboot+vue實現(xiàn)登錄功能的最新方法整理
- vue+springboot+shiro+jwt實現(xiàn)登錄功能
- springboot+VUE實現(xiàn)登錄注冊
- vue+springboot實現(xiàn)登錄驗證碼
- springboot+vue實現(xiàn)登錄功能
- Vue+Jwt+SpringBoot+Ldap完成登錄認(rèn)證的示例代碼
- SpringBoot3結(jié)合Vue3實現(xiàn)用戶登錄功能
相關(guān)文章
vue?eslint報錯error?"Component?name?"*****"
這篇文章主要給大家介紹了關(guān)于vue?eslint報錯error?“Component?name?“*****“?should?always?be?multi-word”的解決方法,文中通過圖文以及實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09
van-uploader保存文件到后端回顯后端接口返回的數(shù)據(jù)
前端開發(fā)想省時間就是要找框架呀,下面這篇文章主要給大家介紹了關(guān)于van-uploader保存文件到后端回顯后端接口返回的數(shù)據(jù),文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-06-06
Vue render渲染時間戳轉(zhuǎn)時間,時間轉(zhuǎn)時間戳及渲染進(jìn)度條效果
這篇文章主要介紹了Vue render渲染時間戳轉(zhuǎn)時間,時間轉(zhuǎn)時間戳及渲染進(jìn)度條效果,通過實例代碼相結(jié)合的形式給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2018-07-07
vue3中defineProps傳值使用ref響應(yīng)式失效詳解
這篇文章主要給大家介紹了關(guān)于vue3中defineProps傳值使用ref響應(yīng)式失效的相關(guān)資料,文章通過實例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2022-03-03
element-ui中select組件綁定值改變,觸發(fā)change事件方法
今天小編就為大家分享一篇element-ui中select組件綁定值改變,觸發(fā)change事件方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08

