org.apache.shiro.SecurityUtils.getSubject().getSession()

简介: Shiro的Session管理概述和配置使用n概述  Shiro提供安全框架界独一无二的东西:一个完整的企业级Session 解决方案,可以为任意的应用提供session支持,包括web和非web应用,并且无需部署你的应用程序到Web 容器或使用EJB容器。

Shiro的Session管理

概述和配置使用

n概述

  Shiro提供安全框架界独一无二的东西:一个完整的企业级Session 解决方案,可以为任意的应用提供session支持,包括web和非web应用,并且无需部署你的应用程序到Web 容器或使用EJB容器。

n基本使用

  可以通过与当前执行的Subject 交互来获取Session:

?
1
2
3
  Subject currentUser = SecurityUtils.getSubject();  
  Session session = currentUser.getSession();
  session.setAttribute( "someKey" , someValue);

n关于SessionManager

  SessionManager是用来管理Session的组件,包括:创建,删除,inactivity(失效)及验证,等等。SessionManager 也是一个由SecurityManager 维护的顶级组件。

  shiro提供了默认的SessionManager实现,一般没有必要自定义这个。

n设置Sessioin的过期时间

  Shiro 的SessionManager 实现默认是30 分钟会话超时。

  你可以设置SessionManager 默认实现的globalSessionTimeout 属性来为所有的会话定义默认的超时时间。例如,

?
1
2
3
[main]
# 3,600,000 milliseconds = 1 hour
securityManager.sessionManager.globalSessionTimeout  =  3600000

nSessioin的事件监听

      你可以实现SessionListener 接口(或扩展易用的SessionListenerAdapter)并与相应的会话操作作出反应。 配置示例:

?
1
2
3
4
[main]
aSessionListener  =  com.foo.my.SessionListener
anotherSessionListener  =  com.foo.my.OtherSessionListener
securityManager.sessionManager.sessionListeners  =  $aSessionListener, $anotherSessionListener

 

SessionDAO

n概述

  每当一个会话被创建或更新时,它的数据需要持久化到一个存储位置以便它能够被稍后的应用程序访问,实现这个功能的组件就是SessionDAO。

  你能够实现该接口来与你想要的任何数据存储进行通信。这意味着你的会话数据可以驻留在内存中,文件系统,关系数据库或NoSQL 的数据存储,或其他任何你需要的位置。

n基本配置 
  SessionDAO是作为一个属性配置在默认的SessionManager 实例上

?
1
2
3
[main]
sessionDAO  =  com.foo.my.SessionDAO
securityManager.sessionManager.sessionDAO  =  $sessionDAO

 这种SessionDAO主要在本地应用中起作用。

n基于EHCache的SessionDAO,基本配置如下:

?
1
2
3
4
5
[main]
sessionDAO  =  org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO
securityManager.sessionManager.sessionDAO  =  $sessionDAO
cacheManager  =  org.apache.shiro.cache.ehcache.EhCacheManager
securityManager.cacheManager  =  $cacheManager

nShiro提供了默认的EHCache的配置xml,如果你要配置自己的EHCache.xml,需要注意以下几点:

1:overflowToDisk=“true” - 这确保当你溢出进程内存时,会话不丢失且能够被序列化到磁盘上。

2: eternal=“true” - 确保缓存项(Session 实例)永不过期或被缓存自动清除。这是很有必要的,因为Shiro 基于计划过程完成自己的验证。如果我们关掉这项,缓存将会在Shiro 不知道的情况下清扫这些Sessions,这可能引起麻烦。

3:如果你想使用一个不同的名字而不是默认的,你可以在EnterpriseCacheSessionDAO 上配置名字,例如:sessionDAO.activeSessionsCacheName = myname

  只要确保在ehcahe.xml 中有一项与这个名字匹配

 

Web应用中的Session

       n在web应用上,默认使用的是容器的会话,如果你想基于Web 应用程序启用SessionDAO 来自定义会话存储或会话群集,你将不得不首先配置一个本地的Web 会话管理器。例如:

?
1
2
3
4
5
[main]
sessionManager = org.apache.shiro.web.session.mgt.DefaultWebSessionManager
securityManager.sessionManager  =  $sessionManager
# Configure a SessionDAO and then set it:
securityManager.sessionManager.sessionDAO  =  $sessionDAO

      n在web应用上,如果想要在每一个请求的基础上启用或禁用会话的创建,可以在配置中的[urls] 里面,为相应的url设置一个noSessionCreation过滤器,如下:

