【SpringBoot】實現登錄功能

在上一篇博客中,我們講解了注冊頁面的實現。在此基礎上會跳轉到登錄頁面,今天給大家帶來的是使用 SpringBoot,MyBatis,Html,CSS,JavaScript,前后端交互實現一個登錄功能。

目錄

一、效果

二、源碼

2.1 前端

2.2 后端


一、效果

用戶名和密碼欄輸入空或沒有值時,提示錯誤。

在數據庫中有以下信息,任意挑選一條信息進行登錄操作。

輸入用戶 lisi123 后登陸成功?

跳轉到個人列表。


二、源碼

2.1 前端

前端登陸頁面 html 代碼,login.html:

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>登陸頁面</title><link rel="stylesheet" href="css/common.css"><link rel="stylesheet" href="css/login.css"><script src="js/jquery.min.js"></script>
</head><body><!-- 導航欄 --><div class="nav"><img src="img/touxiang.png" alt=""><span class="title">隨心日記系統</span><!-- 用來占據中間位置 --><span class="spacer"></span><a href="blog_list.html">主頁</a><a href="blog_edit.html">寫日記</a><a href="login.html">注冊</a><!-- <a href="#">注銷</a> --></div><!-- 版心 --><div class="login-container"><!-- 中間的登陸框 --><div class="login-dialog"><h3>登陸</h3><div class="row"><span>用戶名</span><input type="text" id="username"></div><div class="row"><span>密碼</span><input type="password" id="password"></div><div class="row"><button id="submit" onclick="doLogin()">提交</button></div></div></div><script>// 登錄功能function doLogin() {// 1.非空校驗var username = jQuery("#username");var password = jQuery("#password");if(username.val().trim() == "") {alert("請先輸入用戶名!")username.focus();return false;}if(password.val().trim() == "") {alert("請輸入密碼!")password.focus();return false;}// 2.將數據提交到后端jQuery.ajax({url:"/user/login",type:"GET",data:{"username":username.val(),"password":password.val()},success:function(ret) {//3.將結果展示給用戶if (ret.code == 200 && ret.data == 1 ) {alert("登錄成功!");location.href = "myblog_list.html";} else {alert("登錄失敗!"+ret.msg);}}});}</script>
</body></html>

登陸頁面的樣式,login.css

.login-container {width: 100%;height: calc(100% - 50px);display: flex;justify-content: center;align-items: center;
}.login-dialog {width: 400px;height: 400px;background-color: rgba(255, 255, 255, 0.8);border-radius: 10px;
}.login-dialog h3 {padding: 50px 0;text-align: center;
}.login-dialog .row {height: 50px;display: flex;justify-content: center;align-items: center;
}.login-dialog .row span {display: block;width: 100px;font-weight: 700;
}.login-dialog .row input {width: 200px;height: 40px;line-height: 40px;font-size: 24px;border-radius: 10px;border: none;outline: none;text-indent: 10px;
}.login-dialog button {width: 300px;height: 50px;color: white;background-color: green;border: none;border-radius: 10px;
}.login-dialog button:active {background-color: #666;
}

公共樣式(注冊或登錄頁面),common.css:

* {margin: 0;padding: 0;box-sizing: border-box;
}/* 設置整體頁面高度 */
html, body {height: 100%;background-image: url(../img/cat2.png);background-position: center center;background-size: cover;background-repeat: no-repeat;
}/* 上方導航欄 */
.nav {width: 100%;height: 50px;background-color: rgba(51, 51, 51, 0.4);color: #fff;display: flex;justify-content: left;align-items: center;
}/* 導航欄中的圖標 */
.nav img {width: 40px;height: 40px;margin-left: 30px;margin-right: 10px;border-radius: 50%;
}/* 導航欄中的占位器 */
.nav .spacer {width: 70%;
}/* 導航欄中的按鈕 */
.nav a {color: #fff;text-decoration: none;padding: 0 10px;
}/* 頁面內容容器, 版心 */
.container {/* 使用 calc 計算高度 */height: calc(100% - 50px);/* 設置版心寬度 */width: 1000px;/* 水平居中 */margin: 0 auto;/* 使用彈性布局 */display: flex;justify-content: space-between;align-items: center;
}/* 左側部分, 用來放置用戶信息 */
.container-left {height: 100%;width: 200px;
}/* 右側部分, 用來放置正文 */
.container-right {height: 100%;/* 和左側部分中間留出 5px 間隙 */width: 795px;/* 如果內容溢出就自動加上滾動條 */overflow: auto;background-color: rgba(255, 255, 255, 0.8);border-radius: 10px;
}/* 展示用戶信息的卡片 */
.card {background-color: rgba(255, 255, 255, 0.8);border-radius: 10px;padding: 30px;
}/* 用戶頭像 */
.card img {width: 140px;height: 140px;border-radius: 50%;
}/* 用戶名 */
.card h3 {text-align: center;padding: 10px;
}/* 用戶 github 鏈接 */
.card a {display: block;text-align: center;color: #999;text-decoration: none;padding: 10px;
}/* 展示文章數目的面板 */
.card .counter {padding: 5px;display: flex;justify-content: space-around;
}

