在web.xml中将原先的那个监听器替换为自己写的这个就可以了,检测在线用户的只有一个表,里面只有一个id字段。如果用户不是很多,这个表可以是一个MySQL的内存表,或者Oralce的表存储修改为内存。
package com.yourcompany.service.security;
import javax.servlet.http.HttpSessionEvent;
import org.springframework.security.Authentication;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.security.ui.session.HttpSessionEventPublisher;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.yourcompany.domain.entity.security.User;
import com.yourcompany.service.mgmt.OnlineUserService;
/**
* 扩展的HttpSessionEventPublisher
* 支持在线人数统计
*
*/
public class EnhancedHttpSessionEventPublisher extends HttpSessionEventPublisher {
@Override
public void sessionCreated(HttpSessionEvent event) {
// 将用户加入到在线用户列表中
saveOrDeleteOnlineUser(event, Type.SAVE);
super.sessionCreated(event);
}
@Override
public void sessionDestroyed(HttpSessionEvent event) {
// 将用户从在线用户列表中移除
saveOrDeleteOnlineUser(event, Type.DELETE);
super.sessionDestroyed(event);
}
public void saveOrDeleteOnlineUser(HttpSessionEvent event, Type type) {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (auth != null) {
Object principal = auth.getPrincipal();
if (principal instanceof User) {
User user = (User) principal;
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(event
.getSession().getServletContext());
OnlineUserService onlineUserService = (OnlineUserService) wac.getBean("onlineUserService");
switch (type) {
case SAVE:
onlineUserService.saveOnlineUser(user.getId());
break;
case DELETE:
onlineUserService.deleteOnlineUser(user.getId());
break;
}
}
}
}
/**
* 定义一个简单的内部枚举
*/
private static enum Type {
SAVE, DELETE;
}
}
分享到:
相关推荐
主要介绍了如何基于spring security实现在线用户统计,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
基于SpringBoot框架开发的博客系统,采用了springBoot、springSecurity、Redis、elasticSearch、rabbitmq、Mybatis、Nginx等技术,主要实现了博文管理、统计图表、访问记录、附件管理、用户管理、友链管理、监控管理...
技术栈为Springboot+Vue、Mysql、Mybatis、Druid、SpringSecurity、JWT、Redis的项目。它包括了管理员和普通用户两类人员的功能。 对于管理员,他们可以进行图书管理,包括添加、删除、修改、查看图书,并且可以对...
毕业设计,基于SpringBoot+SpringSecurity开发的新冠疫情管理系统,内含Java完整源代码,数据库脚本 基于SpringBoot+SpringSecurity+Thymeleaf新冠疫情管理系统设计毕业源码案例设计 基于SpringBoot及thymeleaf...
本系统的技术环境为: JDK1.8 SpringMvc SpringSecurity SpringBoot Mysql Mybatis Thymeleaf echarts 如果有同学需要毕业设计的话,可以借鉴 系统功能模块 疫情信息管理系统应当具备两种对象,疫情管理员对象可以...
- 后端采用Spring Boot、Spring Security、Redis & Jwt。 - 权限认证使用Jwt,支持多终端认证系统。 - 支持加载动态权限菜单,多方式轻松权限控制。 - 高效率开发,使用代码生成器可以一键生成前后端代码。 ## 功能...
基于SpringBoot+SpringSecurity+Thymeleaf新冠疫情管理系统设计毕业源码案例设计 基于SpringBoot及thymeleaf搭建的疫情信息管理系统 疫情信息管理系统旨在通过信息化手段记录跟踪本地区密切接触者、受感染者、危...
疫情信息管理系统应当具备两种对象,疫情管理员对象可以对确诊患者、密切接触者、死亡患者、治愈患者的信息进行管理操作,系统管理员对象在疫情管理员对象的功能基础上可以对系统登录用户进行增删改查。抗疫管理员...
【资源说明】 该项目是个人毕设项目源码,评审分达到95分,都经过严格...## 技术环境: JDK1.8 SpringSecurity SpringBoot Mysql Mybatis Thymeleaf echarts 管理员账户密码: admin/admin 用户账户密码: user/123
一个基于SpringBoot架构开发的博客系统,采用了SpringBoot、SpringSecurity、Redis、ElasticSearch、RabbitMQ、Mybatis、Nginx等技术,主要实现了博文管理、统计图表、访问记录、附件管理、用户管理、友链管理、监控...
1、通过spring security实现的RBAC权限的模型基础上实现权限、角色、资源的管理,实现根据数据库动态分配权限的功能,对未登录及未授权的操作进行拦截。在用户管理中心,管理员可添加用户代替了自行注册方式。 2、...
Echo 是一套前后端不分离的开源社区系统,基于目前主流 Java Web 技术栈(SpringBoot + MyBatis + MySQL + Redis + Kafka + Elasticsearch + Spring Security + ...),并提供详细的开发文档和配套教程。包含帖子、...
Java社区系统源码并提供详细的开发文档和配套教程,包含帖子、评论、私信、系统通知、点赞、关注、搜索、用户设置、数据统计等模块。 核心技术栈 后端: Spring Spring Boot 2.1.5 RELEASE Spring MVC ORM:MyBatis ...
技术上,系统采用了 Spring Boot、Spring Security、Spring Data JPA、MySQL 等后端技术,以及 Thymeleaf、Bootstrap、JavaScript 等前端技术,实现了用户身份验证、权限控制、数据加密等安全机制,保障系统的安全性...
使用Spring Security 做权限控制,替代拦截器的拦截控制,并使用自己的认证方案替代Security 认证流程,使权限认证和控制更加方便灵活。 使用Redis的set实现点赞,zset实现关注,并使用Redis存储登录ticket和验证码...
后端:springboot+mybatis+jwt+spring security 前端:vue 数据库:mysql 工具:navicat 、 idea 2、系统功能 系统用户分为普通用户和管理员 具体功能如下: 登录页面 首页 短链接生成页面 短链接管理页面 访问...
项目概述:Spark开发平台是一个基于Spring Cloud Hoxton和Spring Boot 2.3的脚手架系统,采用前后端分离架构,整合了OAuth2、Security、Nacos和flowable等技术栈。该平台提供了一套完善的系统管理功能,包括组织机构...
数据库系统期末大作业项目基于Springboot+Vue图书销售系统.zip秋季期末大作业项目 - 网上书店...销售记录浏览和修改、订单状态处理、订单浏览用户管理:用户查询统计、用户消费情况统计、用户删除,留言回复和反馈处理
Chapter 13: Spring Security................................................................................477 Appendix: Cloud Foundry: Deploying to the Cloud ......................................