并发登录人数控制--Shiro系列(二)
为了安全起见,同一个账号理应同时只能在一台设备上登录,后面登录的踢出前面登录的。用Shiro可以轻松实现此功能。
shiro中sessionManager是专门作会话管理的,而sessinManager将会话保存在sessionDAO中,如果不给sessionManager注入
sessionDAO,会话将是瞬时状态,没有被保存起来,从sessionManager里取session,是取不到的。
此例中sessionDAO注入了Ehcache缓存,会话被保存在Ehcache中,不知Ehcache为何物的,请自行查阅资料。
完整demo下载:http://download.csdn.net/detail/qq_33556185/9555720
shiro.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-4.2.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">
- <bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
- <property name="globalSessionTimeout" value="1800000"/>
- <property name="deleteInvalidSessions" value="true"/>
- <property name="sessionDAO" ref="sessionDAO"/>
- <property name="sessionIdCookieEnabled" value="true"/>
- <property name="sessionIdCookie" ref="sessionIdCookie"/>
- <property name="sessionValidationSchedulerEnabled" value="true"/>
- <property name="sessionValidationScheduler" ref="sessionValidationScheduler"/>
- <property name="cacheManager" ref="shiroEhcacheManager"/>
- </bean>
- <!-- 会话DAO,sessionManager里面的session需要保存在会话Dao里,没有会话Dao,session是瞬时的,没法从
- sessionManager里面拿到session -->
- <bean id="sessionDAO" class="org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO">
- <property name="sessionIdGenerator" ref="sessionIdGenerator"/>
- <property name="activeSessionsCacheName" value="shiro-activeSessionCache"/>
- </bean>
- <!-- 缓存管理器 -->
- <bean id="shiroEhcacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
- <property name="cacheManagerConfigFile" value="classpath:ehcache.xml" />
- </bean>
- <bean id="sessionIdCookie" class="org.apache.shiro.web.servlet.SimpleCookie">
- <constructor-arg value="sid"/>
- <property name="httpOnly" value="true"/>
- <property name="maxAge" value="-1"/>
- </bean>
- <!-- 会话ID生成器 -->
- <bean id="sessionIdGenerator" class="org.apache.shiro.session.mgt.eis.JavaUuidSessionIdGenerator"></bean>
- <bean id="kickoutSessionControlFilter" class="com.core.shiro.KickoutSessionControlFilter">
- <property name="sessionManager" ref="sessionManager"/>
- <property name="cacheManager" ref="shiroEhcacheManager"/>
- <property name="kickoutAfter" value="false"/>
- <property name="maxSession" value="1"/>
- <property name="kickoutUrl" value="/toLogin?kickout=1"/>
- </bean>
- <bean id="logout" class="org.apache.shiro.web.filter.authc.LogoutFilter">
- <property name="redirectUrl" value="/toLogin" />
- </bean>
- <!-- 会话验证调度器 -->
- <bean id="sessionValidationScheduler" class="org.apache.shiro.session.mgt.ExecutorServiceSessionValidationScheduler ">
- <property name="interval" value="1800000"/>
- <property name="sessionManager" ref="sessionManager"/>
- </bean>
- <!-- Shiro Filter 拦截器相关配置 -->
- <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
- <!-- securityManager -->
- <property name="securityManager" ref="securityManager" />
- <!-- 登录路径 -->
- <property name="loginUrl" value="/toLogin" />
- <!-- 用户访问无权限的链接时跳转此页面 -->
- <property name="unauthorizedUrl" value="/unauthorizedUrl.jsp" />
- <!-- 过滤链定义 -->
- <property name="filters">
- <map>
- <entry key="kickout" value-ref="kickoutSessionControlFilter"/>
- </map>
- </property>
- <property name="filterChainDefinitions">
- <value>
- /loginin=kickout,anon
- /logout = logout
- /toLogin=anon
- /css/**=anon
- /html/**=anon
- /images/**=anon
- /js/**=anon
- /upload/**=anon
- <!-- /userList=roles[admin] -->
- /userList=kickout,authc,perms[/userList]
- /toRegister=kickout,authc,perms[/toRegister]
- /toDeleteUser=kickout,authc,perms[/toDeleteUser]
- /** = kickout,authc
- </value>
- </property>
- </bean>
- <!-- securityManager -->
- <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
- <property name="realm" ref="myRealm" />
- <property name="sessionManager" ref="sessionManager"/>
- <property name="cacheManager" ref="shiroEhcacheManager"/>
- </bean>
- <!-- 自定义Realm实现 -->
- <bean id="myRealm" class="com.core.shiro.CustomRealm" />
- <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="prefix" value="/"/>
- <property name="suffix" value=".jsp"></property>
- </bean>
- </beans>
ehcache.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
- updateCheck="false" maxBytesLocalDisk="20G" maxBytesLocalOffHeap="50M">
- <diskStore path="java.io.tmpdir"/> <!-- 系统的默认临时文件路径 -->
- <defaultCache eternal="false"
- maxElementsInMemory="10000"
- overflowToDisk="false"
- timeToIdleSeconds="0"
- timeToLiveSeconds="0"
- memoryStoreEvictionPolicy="LFU" />
- <!--
- eternal:缓存中对象是否为永久的,如果是,超时设置将被忽略,对象从不过期。
- maxElementsInMemory:缓存中允许创建的最大对象数
- overflowToDisk:内存不足时,是否启用磁盘缓存。
- timeToIdleSeconds:缓存数据的钝化时间,也就是在一个元素消亡之前,
- 两次访问时间的最大时间间隔值,这只能在元素不是永久驻留时有效,
- 如果该值是 0 就意味着元素可以停顿无穷长的时间。
- timeToLiveSeconds:缓存数据的生存时间,也就是一个元素从构建到消亡的最大时间间隔值,
- 这只能在元素不是永久驻留时有效,如果该值是0就意味着元素可以停顿无穷长的时间。
- memoryStoreEvictionPolicy:缓存满了之后的淘汰算法。
- 1 FIFO,先进先出
- 2 LFU,最少被使用,缓存的元素有一个hit属性,hit值最小的将会被清出缓存。
- 3 LRU,最近最少使用的,缓存的元素有一个时间戳,当缓存容量满了,而又需要腾出地方来缓存新的元素的时候,那么现有缓存元素中时间戳离当前时间最远的元素将被清出缓存。
- -->
- <cache name="myCache" eternal="false"
- maxElementsInMemory="10000"
- overflowToDisk="false"
- timeToIdleSeconds="0"
- timeToLiveSeconds="0"
- memoryStoreEvictionPolicy="LFU" />
- <cache name="shiro-activeSessionCache" eternal="false"
- maxElementsInMemory="10000"
- overflowToDisk="true"
- timeToIdleSeconds="0"
- timeToLiveSeconds="0"/>
- </ehcache>
login.jsp的javascript:
- <script type="text/javascript">
- function kickout(){
- var href=location.href;
- if(href.indexOf("kickout")>0){
- alert("您的账号在另一台设备上登录,您被挤下线,若不是您本人操作,请立即修改密码!");
- }
- }
- window.onload=kickout();
- </script>
KickoutSessionControlFilter:
- package com.core.shiro;
- import java.io.Serializable;
- import java.util.Deque;
- import java.util.LinkedList;
- import javax.servlet.ServletRequest;
- import javax.servlet.ServletResponse;
- import org.apache.shiro.cache.Cache;
- import org.apache.shiro.cache.CacheManager;
- import org.apache.shiro.session.Session;
- import org.apache.shiro.session.mgt.DefaultSessionKey;
- import org.apache.shiro.session.mgt.SessionManager;
- import org.apache.shiro.subject.Subject;
- import org.apache.shiro.web.filter.AccessControlFilter;
- import org.apache.shiro.web.util.WebUtils;
- public class KickoutSessionControlFilter extends AccessControlFilter{
- private String kickoutUrl; //踢出后到的地址
- private boolean kickoutAfter; //踢出之前登录的/之后登录的用户 默认踢出之前登录的用户
- private int maxSession; //同一个帐号最大会话数 默认1
- private SessionManager sessionManager;
- private Cache<String, Deque<Serializable>> cache;
- public void setKickoutUrl(String kickoutUrl) {
- this.kickoutUrl = kickoutUrl;
- }
- public void setKickoutAfter(boolean kickoutAfter) {
- this.kickoutAfter = kickoutAfter;
- }
- public void setMaxSession(int maxSession) {
- this.maxSession = maxSession;
- }
- public void setSessionManager(SessionManager sessionManager) {
- this.sessionManager = sessionManager;
- }
- public void setCacheManager(CacheManager cacheManager) {
- this.cache = cacheManager.getCache("shiro-activeSessionCache");
- }
- /**
- * 是否允许访问,返回true表示允许
- */
- @Override
- protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) throws Exception {
- return false;
- }
- /**
- * 表示访问拒绝时是否自己处理,如果返回true表示自己不处理且继续拦截器链执行,返回false表示自己已经处理了(比如重定向到另一个页面)。
- */
- @Override
- protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
- Subject subject = getSubject(request, response);
- if(!subject.isAuthenticated() && !subject.isRemembered()) {
- //如果没有登录,直接进行之后的流程
- return true;
- }
- Session session = subject.getSession();
- String username = (String) subject.getPrincipal();
- Serializable sessionId = session.getId();
- // 初始化用户的队列放到缓存里
- Deque<Serializable> deque = cache.get(username);
- if(deque == null) {
- deque = new LinkedList<Serializable>();
- cache.put(username, deque);
- }
- //如果队列里没有此sessionId,且用户没有被踢出;放入队列
- if(!deque.contains(sessionId) && session.getAttribute("kickout") == null) {
- deque.push(sessionId);
- }
- //如果队列里的sessionId数超出最大会话数,开始踢人
- while(deque.size() > maxSession) {
- Serializable kickoutSessionId = null;
- if(kickoutAfter) { //如果踢出后者
- kickoutSessionId=deque.getFirst();
- kickoutSessionId = deque.removeFirst();
- } else { //否则踢出前者
- kickoutSessionId = deque.removeLast();
- }
- try {
- Session kickoutSession = sessionManager.getSession(new DefaultSessionKey(kickoutSessionId));
- if(kickoutSession != null) {
- //设置会话的kickout属性表示踢出了
- kickoutSession.setAttribute("kickout", true);
- }
- } catch (Exception e) {//ignore exception
- e.printStackTrace();
- }
- }
- //如果被踢出了,直接退出,重定向到踢出后的地址
- if (session.getAttribute("kickout") != null) {
- //会话被踢出了
- try {
- subject.logout();
- } catch (Exception e) {
- }
- WebUtils.issueRedirect(request, response, kickoutUrl);
- return false;
- }
- return true;
- }
- }
http://blog.csdn.net/qq_33556185/article/details/51744004
并发登录人数控制--Shiro系列(二)的更多相关文章
- 2017.4.12 开涛shiro教程-第十八章-并发登录人数控制
原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 开涛shiro教程-第十八章-并发登录人数控制 shiro中没有提 ...
- 第十八章 并发登录人数控制——《跟我学Shiro》
目录贴:跟我学Shiro目录贴 在某些项目中可能会遇到如每个账户同时只能有一个人登录或几个人同时登录,如果同时有多人登录:要么不让后者登录:要么踢出前者登录(强制退出).比如spring securi ...
- 2017.6.30 用shiro实现并发登录人数控制(实际项目中的实现)
之前的学习总结:http://www.cnblogs.com/lyh421/p/6698871.html 1.kickout功能描述 如果将配置文件中的kickout设置为true,则在另处再次登录时 ...
- SpringBoot 并发登录人数控制
通常系统都会限制同一个账号的登录人数,多人登录要么限制后者登录,要么踢出前者,Spring Security 提供了这样的功能,本文讲解一下在没有使用Security的时候如何手动实现这个功能 dem ...
- springboot-权限控制shiro(二)
目录 1. 场景描述 2. 解决方案 1. 场景描述 (1)最近有点小忙,公司真实项目内容有点小多以及不想只介绍理论,就使用springboot单独部署了个shiro的demo项目,还是理论和实际项结 ...
- shiro系列二、身份验证和授权
一.身份验证 先来看看身份验证的流程 流程如下: 1.首先调用Subject.login(token)进行登录,其会自动委托给Security Manager,调用之前必须通过SecurityUtil ...
- Apache Shiro系列二,概述 —— 基本概念
做任何事情,首先要双方就一些概念的理解达成一致,这样大家就有共同语言,后续的沟通效率会高一些. #,Authentication,认证,也就是验证用户的身份,就是确定你是不是你,比如通过用户名.密码的 ...
- Apache Shiro系列四,概述 —— Shiro的架构
Shiro的设计目标就是让应用程序的安全管理更简单.更直观. 软件系统一般是基于用户故事来做设计.也就是我们会基于一个客户如何与这个软件系统交互来设计用户界面和服务接口.比如,你可能会说:“如 ...
- Apache Shiro系列之五,概述 —— 配置
Shiro设计的初衷就是可以运行于任何环境:无论是简单的命令行应用程序还是复杂的企业集群应用.由于运行环境的多样性,所以有多种配置机制可用于配置,本节我们将介绍Shiro内核支持的这几种配置机制. ...
随机推荐
- Homebrew-macOS缺失的软件包管理器(简称brew)
[简介] brew又叫Homebrew,是Mac OSX上的软件包管理工具,能在Mac中方便的安装软件或者卸载软件,只需要一个简单的命令,非常方便 [遇到的问题] 在真正了解软件包管理工具之前,一直是 ...
- squid中实现https的透明代理
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...
- Windows 配置 Apache Python CGI
提示:安装Apache可参考 https://jingyan.baidu.com/article/0eb457e53c019f03f1a905c7.html 1. 打开URL: https://ww ...
- mongodb的分布式集群(2、副本集)
概述 副本集是主从复制的一种,是一种自带故障转移功能的主从复制.攻克了上述主从复制的缺点.实现主server发生问题后.不需人为介入.系统自己主动从新选举一个新的主server的功能. ...
- 关于Gson在强转时的ClassCastException
关于Gson的坑人指出: 将list转化为json String beanListToJson = gson.toJson(list, type); 将json还原为list List<T &g ...
- HDU 5288 OO's sequence (2015多校第一场 二分查找)
OO's Sequence Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) ...
- JVM 虚拟机字节码指令表
把JVM虚拟机字节指令表整理了一下,方便搜索,偶尔复习下 纯手工整理,可能存在一些问题,如果发现请及时告之我会修正 字节码 助记符 指令含义 0x00 nop None 0x01 aconst_nul ...
- JSON劫持漏洞攻防原理及演练
注* 作者发表这篇文章的时间较早,某些方法可能并不是最好的解决方案,但针对这种漏洞进行的攻击还依然可见,如早期的:QQMail邮件泄露漏洞,下面介绍的是对这种攻击原理的介绍. 不久之前,我写了一篇文章 ...
- jQuery中读取json文件
json文件是一种轻量级的数据交互格式.一般在jquery中使用getJSON()方法读取. $.getJSON(url,[data],[callback]) url:加载的页面地址 data: 可选 ...
- Java中this与super
l 对象的this引用 作用: this关键字就是让类中一个方法,访问该类中的另一个方法或属性. 1.构造器中引用该构造器正在初始化的对象. 2.在方法中引用调用该方法的对象(哪个对象调用的方法,t ...