1、实现原理其实就是自定义过滤器,然后登录时,A登录系统后,B也登录了,这个时候获取此账号之前的session给删除,然后将新的session放入到缓存里面去,一个账户对应一个有序的集合

编写自定义过滤器:KickoutSessionControlFilter.java

 import java.io.Serializable;
import java.util.Deque;
import java.util.LinkedList; import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; 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; import com.itzixi.pojo.ActiveUser; /**
*
* @Title: KickoutSessionControlFilter.java
* @Description: 同一用户后登陆踢出前面的用户
* @date 2016年12月12日 下午7:25:40
* @version V1.0
*/
public class KickoutSessionControlFilter extends AccessControlFilter { private String kickoutUrl; //踢出后到的地址
private boolean kickoutAfter = false; //踢出之前登录的/之后登录的用户 默认踢出之前登录的用户
private int maxSession = 1; //同一个帐号最大会话数 默认1 private SessionManager sessionManager; // TODO 分布式集群环境下,需要改为redis
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-kickout-session");
} @Override
protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) throws Exception {
return 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();
ActiveUser user = (ActiveUser)subject.getPrincipal();
String username = user.getUsername();
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.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
}
} //如果被踢出了,直接退出,重定向到踢出后的地址
if (session.getAttribute("kickout") != null) {
//会话被踢出了
try {
subject.logout();
} catch (Exception e) { //ignore
}
saveRequest(request); HttpServletRequest httpRequest = WebUtils.toHttp(request);
if (ShiroFilterUtils.isAjax(httpRequest)) {
HttpServletResponse httpServletResponse = WebUtils.toHttp(response);
httpServletResponse.sendError(ShiroFilterUtils.HTTP_STATUS_SESSION_EXPIRE);
return false;
} else {
WebUtils.issueRedirect(request, response, kickoutUrl);
return false;
}
} return true;
}
}

2、在applicationContext-shiro.xml配置文件中增加如下配置:

注意:必须使用本机的ehcache缓存来存储,不能使用集群的redis缓存

 <!--自定义filter实现同一个账户只能同时一个人在线,后者登录的踢出前面登录的用户-->
<bean id="kickoutSessionControlFilter" class="com.itzixi.web.shiro.filter.KickoutSessionControlFilter">
<property name="cacheManager" ref="shiroEhcacheManager"/>
<property name="sessionManager" ref="sessionManager"/>
<!-- 是否踢出后来登录的,默认是false;即后者登录的用户踢出前者登录的用户 -->
<property name="kickoutAfter" value="false"/>
<!-- 同一个用户最大的会话数,默认1;比如2的意思是同一个用户允许最多同时两个人登录 -->
<property name="maxSession" value="1"/>
<property name="kickoutUrl" value="/login.action"/>
</bean>

3、修改shiro过滤器的主题配置:如下图红色的标注为 新增 或 修改的

Shiro和Spring 集合实现同一个账号只能一个人在线使用,其它人在使用进行剔除(八)的更多相关文章

  1. shiro 和spring集合 实现登录时输入验证码并校验(七)

    编写实现验证码的主体实现类:CaptchaCode import java.util.UUID; import javax.servlet.http.HttpServletRequest; impor ...

  2. spring security 管理会话 多个用户不可以使用同一个账号登录系统

    多个用户不能使用同一个账号同时登陆系统. 1. 添加监听器 在web.xml中添加一个监听器,这个监听器会在session创建和销毁的时候通知Spring Security. <listener ...

  3. php 实现同一个账号同时只能一个人登录

    php 实现同一个账号同时只能一个人登录 张映 发表于 2015-01-22 分类目录: php 标签:mysql, nginx, openfire, php, redis 以前考虑过这个问题,今天实 ...

  4. 安全框架Shiro和Spring Security比较

    Shiro 首先Shiro较之 Spring Security,Shiro在保持强大功能的同时,还在简单性和灵活性方面拥有巨大优势. Shiro是一个强大而灵活的开源安全框架,能够非常清晰的处理认证. ...

  5. SpringMVC实现账号只能在一处登陆

    一.问题引导 在Web开发中,实现一个账号只能在一处登陆有两种形式:1.当某个账号在某处登陆后,如果再在其他处登陆,将前一个账号挤掉:2.当某个账号登陆后,此账号在其他设备登陆提示已经登陆,无法登陆. ...

  6. 权限框架Apache Shiro 和 Spring Security

    Shiro 首先Shiro较之 Spring Security,Shiro在保持强大功能的同时,还在简单性和灵活性方面拥有巨大优势.Shiro是一个强大而灵活的开源安全框架,能够非常清晰的处理认证.授 ...

  7. apache shiro整合spring(一)

    apache shiro整合spring 将shiro配置文件整合到spring体系中 方式一:直接在spring的配置文件中import shiro的配置文件 方式二:直接在web.xml中配置sh ...

  8. shiro与spring的整合

    shiro与spring的整合 上一期,我们分享了如何在项目中使用shiro,了解了shiro的基本用法,但毕竟学习shiro的目的就是在项目中应用shiro,更准确地说是在web项目中应用shiro ...

  9. java保持同一时间同一账号只能在一处登录

    //登录页面 login.jsp <%@ page language="java" contentType="text/html; charset=UTF-8&qu ...

随机推荐

  1. Wordpress 加载 js 文件到底部

    wp_enqueue_script wp_enqueue_script( string $handle, string $src = '', array $deps = array(), string ...

  2. Github使用.gitignore文件忽略不必要上传的文件 (转)

    原文地址: https://blog.csdn.net/gjy211/article/details/51607347 常用编程语言及各种框架平台下的通用   .gitignore   文件 http ...

  3. R学习笔记 ---- 系列文章

    R实战 开篇:介绍R的使用 R学习笔记 第五篇:字符串操作 R学习笔记 第六篇:数据变换和清理 R学习笔记 第四篇:函数,分支和循环 R学习笔记 第三篇:数据框 R学习笔记 第二篇:矩阵.数组和列表 ...

  4. dig命令使用大全

    https://www.cnblogs.com/daxian2012/archive/2013/01/10/2854126.html 译者序:可以这样说,翻译本篇文档的过程就是我重新学习DNS的过程, ...

  5. 洛谷2473(SCOI2008)奖励关

    题目:https://www.luogu.org/problemnew/show/P2473 因为可不可选此物与之前选过什么物品有关,所以状态可以记录成前面已经选过什么物品. 因为选不选此物与它带来的 ...

  6. PYTHON 常用API ***

    1.类型判断 data = b'' data = bytes() print (type(data)) #<class 'bytes'> isinstance(123,int) if ty ...

  7. redis3.0自带集群配置

    参考 http://redis.readthedocs.org/en/latest/topic/cluster-tutorial.html http://yindashan.github.io/blo ...

  8. 使用JS获取当前地理位置方法汇总(如用谷歌接口,会出再以上报错,必须申请密钥并设置接受服务器IP!!!)

    RefererNotAllowedMapError 错误 加载 Google Maps JavaScript API 的当前 URL 尚未添加到允许的引用站点列表中.请在 Google API Con ...

  9. SPOJ Count on a tree(主席树+LCA)

    一.题目 COT - Count on a tree You are given a tree with N nodes. The tree nodes are numbered from 1 to  ...

  10. Poly

    folly/Poly.h Poly is a class template that makes it relatively easy to define a type-erasing polymor ...