我已經寫了一個過濾器,它應該在登錄 後創建新會話以修復會話固定。這應該被稱爲只有當用戶登錄到系統:登錄後創建新會話
//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