最近一直使用spring security2.0.x配置一些关于登陆认证方面的使用,我们的项目中有几个特殊的场景,记录下来,这些场景是我在百度/google没有搜到的(maybe我的搜索策略太低级)。

        场景1.某些用户只允许允许单点登陆(即不允许同一用户在多台机器同时在线),某些用户允许多点登陆(即允许同一用户在多台机器上同时在线)。

     解决方法:主要是org.springframework.security.concurrent.ConcurrentSessionControllerImpl中的getMaximumSessionsForThisUser方法进行改造。根据我的场景,针对某些用户返回1,某些用户返回-1(表示session使用数无限制)。

     另需要在Application中添加并行会话控制。

 
  
  1. <authentication-manager alias="authenticationManager" session-controller-ref="concurrentSessionController"/>  
  2. <beans:bean id="concurrentSessionController" class="xx.xxx.ConcurrentSessionControllerImpl"> 
  3. <beans:property name="maximumSessions" value="1"/> 
  4. <beans:property name"sessionRegistry"  ref="sessionRegistry"/> 
  5. <beans:property name="exceptionIfMaximumExceeded" value="false" />  
  6. </beans:bean> 

需要注意的一点:spring security xsd的版本至少2.0.2,才能支持session-control-ref。(由于之前一直使用2.0.1,找了很久问题所在,才发现是版本不对)。

        场景2:我们前台使用了AJAX方案,他的确带来不一样的用户体验,但是有一个问题:session过期问题对AJAX的影响。我们希望对于session过期的用户在其再次进行请求后台数据(主要是js代码和action操作)的时候,将页面重定向到登陆页面。

         解决方案:将concurrentSessionFilter添加到chainFilter中,并对org.springframework.security.concurrent.ConcurrentSessionControllerImpl中的doFilterHttp方法进行改造。我们的简单实现:对于request的header中"x-requested-with"进行检查,有则表示是action请求,我们返回自定义的status,在action异常处理中对其进行解析,重定向到登陆界面;没有则表示是请求js代码,我们返回expireUrl指向的JS代码(这段代码添加了重定向到主界面的代码)。

 
  
  1. <beans:bean id="concurrentSessionFilter"   class="xx.xxxx.ConcurrentSessionFilterImpl"> 
  2.     <custom-filter position="CONCURRENT_SESSION_FILTER" /> 
  3.     <beans:property name="sessionRegistry" ref="sessionRegistry" /> 
  4.     <beans:property name="expiredUrl" value="/error.js"/> 
  5. </beans:bean> 

两天分别实现了这两种场景,方法有些粗暴!但是实用!要开始转向USB-key的工作了,了以总结一下四天所学及应用。