【问题标题】:grails/spring security - issue on get all connected usersgrails/spring security - 获取所有连接用户的问题
【发布时间】:2014-07-29 18:05:35
【问题描述】:

我按照一篇文章在 Grails 中使用 spring 安全性获取所有已连接的用户,但它在 getAllPrincipals 方法上失败了: “消息:无法在空对象上调用方法 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

【问题讨论】:

    标签: spring grails spring-security


    【解决方案1】:
    1. 安装模板:grails install-templates

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

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

    4. 获取所有用户:

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

    【讨论】:

      【解决方案2】:

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

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

      }

      【讨论】:

        猜你喜欢
        • 2014-06-10
        • 2017-03-20
        • 2012-11-19
        • 2013-11-29
        • 1970-01-01
        • 2011-08-31
        • 2012-12-27
        • 2013-05-25
        • 2015-12-19
        相关资源
        最近更新 更多