springboot通过session实现单点登入

springboot通过session实现单点登入,第1张

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;

    
    @GetMapping("/login")
    public String showlogin(){
        return "user/Login";
    }

    
    @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页面



    
    
    首页



    
        
  • 在线人数
  • 普通课程
  • vip课程
  • 贵族课程
  • 游客
    等级: 
    修改信息
    安全管理
    下线
    登入

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5636631.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存