美文网首页
tomcat 多项目session配置

tomcat 多项目session配置

作者: 逝水ly | 来源:发表于2018-06-19 15:59 被阅读0次

    1 修改tomcat的conf目录下的context.xml
    <Context sessionCookiePath="/" sessionCookieName="cysessionid" crossContext="true">

    2 获取session

    ServletContext servletContext = request.getServletContext().getContext("/");//session对应的servlet path
    try {
    if (servletContext instanceof ApplicationContextFacade) {
    // get ApplicationContextFacade context=>ApplicationContext
    if (sApplicationContexField == null) {
    sApplicationContexField = ApplicationContextFacade.class.getDeclaredField("context");
    sApplicationContexField.setAccessible(true);
    }
    ApplicationContext applicationContext=(ApplicationContext) sApplicationContexField.get(servletContext);

                // get ApplicationContext context=>StandardContext
                if(sStandardContextField ==null){
                    sStandardContextField = ApplicationContext.class.getDeclaredField("context");
                    sStandardContextField.setAccessible(true);
                }
                StandardContext standardContext= (StandardContext) sStandardContextField.get(applicationContext);
    
                // get Session Manager
                Session session= standardContext.getManager().findSession(request.getSession().getId());
                if(session!=null&&session.isValid()){
                    return (String) session.getSession().getAttribute(Resource.SESSION_ATTRIBUTE_USRNAME);
                }
            }
        } catch (Exception e) {
            logger.error("getAccountServerSessionUser: error!",e);
        }

    相关文章

      网友评论

          本文标题:tomcat 多项目session配置

          本文链接:https://www.haomeiwen.com/subject/lnnaeftx.html