2016-05-26 14 views
0

Ich habe Grails-Anwendung, mit SessionRegistry kann ich SessionId bekommen.Wie bekomme ich HttpSession von SessionId in Grails-Anwendung

Nun, wie kann ich HttpSession von dieser SessionId bekommen.

+1

Mit 'sessionFactory' meinen Sie die Hibernate SessionFactory? Das hat nichts mit HTTP-Sessions zu tun, die Namen sind nur zufällig ähnlich. Das "Sitzungs" -Konzept wird an verschiedenen Orten verwendet, z. JMS und andere. Sie sind nicht verwandt und teilen keine IDs. –

Antwort

-1

Versuchen Sie folgendes:

def sessions = ContextListener.instance().getSessions() 
def sessionToInvalidate = sessions.find{it.id == sessionId} 

Update: als Burt Beckwith erwähnt, ContextListener ist nicht standart Klasse. Es ist jedoch einfach zu implementieren. Sie können sehen, wie es in App Info grails Pluginhere

+0

'ContextListener' ist keine Standardklasse. Sie muss in einer App verwendet werden, die in web.xml als Listener registriert ist, um Sitzungen im Auge zu behalten. Ohne diese Klassenquelle ist diese Antwort überhaupt nicht nützlich. –

0

implementiert ist, wenn Sie die HttpSession dann wollen, wie dies etwa:

import org.springframework.beans.BeansException 
import org.springframework.context.ApplicationContext 
import org.springframework.context.ApplicationContextAware 
import org.springframework.stereotype.Component 
import org.springframework.web.context.WebApplicationContext 

import javax.servlet.http.HttpSession 
import javax.servlet.http.HttpSessionEvent 
import javax.servlet.http.HttpSessionListener 
import java.util.concurrent.ConcurrentHashMap 
import java.util.concurrent.ConcurrentMap 

@Component 
class SessionTracker implements HttpSessionListener, ApplicationContextAware { 

    private static final ConcurrentMap<String, HttpSession> sessions = new ConcurrentHashMap<String, HttpSession>(); 

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { 
     def servletContext = ((WebApplicationContext) applicationContext).getServletContext() 
     servletContext.addListener(this); 
    } 

    void sessionCreated(HttpSessionEvent httpSessionEvent) { 
     sessions.putAt(httpSessionEvent.session.id, httpSessionEvent.session) 
    } 

    void sessionDestroyed(HttpSessionEvent httpSessionEvent) { 
     sessions.remove(httpSessionEvent.session.id) 
    } 

    HttpSession getSessionById(id) { 
     sessions.get(id) 
    } 
} 

Sobald Sie dies in src/groovy fallen sollte es automatisch zur Verfügung stehen in Ihrem Frühlingskontext. Sie können es so verwenden, nachdem Sie es in einen Controller oder Service injiziert haben.

sessionTracker.getSessionById('sessionId')