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);
}
网友评论