我正在尝试使用app-info插件获取所有当前的用户会话,但是它无法在Grails 2.5.4
中进行编译。还有另一种获取此信息的方法吗?我是Grails的新手。
This link包含一些信息,包括对app-info插件的引用,但它很旧,我无法编译任何东西。
更新
我已在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();
}
*/
}
已将此添加到/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)
}
}
已将此添加到resources.groovy
myHttpSessionListener(usersessions.MyHttpSessionListener)
/* shiroAuthenticator(ModularRealmAuthenticator) {
authenticationStrategy = ref("shiroAuthenticationStrategy")
authenticationListeners = [ ref("authListener") ]
}
*/
将此添加到我的控制器中:
package usersessions
class NewController extends MyHttpSessionListener {
def index() {
myHttpSessionListener.userLoggedIn("user1")
render "Users "+ getCurrentUserNames()
}
}
它可以编译并运行该应用程序,但是我看不到如何在侦听器中获取会话?加载控制器时出现错误:
没有这样的属性:myHttpSessionListener用于类:usersessions.NewController
更新2
我更改了控制器以使用此代码测试创建会话,并且可以正常工作:
def index() {
String randomString = org.apache.commons.lang.RandomStringUtils.random(3, true, true)
userLoggedIn(randomString)
render "Users "+ getCurrentUserNames()
}