对于涉及会话并发控制的非平凡设置,身份验证不等同于登录。例如,用户可以成功进行身份验证,但如果这是第二次登录尝试并max-sessions="1"
,则仍会重定向到注销或错误页面。如果我有登录时需要调用的登录后逻辑(不是身份验证),那么将此逻辑合并到基于Spring Security的Web应用程序中的最佳方法是什么?基于我对框架的有限理解,我提出的解决方案是实现我自己的自定义ConcurrentSessionControlAuthenticationStrategy
适配器,扩展框架的ConcurrentSessionControlAuthenticationStrategy
并将其注入我的Spring Security配置中的CompositeSessionAuthenticationStrategy
XML。我创建了一个1-arg构造函数和onAuthentication
方法。 onAuthentication
在调用super.onAuthentication
之前执行我的登录后处理。有更好的方法吗?
我的自定义onAuthentication
方法类似于
.
.
if (sessionCount < allowedSessions) {
// Record login timestamp in database
Date now = new Date();
userDao.setLastLogin(now);
userDao.save();
}
super.onAuthentication(...);
因为在父级中声明sessionRegistry
成员变量private
(而不是protected
),我必须声明我自己的sessionRegistry
并在1-内部初始化它arg构造函数,以便我的onAuthentication
方法可以访问它。