我用shiro+redis做session共享,用户登陆后重启服务器无法获取ServletContext中得值
用户登陆后将用户得sessionID与用户绑定
private void saveLoginSession(String username){
ServletContext application = Mvcs.getHttpSession().getServletContext();
Map<String, String> loginUsers = (Map<String, String>) application
.getAttribute("loginUsers");
String sessionId = Mvcs.getHttpSession().getId();
log.debug("sessionId=============:" + sessionId);
log.debug("loginUsers:" + loginUsers);
if (loginUsers == null || loginUsers.size() == 0) {
loginUsers = new HashMap<>(10);
loginUsers.put(username, sessionId);
application.setAttribute("loginUsers", loginUsers);
} else {
loginUsers.put(username, sessionId);
}
}
再重启服务器后判断用户登陆无法获取ServletContext中设置得loginUsers,但是可以获取Attribute(Auth.FRONT_KEY)这个设置得值
public static Boolean isLogin() {
HttpSession session = Mvcs.getHttpSession();
ServletContext application = session.getServletContext();
if (application == null) {
return false;
}
Map<String, String> loginUsers = (Map<String, String>) application.getAttribute("loginUsers");
if (loginUsers == null || loginUsers.size() == 0) {
return false;
}
String sessionId = session.getId();
Auth auth = (Auth) session.getAttribute(Auth.FRONT_KEY);
if (auth == null) {
return false;
}
String userSessionId = loginUsers.get(auth.name);
if (userSessionId != null && !sessionId.equals(userSessionId)) {
session.removeAttribute(Auth.FRONT_KEY);
return false;
}
return true;
}