对于使用Spring Sessions with MySQL的不同选项卡的同一浏览器中的每个用户,会话不会持久存储在数据库中

时间:2017-01-24 14:50:44

标签: mysql spring spring-security spring-session

我目前正在使用db(mysql)来存储我的春季会话。它正在为每个用户生成新的会话ID,但它正在替换以前记录的会话ID。请提出一些建议来克服这个问题。 以下是我从控制器为每个请求调用的服务

image1 image2

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 ;

        }
}

0 个答案:

没有答案