springboot如何通過session實現單點登入詳解
我對於單點的理解
正常的登錄
進入自己系統的登錄頁面,輸入用戶名密碼,登錄系統。
單點登錄
來到一個第三方的登錄頁面,輸入用戶名密碼,在這個頁面登錄成功之後,就算成功的登錄瞭應用系統。好處在於這個登錄頁面不僅僅是登錄一個系統,可以同時登錄多個系統。即所謂的一次登錄,全程暢通。
效果圖走起
另外開一個瀏覽器
原來的頁面刷新一下
發現他已經被擠下線
代碼部分
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="">在線人數<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>
總結
到此這篇關於springboot如何通過session實現單點登入的文章就介紹到這瞭,更多相關springboot session單點登入內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- 如何讓Spring Rest 接口中路徑參數可選
- SpringBoot整合Mybatis簡單實現增刪改查
- SpringMVC攔截器超詳細解讀
- springMVC盜鏈接詳解
- SpringMVC自定義攔截器登錄檢測功能的實現代碼