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>
<think>好的,我现在需要分析用户提供的这段JavaScript代码,并解释它的功能。首先,我会通读代码,理解每个部分的作用。 这段代码定义了一个名为getQ的函数。函数一开始获取了user_nickname输入框的值,存储在userid变量中。如果userid是null或undefined,函数返回false。否则,发起一个AJAX POST请求到../services/Qxgl.ashx,传递action和id参数。请求头里有一个Authorization令牌,看起来像是某种认证信息。 成功回调函数处理返回的msg数据,遍历msg.ds数组,对每个item,通过typeid设置对应的复选框为选中状态。然后收集所有选中的复选框的值,用逗号拼接后去掉最后一个逗号,将结果填入typename输入框。 接下来,我需要确认各个部分的细节是否正确。例如,检查jQuery选择器是否正确,AJAX调用的参数是否合理,以及数据处理是否有潜在错误。比如,$.each遍历msg.ds是否正确,假设msg.ds是一个数组。另外,拼接结果时使用substring去除末尾逗号是否可靠,是否有更稳妥的方法。 还要注意可能的安全问题,比如硬编码的Authorization头,这可能存在安全风险,但用户可能已经处理了。此外,用户权限验证是否在服务端进行,这里只是前端控制,需提醒用户不能仅依赖前端验证。 最后,将这些分析整理成结构清晰的解释,分步骤说明代码的功能和潜在注意事项,确保用户能够理解每个部分的作用及可能的改进点。</think>这是一个典型的基于jQuery的AJAX请求实现,主要用于获取并设置用户权限信息。让我们分步骤解析这个函数的功能: 1. **函数定义与用户ID获取** ```javascript var getQ = function () { var userid = $('#user_nickname').val(); // 获取用户输入框的值 ``` 2. **空值校验** ```javascript if (userid == null || userid == undefined) { // 验证用户ID有效性 return false; } ``` 3. **AJAX请求配置** ```javascript $.ajax({ type: "POST", // 使用POST方法 url: "../services/Qxgl.ashx", // 后端接口地址 headers: { // 请求头认证信息 "Authorization": "0772A4AA051DDEBB393DC164B19A32E57C4FEEAB27A77820" }, ``` 4. **请求参数传递** ```javascript data: { action: "getQ", // 执行动作类型 id: userid // 传递用户ID参数 }, ``` 5. **成功回调处理** ```javascript success: function (msg) { // 遍历返回数据集 $.each(msg.ds, function (index, item) { // 根据typeid勾选对应复选框 $('#a' + item.typeid).attr("checked", true); }) // 拼接选中值 var result = ""; $("input[name='select_checkbox']:checked").each(function () { result += $(this).val() + ','; }); // 去除末尾逗号 if (result != "") { result = result.substring(0, result.lastIndexOf(',')); } // 填充到显示框 $("#typename").val(result); } ``` **功能总结**: - 这是一个用户权限查询功能 - 通过用户输入的用户名查询关联的权限配置 - 将查询结果转换为复选框勾选状态 - 汇总选中权限显示在文本框中 **潜在注意事项**: 1. 硬编码的`Authorization`头存在安全风险,建议动态生成 2. 缺少错误处理逻辑(error回调) 3. 使用字符串拼接处理结果可以考虑改用`Array.join()` 4. 权限控制不能仅依赖前端验证,必须配套后端验证 5. 对`msg.ds`的数据结构有强依赖,建议增加数据校验 6. 建议使用`.prop()`代替`.attr()`操作复选框状态
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值