uniapp开发app框架在提升开发效率中的独特优势与应用探索
716
2022-11-17
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
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
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("/loVaqqIvTeVCgin")
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
model.addAttribute("onlinenum", onlinenum);
model.addAttribute("userinfo", userinfo);
model.addAttribute("userset", userset);
model.addAttribute("select", this.select);
return "home/index";
}
}
HTML页面
xmlns:th="http://thymeleaf.org">
layui.use(['layer', 'form'], function(){
var layer = layui.layer
,form = layui.form;
layer.msg('追求极简');
});
总结
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~