Grails는 세션 ID를 통해 세션 객체를 가져옵니다.

Grails는 세션 ID를 통해 세션 객체를 가져옵니다.


사고방식: 감청에 사용할 클래스를 사용자 정의합니다session, 모두session입금map중,sessionId 읽기key

스니퍼 클래스 SessionTracker 만들기

package com.session

import org.springframework.beans.BeansException
import org.springframework.context.ApplicationContext
import org.springframework.context.ApplicationContextAware
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

class SessionTracker implements HttpSessionListener, ApplicationContextAware {

    private static final ConcurrentMap sessions = new ConcurrentHashMap();

    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)
    }
}

grails-app/conf/resources.groovy에 등록

import com.session.SessionTracker

// Place your Spring DSL code here
beans = {

    //  session 
    sessionTracker(SessionTracker)
}

세션 가져오기

package com.genee

import org.springframework.web.context.request.RequestContextHolder

import javax.servlet.http.HttpSession

class HiController {

    //  
    def sessionTracker

    def index() {

        //  session
        def sessionId = RequestContextHolder.currentRequestAttributes().getSessionId()

        println " sessionId:$sessionId"

        //  sessionId session 
        HttpSession httpSession = sessionTracker.getSessionById(sessionId).getId()
        println " session :"+httpSession.getId()

        //  session 
        sessionTracker.getSessionById(sessionId).invalidate()
        render sessionId
    }
}

좋은 웹페이지 즐겨찾기