我已经写了一个过滤器,它应该在登录 后创建新会话以修复会话固定。这应该被称为只有当用户登录到系统:登录后创建新会话
//variables
public class GenerteNewSessionFilter implements Filter {
public static final String NEW_SESSION_INDICATOR = "cab";
// destroy
public void destroy() {
// TODO Auto-generated method stub
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
if (httpRequest.getSession(false) != null && httpRequest.getSession(false).getAttribute(NEW_SESSION_INDICATOR) != null) {
// copy session attributes from new session to a map.
HttpSession session = httpRequest.getSession();
// HashMap old = new HashMap();
HashMap<String, Object> old = new HashMap<String, Object>();
Enumeration keys = (Enumeration) session.getAttributeNames();
while (keys.hasMoreElements()) {
String key = (String) keys.nextElement();
if (!NEW_SESSION_INDICATOR.equals(key)) {
old.put(key, session.getAttribute(key));
session.removeAttribute(key);
}
}
// invalidation session and create new session.
session.invalidate();
session = httpRequest.getSession(true);
// copy key value pairs from map to new session.
for (Map.Entry entry : old.entrySet()) {
session.setAttribute((String) entry.getKey(), entry.getValue());
}
}
}
// initiatiliazion
public void init(FilterConfig filterConfig) throws ServletException {
}
}
但我想只有一次,当用户登录到应用程序请指导我怎样才能实现它执行它。
谢谢。
如果您将该代码放在处理用户的凭证POST过程的地方,该怎么办?基本上,一旦您确认用户是您认为他们的人,就会使会话无效。 – 2014-08-29 23:26:41