grails / spring security - 获取所有连接用户的问题

时间:2014-06-09 08:16:02

标签: spring grails spring-security

我发表了一篇文章,让所有连接的用户都在Grails中获得Spring securty,但是在getAllPrincipals方法失败了: "消息:无法在null对象上调用方法getAllPrincipals()"

代码:

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 = {

    sessionRegistry(SessionRegistryImpl)

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

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

的web.xml

<listener>
        <listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
</listener> 

控制器

def sessionRegistry  
def users= new ArrayList<User>(sessionRegistry.getAllPrincipals())

http://classpattern.com/spring-security-sessionregistry-on-grails.html#.U5GICfl_uhF

2 个答案:

答案 0 :(得分:0)

  1. 安装模板:grails install-templates

  2. web.xml位于:src/templates/war

  3. 确保侦听器标记位于servlet标记之前:

  4. 获取所有用户:

    sessionRegistry.getAllPrincipals().collect{User.get(it.id)}

答案 1 :(得分:0)

我发现了这个问题,我的解决方案是将sessionRegistry声明为变量类 class DummyController {

def sessionRegistry

def index() {        
    def users= new ArrayList<User>(sessionRegistry.getAllPrincipals())

}

}