欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

springboot如何通过session实现单点登入详解

程序员文章站 2022-06-27 10:36:55
目录我对于单点的理解效果图走起代码部分总结我对于单点的理解正常的登录进入自己系统的登录页面,输入用户名密码,登录系统。单点登录来到一个第三方的登录页面,输入用户名密码,在这个页面登录成功之后,就算成功...

我对于单点的理解

正常的登录

进入自己系统的登录页面,输入用户名密码,登录系统。

单点登录

来到一个第三方的登录页面,输入用户名密码,在这个页面登录成功之后,就算成功的登录了应用系统。好处在于这个登录页面不仅仅是登录一个系统,可以同时登录多个系统。即所谓的一次登录,全程畅通。

效果图走起

springboot如何通过session实现单点登入详解
springboot如何通过session实现单点登入详解

另外开一个浏览器

springboot如何通过session实现单点登入详解

原来的页面刷新一下

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<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", "用户名或者密码出错");
            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<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页面

<!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>首页</title>
</head>
<body>
<div class="layui-container">
    <div>
        <ul class="layui-nav layui-bg-green" lay-filter="">
            <li class="layui-nav-item">
                <a href="">在线人数<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">等级:&nbsp;</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('追求极简');
  });
</script>
</body>
</html>

总结

到此这篇关于springboot如何通过session实现单点登入的文章就介绍到这了,更多相关springboot session单点登入内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!