如何使用spring security获取grails中所有当前登录用户(包括rememberme cookie)的列表

use*_*685 7 authentication cookies grails spring-security

我正在构建一个grails应用程序,它具有spring-security-core 1.2.7.3插件以及spring-security-ui 0.2插件,并希望获得当前登录的所有用户的列表(即有一个目前活跃的会议).用户可以通过登录控制器(daoAuthenticationProvider)登录,也可以通过rememberMe cookie自动登录.我已经实现了下面的代码,使用ConcurrentSessionControlStrategy来创建sessionRegistry:

在/conf/spring/resources.groovy中:

import org.springframework.security.web.authentication.session.ConcurrentSessionControlStrategy
import org.springframework.security.web.session.ConcurrentSessionFilter
import org.springframework.security.core.session.SessionRegistryImpl
import org.springframework.security.web.authentication.session.SessionAuthenticationStrategy

beans = {
userDetailsService(lablore.MyUserDetailsService)

    sessionRegistry(SessionRegistryImpl)

    sessionAuthenticationStrategy(ConcurrentSessionControlStrategy, sessionRegistry) {
        maximumSessions = -1
    }

    concurrentSessionFilter(ConcurrentSessionFilter){
        sessionRegistry = sessionRegistry
        expiredUrl = '/login/concurrentSession'
    }

}
Run Code Online (Sandbox Code Playgroud)

在/plugins/spring-security-core/conf/DefaultSecurityConfig.groovy

useHttpSessionEventPublisher = true
Run Code Online (Sandbox Code Playgroud)

在控制器中:

controller{
    def sessionRegistry

    action(){
        def loggedInUsers = sessionRegistry.getAllPrincipals()
    }
}
Run Code Online (Sandbox Code Playgroud)

它适用于通过登录页面登录的用户 - 通过"注销"链接登出的用户 - 会话过期的用户,但它不适用于使用rememberMe cookie自动进行身份验证的用户.它没有看到他们有一个新创建的会话.如果我理解正确,这是因为与ConcurrentSessionFilter(运行sessionRegistry的那个)相比,RememberMeAuthenticationFilter在过滤器链中"更进一步"?或者,我搞砸了我的配置....

任何有关如何使其工作的帮助都会很棒!

谢谢!!

Joh*_*ved 2

ConcurrentSessionControlStrategy 已弃用

请改用ConcurrentSessionControlAuthenticationStrategy

或者,

您可以实现HttpSessionListener接口,该接口具有sessionCreatedHttpSessionEvent事件)和sessionDestroyedHttpSessionEvent事件)方法,但是您必须添加您使用的类

Web 应用程序中活动会话列表的更改会通知此接口的实现。要接收通知事件,必须在 Web 应用程序的部署描述符中配置实现类。

您可以像这样将实现类添加到您的部署描述符中(即您的 web.xml 文件)

<listener>
   <listener-class>com.hazelcast.web.SessionListener</listener-class>
</listener>
Run Code Online (Sandbox Code Playgroud)

或者使用grails 中的WebXmlConfig插件

您的实现类可能如下所示,另请参阅使用 Spring Security 的在线用户

class WebSessionListener implements HttpSessionListener{

     sessionCreated(HttpSessionEvent se){

          //Checked if user has logged in Here  and keep record 
              HttpSession webSession = se.getSession();

     }

     sessionDestroyed(HttpSessionEvent se){

          //Checked if user has logged in Here  and keep record     
            HttpSession webSession = se.getSession();
     }

}
Run Code Online (Sandbox Code Playgroud)