栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot通过session实现单点登入

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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



    
    
    首页







转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/630754.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号