细说shiro之六:session管理

技术分享图片
官网:https://shiro.apache.org/

我们先来看一下shiro中关于Session和Session Manager的类图。
技术分享图片
技术分享图片

如上图所示,shiro自己定义了一个新的Session接口,用于统一操作接口,并通过SessionManager实现Session管理。
其中的3个实现类HttpServletSession,SimpleSession和StoppingAwareProxiedSession是我们经常需要打交道的。

HttpServletSession

首先,我们来看看org.apache.shiro.web.session.HttpServletSession的实现。

public HttpServletSession(HttpSession httpSession, String host) {
if (httpSession == null) {
String msg = "HttpSession constructor argument cannot be null.";
throw new IllegalArgumentException(msg);
}
if (httpSession instanceof ShiroHttpSession) {
String msg = "HttpSession constructor argument cannot be an instance of ShiroHttpSession. This " +
"is enforced to prevent circular dependencies and infinite loops.";
throw new IllegalArgumentException(msg);
}
this.httpSession = httpSession;
if (StringUtils.hasText(host)) {
setHost(host);
}
}

显然,HttpServletSession只是简单对javax.servlet.http.HttpSession进行了封装,即:
在Web应用程序中,所有对Session相关的操作最终都是对javax.servlet.http.HttpSession进行的。
技术分享图片
通过对上述Subject.login()的时序图分析可以知道:
在Web应用程序中,Shiro确实是通过ServletContainerSessionManager获取到容器创建的HttpSession再封装为HttpServletSession的。
也就是说,Subjec.login()登录成功后用户的认证信息实际上是保存在HttpSession中的。如果此时Web应用程序部署了多实例,必须要进行Session同步。
我们知道,SecurityManager是整个Shiro框架的核心控制器,在SpringMVC中集成Shiro时,就需要明确配置对应的SecurityManager。

<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<!-- Single realm app. If you have multiple realms, use the ‘realms‘ property instead. -->
<property name="realm" ref="myRealm" />
</bean>

而在org.apache.shiro.web.mgt.DefaultWebSecurityManager的实现中,使用的SessionManager就是ServletContainerSessionManager。

public DefaultWebSecurityManager() {
super();
((DefaultSubjectDAO) this.subjectDAO).setSessionStorageEvaluator(new DefaultWebSessionStorageEvaluator());
this.sessionMode = HTTP_SESSION_MODE;
setSubjectFactory(new DefaultWebSubjectFactory());
setRememberMeManager(new CookieRememberMeManager());
setSessionManager(new ServletContainerSessionManager()); // 配置Session Manager
}

SimpleSession

shiro具备完善的Session管理机制,当在命令行程序中使用Shiro框架时,同样可以执行与Web应用程序一样的Session操作。
此时,Shiro实际上使用SimpleSession实现。

StoppingAwareProxiedSession

实际上,StoppingAwareProxiedSession仅仅是一个Session包装类,即:
无论是HttpServletSession还是SimpleSession,在执行Subject.login()时保存到Subject中的Session都是StoppingAwareProxiedSession对象。

private class StoppingAwareProxiedSession extends ProxiedSession {
private final DelegatingSubject owner;

private StoppingAwareProxiedSession(Session target, DelegatingSubject owningSubject) {
super(target);
owner = owningSubject;
}
public void stop() throws InvalidSessionException {
super.stop();
owner.sessionStopped();
}
}

【参考】
https://shiro.apache.org/session-management.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值