| Blog信息 |
|
blog名称: 日志总数:1304 评论数量:2242 留言数量:5 访问次数:7688242 建立时间:2006年5月29日 |

| |
|
[Java Open Source]ConcurrentSession的使用(Acegi) 软件技术, 电脑与网络
lhwork 发表于 2006/6/13 11:07:30 |
| Acegi可以限制同一个用户名在同一时刻成功登录同一个应用的次数。例如,你可以阻止某个用户名在已经成功登录进web应用的同时再进行一次成功的登录。当然,这个允许同时成功登录的次数你是可以自己设定的。
为支持并发(concurrent)session支持,你需要加以下代码到web.xml:
<listener> <listener-class>org.acegisecurity.ui.session.HttpSessionEventPublisher</listener-class></listener>
另外,你还需要在你的过滤器链(FilterChainProxy)里加上
org.acegisecurity.concurrent.ConcurrentSessionFilter。
ConcurrentSessionFilter在最简单的情况下只需要一个属性:sessionRegistry。这个属性应该指向一个
SessionRegistryImpl的实例。
每次HttpSession开始或者结束的时候,web.xml中的HttpSessionEventPublisher都会发布一个
ApplicationEvent事件到Spring的ApplicationContext。这是至关重要的,因为这个机制允许在session结束的
时候,SessionRegistryImpl会得到通知。这解释了为什么我们需要在ConcurrentSessionFilter中指向
SessionRegistryImpl的实例。
你还需要在xxx-servlet.xml里面把ConcurrentSessionControllerImpl编织起来,把它注入到你的ProviderManager里面去。
<bean id="authenticationManager" class="org.acegisecurity.providers.ProviderManager"> <property name="providers"> <!-- 你的 providers 放在这里 --> </property> <property name="sessionController"><ref bean="concurrentSessionController"/></property></bean><bean id="concurrentSessionController" class="org.acegisecurity.concurrent.ConcurrentSessionControllerImpl"> <property name="maximumSessions"><value>1</value></property> <property name="sessionRegistry"><ref local="sessionRegistry"/></property></bean><bean id="sessionRegistry" class="org.acegisecurity.concurrent.SessionRegistryImpl"/>
ProviderManager在默认情况下是不对并发登录进行检验的(private
ConcurrentSessionController sessionController = new
NullConcurrentSessionController();)。
org.acegisecurity.concurrent.NullConcurrentSessionController
public class NullConcurrentSessionController implements ConcurrentSessionController { //~ Methods ================================================================ public void checkAuthenticationAllowed(Authentication request) throws AuthenticationException {} public void registerSuccessfulAuthentication(Authentication authentication) {}}
以上是spring对于concurrentsession支持部分的文档,注意到文档说concurrentSessionController
只需要一个sessionRegistry实例的属性,但它应用时还使用了maximumSessions属性,把它赋值为1。在
ConcurrentSessionControllerImpl的源代码中,这个值初始值其实也是1的(private int
maximumSessions = 1;)。
它的用法和用处是这样的:
可以给它赋值-1,那么将不会限制并发登录。可以给它赋大于0值,这个数字限制相同用户名同时成功并发登录同一个web应用的次数。Assert.isTrue(maximumSessions != 0,"MaximumLogins must be either -1
to allow unlimited logins, or a positive integer to specify a maximum");
例如,如果这个值是3,那么用户名为ifunsu@gmail.com的用户可以同时在3个地点同时成功登录进web应用,只要密码正确。
不可以给它赋值为0,这是不允许的。英文文档 http://www.acegisecurity.org/docbook/acegi.html#security-authentication-concurrent-login |
|
|