我发表了一篇文章,让所有连接的用户都在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
答案 0 :(得分:0)
安装模板:grails install-templates
web.xml
位于:src/templates/war
确保侦听器标记位于servlet标记之前:
获取所有用户:
sessionRegistry.getAllPrincipals().collect{User.get(it.id)}
答案 1 :(得分:0)
我发现了这个问题,我的解决方案是将sessionRegistry声明为变量类 class DummyController {
def sessionRegistry
def index() {
def users= new ArrayList<User>(sessionRegistry.getAllPrincipals())
}
}