2.2 后端

統一返回格式,ResultAjax 類:

/*** 統一的返回格式*/
@Data
public class ResultAjax {private int code;private String msg;private Object data;// 成功public static ResultAjax success(Object data) {ResultAjax resultAjax = new ResultAjax();resultAjax.setCode(200);resultAjax.setMsg("");resultAjax.setData(data);return resultAjax;}public static ResultAjax success(int code, String msg, Object data) {ResultAjax resultAjax = new ResultAjax();resultAjax.setCode(code);resultAjax.setMsg(msg);resultAjax.setData(data);return resultAjax;}// 失敗public static ResultAjax fail(int code, String msg) {ResultAjax resultAjax = new ResultAjax();resultAjax.setCode(code);resultAjax.setMsg(msg);return resultAjax;}public static ResultAjax fail(int code, String msg, Object data) {ResultAjax resultAjax = new ResultAjax();resultAjax.setCode(code);resultAjax.setMsg(msg);resultAjax.setData(data);return resultAjax;}
}

UserMapper 接口:

@Mapper
public interface UserMapper {//登錄功能@Select("select * from userinfo where username=#{username}")Userinfo getUserinfoByName(@Param("username") String username);
}

UserService 類:

@Service
public class UserService {//注冊接口注入進來@Autowiredprivate UserMapper userMapper;//登錄功能public Userinfo getUserByName(String username) {return userMapper.getUserinfoByName(username);}
}

UserControler 類:

