Tomcat:如何从servlet访问(会话)管理器

Jur*_*riy 6 session tomcat servlets java-ee

我需要从Tomcat中的servlet(或过滤器)访问Manager,以通过自定义会话ID加载自定义会话.

回答你的下一个问题:为什么我需要它.Flash中存在一个旧错误,导致它从IE发送cookie,而不是从当前浏览器发送.所以,如果我在FF中并且我正在尝试使用SWFUpload上传文件,那么我最终会遇到错误的会话和错误.

我想将magic参数添加到应该覆盖默认(错误)会话ID的POST,然后加载自定义会话而不是Tomcat加载的会话.我不能使用URL重写,因为cookie首先被解析,当flash从IE发送错误的cookie时,Tomcat不会尝试从url重写的地址加载会话.

我很感激任何其他提示如何从上下文访问管理器或原始问题的解决方案.

谢谢,Juriy

Mac*_*ski 6

相对于伊霍尔的代码,该代码使用越来越少一点抽象Manager来自HttpSession:

private Manager manager(HttpSession session) throws Exception {

    Field facadeSessionField = StandardSessionFacade.class.getDeclaredField("session");
    facadeSessionField.setAccessible(true);
    StandardSession stdSession = (StandardSession) facadeSessionField.get(session);

    return stdSession.getManager();
}
Run Code Online (Sandbox Code Playgroud)


Iho*_* M. 5

对于Tomcat:

   ApplicationContextFacade appContextFacadeObj = (ApplicationContextFacade)    request.getSession().getServletContext();

    try
    {
        Field applicationContextField = appContextFacadeObj.getClass().getDeclaredField("context");
        applicationContextField.setAccessible(true);
        ApplicationContext appContextObj = (ApplicationContext) applicationContextField.get(appContextFacadeObj);
        Field standardContextField = appContextObj.getClass().getDeclaredField("context");
        standardContextField.setAccessible(true);
        StandardContext standardContextObj = (StandardContext) standardContextField.get(appContextObj);
        Manager persistenceManager = standardContextObj.getManager();
    }
    catch(SecurityException e)
    {
        logger.error(e);
    }
    catch(NoSuchFieldException e)
    {
        logger.error(e);
    }
    catch(IllegalArgumentException e)
    {
        logger.error(e);
    }
    catch(IllegalAccessException e)
    {
        logger.error(e);
    }
Run Code Online (Sandbox Code Playgroud)


Boz*_*zho 4

它应该可以通过实现来访问ServletContext。获取 tomcat 的来源来检查,或者使用反射来获取上下文的所有字段。您可能需要进行大量反思才能找到经理。

(我找不到管理器是否暴露在JNDI中,但你也可以在那里查看)