在我的应用程序中,我需要以编程方式注销用户,而不是当前用户。我能够终止其 session ,但这不会强制该特定用户注销。所以我的问题是,如何强制注销用户的下一个请求?现在是我的代码:

sessionRegistry.getAllPrincipals().each {
   principal = it
   sessionRegistry.getAllSessions(it, false).each {
       if (principal.username == userSec.username) {
           it.expireNow()
       }
   }
}

我的资源中有这个。
sessionRegistry(SessionRegistryImpl)
    sessionAuthenticationStrategy(ConcurrentSessionControlStrategy, sessionRegistry) {
        maximumSessions = -1
    }
    concurrentSessionFilter(ConcurrentSessionFilter){
        sessionRegistry = sessionRegistry
        expiredUrl = '/login/concurrentSession'
    }

最佳答案

使用 session 机制是不可能的。您必须使用可以保留用户的存储介质。该介质可以是RAM中的单例(如ConcurrentHashMap实例),也可以是DB,具体取决于您的集群体系结构。

然后在每次调用springSecurityService.currentUser时(例如在Spring Security核心插件中),您必须检查sessionRegistry是否包含该用户,如果不包含该用户,则使 session 或该用户无效

10-05 23:29
查看更多