@RestController
@RequestMapping("/user")
public class UserController {@Autowiredprivate UserService userService; /*** 登錄接口* @param userinfoVO* @return*/@RequestMapping("/login")public ResultAjax login(UserinfoVO userinfoVO, HttpServletRequest request) {// 1.參數校驗if (userinfoVO == null || !StringUtils.hasLength(userinfoVO.getUsername())|| !StringUtils.hasLength(userinfoVO.getPassword())) {// 非法登錄return ResultAjax.fail(-1,"非法登錄!");}// 2.根據用戶名查詢對象,判斷用戶名是否錯誤Userinfo userinfo = userService.getUserByName(userinfoVO.getUsername());if (userinfo == null && userinfo.getId() == 0) {return ResultAjax.fail(-2,"賬號或密碼錯誤!");}// 3.使用對象中的密碼和輸入的密碼進行對比,判斷密碼是否錯誤if (!userinfoVO.getPassword().equals(userinfo.getPassword())) {return ResultAjax.fail(-2,"賬號或密碼錯誤!");}// 4.成功后將對象存儲到 session 中HttpSession session = request.getSession();session.setAttribute(ApplicationVariable.SESSION_USERINFO_KEY,userinfo);// 5.結果返回給用戶return ResultAjax.success(1);}
}

上述為整個登錄功能的核心代碼,其中需要注意的是前端需自行映入 jQuery 依賴、在 application.properties 文件中連接數據庫、創建對應的數據庫。連接數據庫,數據庫的創建代碼在上篇博客中已講解。


本篇博客到這里就結束了,感謝各位的觀看。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/diannao/73271.shtml
繁體地址,請注明出處:http://hk.pswp.cn/diannao/73271.shtml
英文地址,請注明出處:http://en.pswp.cn/diannao/73271.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

【小白向】Ubuntu|VMware 新建虛擬機后打開 SSH 服務、在主機上安裝vscode并連接、配置 git 的 ssh

常常有人問VMware-Tools裝了也復制粘貼不了怎么辦&#xff0c;這個東西影響因素太多了&#xff0c;具體解決辦法你們可以參考一下&#xff1a;【經驗】VMware&#xff5c;虛擬機只能使用鼠標無法使用鍵盤、裝不了或裝了VMware-Tools無法復制粘貼的可能解決辦法_增強型鍵盤驅動程…

mingw工具源碼編譯

ming-w64 mingw編譯生成的庫&#xff0c;需要mingw的lib文件支持。 https://github.com/mingw-w64/mingw-w64 使用msys2的bash git checkout v8.0.3 ./configure --disable-dependency-tracking --targetx86_64-w64-mingw32 mingw32-make.exe -j4 修改makefile中的make 改成mi…

LSTM方法實踐——基于LSTM的汽車銷量時序建模與預測分析

Hi&#xff0c;大家好&#xff0c;我是半畝花海。本實驗基于汽車銷量時序數據&#xff0c;使用LSTM網絡&#xff08;長短期記憶網絡&#xff09;構建時間序列預測模型。通過數據預處理、模型訓練與評估等完整流程&#xff0c;驗證LSTM在短期時序預測中的有效性。 目錄 一、實驗…

Stable Diffusion教程|快速入門SD繪畫原理與安裝

什么是Stable Diffusion&#xff0c;什么是煉丹師&#xff1f;根據市場研究機構預測&#xff0c;到2025年全球AI繪畫市場規模將達到100億美元&#xff0c;其中Stable Diffusion&#xff08;簡稱SD&#xff09;作為一種先進的圖像生成技術之一&#xff0c;市場份額也在不斷增長&…

Webpack構建流程詳解優化前端性能\Dev-Server與Proxy\網絡攻擊\HMR

簡版 核心流程圖 根據&#xff0c;Webpack的構建流程分為初始化、編譯和輸出三個階段。初始化階段讀取配置、加載插件、實例化Compiler。編譯階段&#xff08;構建依賴關系&#xff09;涉及Compiler類的運行&#xff0c;生成Compilation對象&#xff0c;處理模塊依賴。輸出階…

《Transformer如何進行圖像分類:從新手到入門》

引言 如果你對人工智能&#xff08;AI&#xff09;或深度學習&#xff08;Deep Learning&#xff09;感興趣&#xff0c;可能聽說過“Transformer”這個詞。它最初在自然語言處理&#xff08;NLP&#xff09;領域大放異彩&#xff0c;比如在翻譯、聊天機器人和文本生成中表現出…

Java --- 根據身份證號計算年齡

介紹 根據身份證號計算年齡 Java代碼 /*** 根據身份證號計算年齡* param birthDateStr* return*/public static int calculateAge(String birthDateStr) {try {birthDateStrbirthDateStr.substring(6,68);// 定義日期格式SimpleDateFormat sdf new SimpleDateFormat("…

零成本搭建Calibre個人數字圖書館支持EPUB MOBI格式遠程直讀

文章目錄 前言1.網絡書庫軟件下載安裝2.網絡書庫服務器設置3.內網穿透工具設置4.公網使用kindle訪問內網私人書庫 前言 嘿&#xff0c;各位書蟲們&#xff01;今天要給大家安利一個超級炫酷的技能——如何在本地Windows電腦上搭建自己的私人云端書庫。亞馬遜服務停了&#xff…

【Linux 指北】常用 Linux 指令匯總

第一章、常用基本指令 # 注意&#xff1a; # #表示管理員 # $表示普通用戶 [rootlocalhost Practice]# 說明此處表示管理員01. ls 指令 語法&#xff1a; ls [選項][目錄或文件] 功能&#xff1a;對于目錄&#xff0c;該命令列出該目錄下的所有子目錄與文件。對于文件&#xf…

跟蹤napi_gro_receive_entry時IP頭信息缺失的分析

問題描述 在使用eBPF程序跟蹤napi_gro_receive_entry內核跟蹤點時&#xff0c;發現獲取到的IP頭部字段&#xff08;如saddr、daddr、protocol&#xff09;為空值。 代碼如下&#xff1a; /* 自定義結構體來映射 napi_gro_receive_entry tracepoint 的 format */ struct napi…

Android子線程更新View的方法原理

對于所有的Android開發者來說&#xff0c;“View的更新必須在UI線程中進行”是一項最基本常識。 如果不在UI線程中更新View&#xff0c;系統會拋出CalledFromWrongThreadException異常。那么有沒有什么辦法可以不在UI線程中更新View&#xff1f;答案當然是有的&#xff01; 一…

【Manus資料合集】激活碼內測渠道+《Manus Al:Agent應用的ChatGPT時刻》(附資源)

DeepSeek 之后&#xff0c;又一個AI沸騰&#xff0c;沖擊的不僅僅是通用大模型。 ——全球首款通用AI Agent的破圈啟示錄 2025年3月6日凌晨&#xff0c;全球AI圈被一款名為Manus的產品徹底點燃。由Monica團隊&#xff08;隸屬中國夜鶯科技&#xff09;推出的“全球首款通用AI…

Python----計算機視覺處理(opencv:像素,RGB顏色,圖像的存儲,opencv安裝,代碼展示)

一、計算機眼中的圖像 像素 像素是圖像的基本單元&#xff0c;每個像素存儲著圖像的顏色、亮度和其他特征。一系列像素組合到一起就形成 了完整的圖像&#xff0c;在計算機中&#xff0c;圖像以像素的形式存在并采用二進制格式進行存儲。根據圖像的顏色不 同&#xff0c;每個像…

SQLiteStudio:一款免費跨平臺的SQLite管理工具

SQLiteStudio 是一款專門用于管理和操作 SQLite 數據庫的免費工具。它提供直觀的圖形化界面&#xff0c;簡化了數據庫的創建、編輯、查詢和維護&#xff0c;適合數據庫開發者和數據分析師使用。 功能特性 SQLiteStudio 提供的主要功能包括&#xff1a; 免費開源&#xff0c;可…

【軟考網工-實踐篇】DHCP 動態主機配置協議

一、DHCP簡介 DHCP&#xff0c;Dynamic Host Configuration Protocol&#xff0c;動態主機配置協議。 位置&#xff1a;DHCP常見運行于路由器上&#xff0c;作為DHCP服務器功能&#xff1a;用于自動分配IP地址及其他網絡參數給網絡中的設備作用&#xff1a;簡化網絡管理&…

【Linux學習筆記】Linux用戶和文件權限的深度剖析

【Linux學習筆記】Linux用戶和文件權限的深度剖析 &#x1f525;個人主頁&#xff1a;大白的編程日記 &#x1f525;專欄&#xff1a;Linux學習筆記 前言 文章目錄 【Linux學習筆記】Linux用戶和文件權限的深度剖析前言一. Linux權限管理1.1 文件訪問者的分類&#xff08;人)…

Centos離線安裝openssl-devel

文章目錄 Centos離線安裝openssl-devel1. openssl-devel是什么&#xff1f;2. openssl-devel下載地址3. openssl-devel安裝4. 安裝結果驗證 Centos離線安裝openssl-devel 1. openssl-devel是什么&#xff1f; openssl-devel 是 Linux 系統中與 OpenSSL 加密庫相關的開發包&…

深度學習篇---Opencv中Haar級聯分類器的自定義

文章目錄 1. 準備工作1.1安裝 OpenCV1.2準備數據集1.2.1正樣本1.2.2負樣本 2. 數據準備2.1 正樣本的準備2.1.1步驟2.1.2生成正樣本描述文件2.1.3示例命令2.1.4正樣本描述文件格式 2.2 負樣本的準備2.2.1步驟2.2.2負樣本描述文件格式 3. 訓練分類器3.1命令格式3.2參數說明 4. 訓…

Smart Time Plus smarttimeplus-MySQLConnection SQL注入漏洞(CVE-2024-53544)

免責聲明 本文所描述的漏洞及其復現步驟僅供網絡安全研究與教育目的使用。任何人不得將本文提供的信息用于非法目的或未經授權的系統測試。作者不對任何由于使用本文信息而導致的直接或間接損害承擔責任。如涉及侵權,請及時與我們聯系,我們將盡快處理并刪除相關內容。 0x01…

58.Harmonyos NEXT 圖片預覽組件架構設計與實現原理

溫馨提示&#xff1a;本篇博客的詳細代碼已發布到 git : https://gitcode.com/nutpi/HarmonyosNext 可以下載運行哦&#xff01; Harmonyos NEXT 圖片預覽組件架構設計與實現原理 文章目錄 Harmonyos NEXT 圖片預覽組件架構設計與實現原理效果預覽一、組件架構概述1. 核心組件層…