Home  >  Forum  >  Spring
Post New Query

Sessions are not persisting in the database for each user in same browser with different tabs using Spring Sessions with MySQL



Asked on January 24, 2017

I'm currently using db (mysql) to store my sessions with spring.It is generating new session id for each user .but it is replacing session id's of previously who are logged. Please give some suggestions to overcome this problem. The following is the service which is i am calling from controller for each request

public class MultiLoginServiceImpl
{
        public static String createMultiLogin(HttpServletRequest httpRequest)
        {

                String alias = httpRequest.getParameter("_s");
                System.err.println("alias"+alias);
                @SuppressWarnings("unchecked")

            SessionRepository<Session> sessionRepository =(SessionRepository<Session>) httpRequest.getAttribute(SessionRepository.class.getName());

                Map<String, String> sessionIds = sessionManager.getSessionIds(httpRequest);
                System.out.println(sessionIds);
                for(Map.Entry<String, String> entry : sessionManager.getSessionIds(httpRequest).entrySet()) {

                    String aliasId = entry.getKey();
                    String sessionId = entry.getValue();
                    HttpSession httpSession = httpRequest.getSession(false);
                    Session storedSession = sessionRepository.getSession(sessionId);
                    if(storedSession.getAttribute("userEmailId")!= null)
                    {
                        httpSession.setAttribute("userEmailId", storedSession.getAttribute("userEmailId"));
                    }
                    System.out.println("mapids"+aliasId +" : "+sessionId);
                }

                String addAlias = alias== null ? newSessionAlias1: requestedSessionAlias1; 

                /*if(alias == null || "".equals(alias.trim()))
                {
                        alias = sessionManager.getNewSessionAlias(httpRequest);
                }*/
                httpRequest.setAttribute("alias",addAlias);
                //SecurityContext context = (SecurityContext) httpSession.getAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY);
                //System.out.println(context.toString());
                String addAccountUrl = sessionManager.encodeURL(httpRequest.getContextPath(), addAlias); 
                System.out.println("addalias"+addAlias);
                return addAccountUrl ;

        }
}


Write Answer






©2019 concretepage.com | Privacy Policy | Contact Us