`
确实比较男
  • 浏览: 112396 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

会话管理Session Management

阅读更多

手赚入口:http://szjx.top

 

HttpSessionEventPublisher

实现了HttpSessionListener接口,监听session的创建和销毁事件,通过ApplicationContext发布对应的事件HttpSessionCreatedEvent  HttpSessionDestroyedEvent
//监听session创建事件
public void sessionCreated(HttpSessionEvent event) {
    HttpSessionCreatedEvent e = new HttpSessionCreatedEvent(event.getSession());
    //发布事件
    getContext(event.getSession().getServletContext()).publishEvent(e);
}

//监听session销毁事件
public void sessionDestroyed(HttpSessionEvent event) {
   HttpSessionDestroyedEvent e = new HttpSessionDestroyedEvent(event.getSession());
   //发布事件
   getContext(event.getSession().getServletContext()).publishEvent(e);
}

SessionRegistry , SessionInformation

SessionInformation :记录认证用户的session信息 。

lastRequest:最后一次访问次数

principal:认证用户信息

sessionId:session的id

expired:是否过期

SessionRegistry :

 

    保存了所有认证成功后用户的SessionInformation信息,每次用户访问服务器的会从sessionRegistry中查询出当前用户的session信息 ,判断是否过期以及刷新最后一次方法时间,默认的实现类SessionRegistryImpl,监听了session的销毁事件,若销毁,那么删除掉session信息,有两个属性:

/** <principal:Object,SessionIdSet>  以认证用户对象做key,多个sessionId 为value 。一个用户可以对应多个不同的session,表示同一个帐号可以同时在不同的浏览器上登录,可以配置最大允许同时登录的数*/
private final ConcurrentMap<Object,Set<String>> principals = new ConcurrentHashMap<Object,Set<String>>();
/** <sessionId:Object,SessionInformation> 以sessionid为key SessionInformation为value */
private final Map<String, SessionInformation> sessionIds = new ConcurrentHashMap<String, SessionInformation>();

 

SessionAuthenticationStrategy

void onAuthentication(Authentication authentication, HttpServletRequest request, HttpServletResponse response)  throws SessionAuthenticationException;

 

有多个实现类:

ChangeSessionIdAuthenticationStrategy:

调用HttpServletRequest的changeSessionId方法改变sessionid

SessionFixationProtectionStrategy:

首先让原来的session过期,然后创建一个新的session,把原来session的属性拷贝到新的session中

http://xpenxpen.iteye.com/blog/1664075

RegisterSessionAuthenticationStrategy:

用户认证成功后sessionRegistry调用registerNewSession,保存用户的信息和session

ConcurrentSessionControlAuthenticationStrategy:

允许用户同时在线数,有一个maximumSessions属性,默认是1。通过sessionRegistry判断用户数是否已经超过了最大允许数,若超过了,那么就让最近一个的session过期(让上一个用户强制下线)

 

CompositeSessionAuthenticationStrategy:组合使用多个SessionAuthenticationStrategy

 

 

实现控制同时在线用户数

 1.    在web.xml中添加listener

 

 

<listener>
  <listener-class>
     org.springframework.security.web.session.HttpSessionEventPublisher
  </listener-class>
</listener>

 2.  在security.xml中添加代码:

 

<security:http>
    <security:intercept-url pattern="/**" access="ROLE_USER"/>
    <security:form-login/>
    <security:logout logout-url="/logout"/>
    <security:session-management>
        <security:concurrency-control max-sessions="1"/>
    </security:session-management>
</security:http>

 默认创建的SessionAuthenticationStrategy是组合CompositeSessionAuthenticationStrategy:设置的SessionAuthenticationStrategy有:RegisterSessionAuthenticationStrategy  ConcurrentSessionControlAuthenticationStrategy  SessionFixationProtectionStrategy

 

 

 

 小忆智库:http://blog.xiaoyizhiku.cn/

 

 

0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics