Grails 2.5.4: Get all logged in users with HttpSessionListener

主宰稳场 提交于 2021-01-28 13:16:31

问题


I'm trying to get all the current user sessions using the app-info plugin but it doesn't compile in Grails 2.5.4. Is there another way to get this information? I'm new to Grails.

This link has some information including a reference to the app-info plugin but it's old and I wasn't able to get anything to compile.

UPDATE

I've added this custom class in src\groovy\UserSessions:

package usersessions

import javax.servlet.http.HttpSessionEvent
import javax.servlet.http.HttpSessionListener

//import org.apache.shiro.subject.PrincipalCollection
//import org.apache.shiro.subject.SimplePrincipalCollection;
//import org.apache.shiro.subject.support.DefaultSubjectContext

class MyHttpSessionListener implements HttpSessionListener {
    private static List activeUsers = Collections.synchronizedList(new ArrayList());
@Override
public void sessionCreated(HttpSessionEvent event) { }

@Override
public void sessionDestroyed(HttpSessionEvent event) {
    String userName = getCurrentUserName(event)
    if (userName == null) {
        return
    }
    MyHttpSessionListener.userLoggedOut(userName)
}

public static void userLoggedIn(String userName) {
    if (!activeUsers.contains(userName)) {
        activeUsers.add(userName)
    }
}

public static void userLoggedOut(String userName) {
    activeUsers.remove(userName)
}

public static List getCurrentUserNames() {
    return Collections.unmodifiableList(activeUsers);
}
/* 
private String getCurrentUserName(HttpSessionEvent event) {
    PrincipalCollection currentUser = (PrincipalCollection) event.getSession().getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY);
    if (currentUser == null) {
        return null
    }
    return (String) currentUser.getPrimaryPrincipal();
}
*/

}

Added this to /scripts/_Events.groovy

import groovy.xml.StreamingMarkupBuilder

eventWebXmlEnd = {String tmpfile ->
    def root = new XmlSlurper().parse(webXmlFile)
    root.appendNode {
       'listener' {
       'listener-class' (
           'usersessions.MyHttpSessionListener'
       )
        }
    }

    webXmlFile.text = new StreamingMarkupBuilder().bind {
        mkp.declareNamespace(
            "": "http://java.sun.com/xml/ns/javaee")
        mkp.yield(root)
    }
}

Added this to resources.groovy

myHttpSessionListener(usersessions.MyHttpSessionListener)
/*      shiroAuthenticator(ModularRealmAuthenticator) {
        authenticationStrategy = ref("shiroAuthenticationStrategy")
        authenticationListeners = [ ref("authListener") ]
    }
*/

Added this to my controller:

package usersessions

class NewController extends MyHttpSessionListener {
    def index() {
        myHttpSessionListener.userLoggedIn("user1")
        render "Users "+ getCurrentUserNames()
    }
 }

It compiles and the app runs but I don't see how to get sessions in the listener? The controller gives an error when I load it:

No such property: myHttpSessionListener for class: usersessions.NewController

UPDATE 2

I changed the controller to use this code to test creating a sessions and it works:

def index() {        
    String randomString = org.apache.commons.lang.RandomStringUtils.random(3, true, true)
    userLoggedIn(randomString)  
    render "Users "+ getCurrentUserNames()
}

来源:https://stackoverflow.com/questions/50959545/grails-2-5-4-get-all-logged-in-users-with-httpsessionlistener

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!