Apache shiro如何实现一个账户同一时刻只有一个人登录
继承AuthorizingRealm类,重写方法doGetAuthenticationInfo
/**
* 认证(登录时调用)
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
String username = (String) token.getPrincipal();
String password = new String((char[]) token.getCredentials());
ShiroUser shiroUser = userCache.get(username);
// 账号不存在
if (shiroUser == null) {
throw new UnknownAccountException("账号或密码不正确");
}
// 密码错误
if (!password.equals(shiroUser.getPassword())) {
throw new IncorrectCredentialsException("账号或密码不正确");
}
// 账号锁定
if (shiroUser.getStatus() == 0) {
throw new LockedAccountException("账号已被锁定,请联系管理员");
}
//处理session
DefaultWebSecurityManager securityManager = (DefaultWebSecurityManager) SecurityUtils.getSecurityManager();
DefaultWebSessionManager sessionManager = (DefaultWebSessionManager)securityManager.getSessionManager();
Collection<Session> sessions = sessionManager.getSessionDAO().getActiveSessions();//获取当前已登录的用户session列表
for(Session session:sessions){
//判断用是否登录
SimplePrincipalCollection simplePrincipalCollection = (SimplePrincipalCollection)session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY);
if( simplePrincipalCollection != null ){
ShiroUser user = (ShiroUser)simplePrincipalCollection.getPrimaryPrincipal();
if(user!= null && username.equals(user.getUsername())) {
//session超时
if((new Date().getTime()- session.getStartTimestamp().getTime())>= session.getTimeout()){
sessionManager.getSessionDAO().delete(session);//移除(提出用户)
}else{
//sessionManager.getSessionDAO().delete(session);//移除(提出用户)
throw new LockedAccountException("账号已在其他处登录");//不移除(不允许其他人登录相同用户)
}
}
}
}
SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(shiroUser, password, getName());
return info;
}
以上是临时解决方案,后面有更好的在补上
灵感来源:http://www.cnblogs.com/lingxue3769/p/5809543.html
Apache shiro如何实现一个账户同一时刻只有一个人登录的更多相关文章
- shiro 实现单用户登录,一个用户同一时刻只能在一个地方登录
我这里 shiro 并没有集成 springMVC,直接使用 ini 配置文件. shiro.ini [main] # Objects and their properties are defined ...
- 今天一起探讨shiro实现账户同一时刻session唯一
今天和同事在一起探讨shiro如何实现一个账户同一时刻只有一session存在的问题,下面小编把核心代码分享到博客园平台,需要的朋友参考下http://m.0834jl.com 今天遇到一个项目问题, ...
- Apache Shiro系列四,概述 —— Shiro的架构
Shiro的设计目标就是让应用程序的安全管理更简单.更直观. 软件系统一般是基于用户故事来做设计.也就是我们会基于一个客户如何与这个软件系统交互来设计用户界面和服务接口.比如,你可能会说:“如 ...
- 让Apache Shiro保护你的应用
在尝试保护你的应用时,你是否有过挫败感?是否觉得现有的Java安全解决方案难以使用,只会让你更糊涂?本文介绍的Apache Shiro,是一个不同寻常的Java安全框架,为保护应用提供了简单而强大的方 ...
- Apache Shiro 简介
使用 Apache Shiro 为 web 应用程序进行用户身份验证 Shiro 是一个 Apache Incubator 项目,旨在简化身份验证和授权.在本文中,了解 Apache Shiro 并通 ...
- Apache Shiro(安全框架)
当前常用流行的安全框架主要有两种:一个是Apache Shiro:另一个是Springsource. 现在介绍一下apache shiro: 既然是安全框架,解决的肯定是权限的 控制.所谓权限是指:用 ...
- Java开源安全框架之Apache Shiro
APACHE SHIRO安全框架 1 背景 Shiro项目始于2003年初,当时它叫JSecurity项目,当时对于Java应用开发人员没有太多的安全替代方案,始终被一个叫JAAS(Java ...
- Apache Shiro 使用手册(二)Shiro 认证
认证就是验证用户身份的过程.在认证过程中,用户需要提交实体信息(Principals)和凭据信息(Credentials)以检验用户是否合法.最常见的"实体/凭证"组合便是&quo ...
- Apache Shiro 手册
(一)Shiro架构介绍 一.什么是Shiro Apache Shiro是一个强大易用的Java安全框架,提供了认证.授权.加密和会话管理等功能: 认证 - 用户身份识别,常被称为用户"登录 ...
随机推荐
- 将程序sublime添加到右键菜单中
新建wangzhaobo.bat复制一下代码, 粘贴保存,然后打开. @echo Off :START CLS echo *====================================== ...
- Codeforces 452E Three strings 字符串 SAM
原文链接https://www.cnblogs.com/zhouzhendong/p/CF542E.html 题目传送门 - CF452E 题意 给定三个字符串 $s1,s2,s3$ ,对于所有 $L ...
- html-伪类
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Java中System类的相关应用
1.Runtime: public class RuntimeDemo { public static void main(String[] args) { Runtime runtime=Runti ...
- P3810 -三维偏序(陌上花开)cdq-分治
P3810 [模板]三维偏序(陌上花开) 思路 :按照 1维排序 二维 分治三维树状数组维护 #include<bits/stdc++.h> using namespace std; #d ...
- Django路由详解
一.路由基础 1.路由url函数:路由自上而下进行匹配:url(正则路径,视图函数内存地址,默认参数,路由别名) 2.路由正则: 规定开始:^ | 规定结束:$ #url(r'index', view ...
- 搭建WordPress 个人博客
1,准备 LAMP 环境 LAMP 是 Linux.Apache.MySQL 和 PHP 的缩写,是 Wordpress 系统依赖的基础运行环境.我们先来准备 LAMP 环境: (由于部分服务安装过程 ...
- [BZOJ3683]Falsita
[BZOJ3683]Falsita 题目大意: 一个\(n(n\le3\times10^5)\)个结点的树,每个结点有一个权值\(w_i\),\(m(m\le3\times10^5)\)次操作,操作包 ...
- [AHOI2009]维护序列
OJ题号:洛谷2023.BZOJ1798 思路: 参见[洛谷3373][模板]线段树 2 #include<cstdio> #include<cctype> #include& ...
- no such file or directory, open '/node_modules/.staging/
报错 在使用npm过程中连续产生多行报错 no such file or directory, open '/node_modules/.staging/ 原因 npm版本配置不一致导致,可以尝试重新 ...