I have written one filter which should create a new session after login to fix session fixation. This should be called only when user logins into the system:
//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 { } } But i want to execute it only once when user login into the application please guide me how can i achieve it.
Thanks.