?
1
2
[urls]
/ rest / * *  =  noSessionCreation, authcBasic

 自定义SessionDAO

       n在某些场景中,我们需要管理用户的Session信息,比如把Session信息放到数据库中,这样就可以记录一个操作日志,或是统计在线人员等等。

       n自定义SessionDAO也非常简单,通常是继承AbstractSessionDAO,实现对Session数据的CRUD即可,简单示例如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public  class  MySessionDAO  extends  AbstractSessionDAO{
private  Map<Serializable,Session> map =  new  HashMap<Serializable,Session>();
   public  void  update(Session session)  throws  UnknownSessionException {
   System.out.println( "now update session" );
   map.put(session.getId(),session);
   }
   public  void  delete(Session session) {
   System.out.println( "now delete session" ); 
   map.remove(session.getId());
   }
   public  Collection<Session> getActiveSessions() {
   System.out.println( "now getActiveSessions session" );
   return  map.values();
   }
  
   protected  Serializable doCreate(Session session) {
   System.out.println( "now doCreate session" );
   Serializable sessionId = generateSessionId(session);
       assignSessionId(session, sessionId);
       map.put(sessionId, session);
    
      return  sessionId;
   }
   protected  Session doReadSession(Serializable sessionId) {
   System.out.println( "now doReadSession session" );
   return  map.get(sessionId);
   }
}

n基本的配置示例:

?
1
2
sessionDAO = cn.javass.hello.MySessionDAO
securityManager.sessionManager.sessionDAO = $sessionDAO

 私塾在线 原创,转载请注明 http://sishuok.com/forum/blogPost/list/0/7458.html



使用shirossion时需要把配置中存session的地方改成本地机或者是ip才可以取到,shirosession比session稳定

sessionId.cookie.domain=localhost

目录
相关文章
|
5天前
|
Java 数据库连接 数据库
org.apache.ibatis.session.AutoMappingUnknownColumnBehavior
org.apache.ibatis.session.AutoMappingUnknownColumnBehavior
14 0
|
6月前
|
XML Java 数据库连接
解决在mybatis中出现的org.apache.ibatis.exceptions.PersistenceException~
解决在mybatis中出现的org.apache.ibatis.exceptions.PersistenceException~
627 0
|
5月前
解决:org.springframework.web.method.annotation.MethodArgumentTypeMismatchExceptio
解决:org.springframework.web.method.annotation.MethodArgumentTypeMismatchExceptio
141 0
|
Java 数据库连接 程序员
异常:org.apache.ibatis.reflection.ReflectionException
异常:org.apache.ibatis.reflection.ReflectionException
121 0
|
SQL Java 数据库连接
org.apache.ibatis.reflection.ReflectionException: There is no getter for propert
org.apache.ibatis.reflection.ReflectionException: There is no getter for propert
104 0
org.apache.ibatis.reflection.ReflectionException: There is no getter for propert
|
存储 安全 Java
Apache shiro介绍
当你尝试保护你的程序时候你会被困扰吗?你会觉得现有的java安全方案难以使用并且将来还会困惑你?这篇文章介绍Apach shiro,一个简单而又强大的保护程序安全的安全框架。它解释了Apache的项目目标。架构原理和如何使用shiro保护你的程序。
243 0
|
数据库 数据安全/隐私保护
|
Java 应用服务中间件 Android开发
使用 JSTL1.2 报错 org.apache.catalina.core.StandardWrapperValve.invoke...
导入所需依赖并引用如下核心标签库后报错,无法使用JSTL的原因及解决方案
使用 JSTL1.2 报错 org.apache.catalina.core.StandardWrapperValve.invoke...
|
XML Java 程序员
org.apache.ibatis.binding.BindingException
org.apache.ibatis.binding.BindingException
144 0
org.apache.ibatis.binding.BindingException
|
Java Apache
org.apache.commons.fileupload.FileUploadBase$SizeL
上传94M的视频出现异常如下: [@APPNAME@] ERROR [http-80-3] MultiPartRequest.parse(130) | org.apache.commons.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (102147245) exceeds the configured maximum (50097152) [@APPNAME@] ERROR [http-80-3] FileUploadInterceptor
337 0