springboot如何通過session實現(xiàn)單點登入詳解
我對于單點的理解
正常的登錄
進(jìn)入自己系統(tǒng)的登錄頁面,輸入用戶名密碼,登錄系統(tǒng)。
單點登錄
來到一個第三方的登錄頁面,輸入用戶名密碼,在這個頁面登錄成功之后,就算成功的登錄了應(yīng)用系統(tǒng)。好處在于這個登錄頁面不僅僅是登錄一個系統(tǒng),可以同時登錄多個系統(tǒng)。即所謂的一次登錄,全程暢通。
效果圖走起
另外開一個瀏覽器
原來的頁面刷新一下
發(fā)現(xiàn)他已經(jīng)被擠下線
代碼部分
package com.nx.j2ee.service; import org.springframework.stereotype.Service; import javax.servlet.http.HttpSession; import java.util.HashMap; import java.util.Map; @Service public class OnlineService { private Map<String, HttpSession> UserMap = new HashMap<>(); public HttpSession getUserMap(String name) { return UserMap.get(name); } public void setUserMap(String name, HttpSession httpSession) { UserMap.put(name, httpSession); } public void delectUserMap(String name){ UserMap.remove(name); } public int shownum(){ return UserMap.size(); } public Map<String, HttpSession> showall(){ return UserMap; } }
登入controller
package com.nx.j2ee.controller; import com.nx.j2ee.entity.UserEntity; import com.nx.j2ee.service.OnlineService; import com.nx.j2ee.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; @Controller public class User { @Autowired private UserService userService; @Autowired private OnlineService onlineService; /** * @Description : 登入顯示 * @Author : 南巷的花貓 * @Date : 2021/11/23 14:02 */ @GetMapping("/login") public String showlogin(){ return "user/Login"; } /** * @Description : 獲取登入信息 * @Author : 南巷的花貓 * @Date : 2021/11/23 14:03 */ @PostMapping("/login") public String setlogin(@RequestParam("name") String name, @RequestParam("password") String password, Model model, HttpSession httpSession){ UserEntity userEntity = userService.login(name, password); if (userEntity != null){ if(onlineService.getUserMap(name) != null){ onlineService.getUserMap(name).invalidate(); } httpSession.setAttribute("userinfo", userEntity); onlineService.setUserMap(name, httpSession); return "redirect:/"; }else { model.addAttribute("eroor", "用戶名或者密碼出錯"); return "user/Login"; } } @GetMapping("/downline") public String downline(HttpSession httpSession){ UserEntity userEntity = (UserEntity) httpSession.getAttribute("userinfo"); onlineService.delectUserMap(userEntity.getName()); httpSession.invalidate(); return "redirect:/"; } }
首頁controller
package com.nx.j2ee.controller; import com.nx.j2ee.entity.UserEntity; import com.nx.j2ee.service.OnlineService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import java.util.Map; import java.util.Set; @Controller public class Index { @Autowired private OnlineService onlineService; private boolean select = false; @GetMapping("/") public String showindex(Model model, HttpSession httpSession){ UserEntity userinfo = (UserEntity) httpSession.getAttribute("userinfo"); if (userinfo != null){ this.select = true; }else { this.select = false; } int onlinenum = onlineService.shownum(); Set<String> userset = onlineService.showall().keySet(); model.addAttribute("onlinenum", onlinenum); model.addAttribute("userinfo", userinfo); model.addAttribute("userset", userset); model.addAttribute("select", this.select); return "home/index"; } }
HTML頁面
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <link rel="stylesheet" href="/layui/css/layui.css"> <title>首頁</title> </head> <body> <div class="layui-container"> <div> <ul class="layui-nav layui-bg-green" lay-filter=""> <li class="layui-nav-item"> <a href="">在線人數(shù)<span class="layui-badge" th:text="${onlinenum}"></span></a> </li> <li class="layui-nav-item"> <a th:href="@{/PTcourse}">普通課程</a> </li> <li class="layui-nav-item"> <a th:href="@{/VIPcourse}">vip課程</a> </li> <li class="layui-nav-item"> <a th:href="@{/GZcourse}">貴族課程</a> </li> <li class="layui-nav-item" style="float: right"> <a href="" th:if="${not select}">游客</a> <a href="" th:if="${userinfo}" th:text="${userinfo.name}"></a> <dl class="layui-nav-child"> <dd th:if="${select}"><span style="color: #2d6086">等級: </span><span style="color: #0C0C0C" th:text="${userinfo.getTest1()}"></span></dd> <dd><a href="javascript:;">修改信息</a></dd> <dd><a href="javascript:;">安全管理</a></dd> <dd><a th:href="@{/downline}" th:if="${select}">下線</a></dd> <dd><a th:href="@{/login}" th:if="${not select}">登入</a></dd> </dl> </li> </ul> </div> <div style="margin-top: 20px;padding: 0px 50px 0px 50px"> <div> <h3 style="color: #ac0d22">在線用戶列表</h3> </div> <div th:each="username:${userset}"> <p th:text="${username}"></p> </div> </div> </div> <script src="/layui/layui.js"></script> <script> layui.use(['layer', 'form'], function(){ var layer = layui.layer ,form = layui.form; layer.msg('追求極簡'); }); </script> </body> </html>
總結(jié)
到此這篇關(guān)于springboot如何通過session實現(xiàn)單點登入的文章就介紹到這了,更多相關(guān)springboot?session單點登入內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot實現(xiàn)quartz定時任務(wù)可視化管理功能
這篇文章主要介紹了SpringBoot實現(xiàn)quartz定時任務(wù)可視化管理功能,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-08-08JDK動態(tài)代理之WeakCache緩存的實現(xiàn)機制
這篇文章主要介紹了JDK動態(tài)代理之WeakCache緩存的實現(xiàn)機制2018-02-02java數(shù)據(jù)結(jié)構(gòu)與算法之桶排序?qū)崿F(xiàn)方法詳解
這篇文章主要介紹了java數(shù)據(jù)結(jié)構(gòu)與算法之桶排序?qū)崿F(xiàn)方法,結(jié)合具體實例形式詳細(xì)分析了桶排序的概念、原理、實現(xiàn)方法與相關(guān)操作技巧,需要的朋友可以參考下2017-05-05Spring?Cloud詳細(xì)講解zuul集成Eureka流程
這篇文章主要介紹了Spring?Cloud?zuul集成Eureka,Eureka?Client中內(nèi)置一個負(fù)載均衡器,用來進(jìn)行基本的負(fù)載均衡,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06mybatis整合spring實現(xiàn)開啟mapper.xml映射文件掃描
這篇文章主要介紹了mybatis整合spring實現(xiàn)開啟mapper.xml映射文件掃描,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10SpringBoot使用 druid 連接池來優(yōu)化分頁語句
這篇文章主要介紹了SpringBoot使用 druid 連接池來優(yōu)化分頁語句,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11