作者:大姑氵娘祖茉茉_749 | 来源:互联网 | 2023-09-18 19:24
单点登录(SSO)的定义是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统,下面这篇文章主要给大家介绍了关于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 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 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 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页面
总结
到此这篇关于springboot如何通过session实现单点登入的文章就介绍到这了,更多相关springboot session单点登入内容请搜索编程笔记以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程笔记!