shiro实现单点登录(一个用户同一时刻只能在一个地方登录)

本文介绍了如何在Shiro中通过ini配置文件实现单点登录,重点讲解了如何在LocalAuthorizingRealm中删除用户旧session以确保登录状态的唯一性。通过移除session,确保用户在同一时间仅能在一处登录,并在登录失效时提供相应的客户端响应。

我这里 shiro 并没有集成 springMVC,直接使用 ini 配置文件。

shiro.ini

[main]

# Objects and their properties are defined here,

# Such as the securityManager, Realms and anything

# else needed to build the SecurityManager

authc.loginUrl = /login.jsp

authc.successUrl = /web/index.jsp

#cache manager

builtInCacheManager = org.apache.shiro.cache.MemoryConstrainedCacheManager

securityManager=org.apache.shiro.web.mgt.DefaultWebSecurityManager

securityManager.cacheManager = $builtInCacheManager

securityManager.sessionManager=$sessionManager

#session 必须配置session,强制退出时,通过将session移除实现

sessionManager=org.apache.shiro.web.session.mgt.DefaultWebSessionManager

sessionManager.sessionDAO=$sessionDAO

sessionDAO=org.apache.shiro.session.mgt.eis.MemorySessionDAO

# Create ldap realm

ldapRealm = org.apache.shiro.realm.ldap.JndiLdapRealm

#…

# Configure JDBC realm datasource

dataSource = org.postgresql.ds.PGPoolingDataSource

#…

# Create JDBC realm.

jdbcRealm.permissionsLookupEnabled = true

jdbcRealm = org.apache.shiro.realm.jdbc.JdbcRealm

jdbcRealm.userRolesQuery = …

jdbcRealm.permissionsQuery = …

jdbcRealm.dataSource = $dataSource

#self realm

localAuthorizingRealm = com.redbudtek.shiro.LocalAuthorizingRealm

securityManager.realms = $ldapRealm, $localAuthorizingRealm

在 LocalAuthorizingRealm 中,用户登录进行认证之前,先将该用户的其他session移除:

@Override

protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {

String userName = (String)authenticationToken.getPrincipal();

//处理session

DefaultWebSecurityManager securityManager = (DefaultWebSecurityManager) SecurityUtils.getSecurityManager();

DefaultWebSessionManager sessionManager = (DefaultWebSessionManager)securityManager.getSessionManager();

Collection sessions = sessionManager.getSessionDAO().getActiveSessions();//获取当前已登录的用户session列表

for(Session session:sessions){

//清除该用户以前登录时保存的session

if(userName.equals(String.valueOf(session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY)))) {

sessionManager.getSessionDAO().delete(session);

}

}

String pwd = null;

return new SimpleAuthenticationInfo(userName,pwd,getName());

}

当session删除之后,必须有客户端与服务器端的交互,shiro才能进行认证判断。在与服务器交互时,subject信息截图如下:

此时的登录的用户认证已经失效,可以对客户端做出响应。

以上所述是小编给大家介绍的shiro实现单点登录(一个用户同一时刻只能在一个地方登录),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值