springboot如何通過(guò)session實(shí)現(xiàn)單點(diǎn)登入詳解
我對(duì)于單點(diǎn)的理解
正常的登錄
進(jìn)入自己系統(tǒng)的登錄頁(yè)面,輸入用戶名密碼,登錄系統(tǒng)。
單點(diǎn)登錄
來(lái)到一個(gè)第三方的登錄頁(yè)面,輸入用戶名密碼,在這個(gè)頁(yè)面登錄成功之后,就算成功的登錄了應(yīng)用系統(tǒng)。好處在于這個(gè)登錄頁(yè)面不僅僅是登錄一個(gè)系統(tǒng),可以同時(shí)登錄多個(gè)系統(tǒng)。即所謂的一次登錄,全程暢通。
效果圖走起


另外開(kāi)一個(gè)瀏覽器

原來(lái)的頁(yè)面刷新一下

發(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", "用戶名或者密碼出錯(cuò)");
return "user/Login";
}
}
@GetMapping("/downline")
public String downline(HttpSession httpSession){
UserEntity userEntity = (UserEntity) httpSession.getAttribute("userinfo");
onlineService.delectUserMap(userEntity.getName());
httpSession.invalidate();
return "redirect:/";
}
}
首頁(yè)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頁(yè)面
<!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>首頁(yè)</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">等級(jí): </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('追求極簡(jiǎn)');
});
</script>
</body>
</html>
總結(jié)
到此這篇關(guān)于springboot如何通過(guò)session實(shí)現(xiàn)單點(diǎn)登入的文章就介紹到這了,更多相關(guān)springboot?session單點(diǎn)登入內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot實(shí)現(xiàn)quartz定時(shí)任務(wù)可視化管理功能
這篇文章主要介紹了SpringBoot實(shí)現(xiàn)quartz定時(shí)任務(wù)可視化管理功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08
Spring中@DependsOn注解的使用代碼實(shí)例
這篇文章主要介紹了Spring中@DependsOn注解的使用代碼實(shí)例,Spring中@DependsOn,主要是使用在類和方法上, 作用是當(dāng)前對(duì)象要依賴另外一些對(duì)象,被依賴的對(duì)象會(huì)先注冊(cè)到Spring的IOC容器中,需要的朋友可以參考下2024-01-01
JDK動(dòng)態(tài)代理之WeakCache緩存的實(shí)現(xiàn)機(jī)制
這篇文章主要介紹了JDK動(dòng)態(tài)代理之WeakCache緩存的實(shí)現(xiàn)機(jī)制2018-02-02
java數(shù)據(jù)結(jié)構(gòu)與算法之桶排序?qū)崿F(xiàn)方法詳解
這篇文章主要介紹了java數(shù)據(jù)結(jié)構(gòu)與算法之桶排序?qū)崿F(xiàn)方法,結(jié)合具體實(shí)例形式詳細(xì)分析了桶排序的概念、原理、實(shí)現(xiàn)方法與相關(guān)操作技巧,需要的朋友可以參考下2017-05-05
Java實(shí)現(xiàn)一個(gè)簡(jiǎn)易版的多級(jí)菜單功能
這篇文章主要給大家介紹了關(guān)于Java如何實(shí)現(xiàn)一個(gè)簡(jiǎn)易版的多級(jí)菜單功能的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-01-01
Spring?Cloud詳細(xì)講解zuul集成Eureka流程
這篇文章主要介紹了Spring?Cloud?zuul集成Eureka,Eureka?Client中內(nèi)置一個(gè)負(fù)載均衡器,用來(lái)進(jìn)行基本的負(fù)載均衡,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06
mybatis整合spring實(shí)現(xiàn)開(kāi)啟mapper.xml映射文件掃描
這篇文章主要介紹了mybatis整合spring實(shí)現(xiàn)開(kāi)啟mapper.xml映射文件掃描,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
SpringBoot使用 druid 連接池來(lái)優(yōu)化分頁(yè)語(yǔ)句
這篇文章主要介紹了SpringBoot使用 druid 連接池來(lái)優(yōu)化分頁(yè)語(yǔ)句,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11

