因工作需要2个登录页面,配置2个登录页面后发现session超时后不能跳转到特定的登录页,只能跳转到一个登录页,不能区分跳转。
解决方式:因为session超时,不能获取有用信息,考虑使用cookie来保存登录原始页面,用于超时判断跳转到那个登录页。考虑到因为都跳转到默认登录页,就在登录页面的方法中进行判断,来区分跳转。登录时保存登录标识到cookie中。代码入下。其中login_cookie用于区分是从哪个登录页面进入系统的。
第一个访问登录页面的方法
@RequestMapping(value = "/login", method = RequestMethod.GET) public String loginPage(HttpServletRequest request) { Cookie[] cs = request.getCookies(); if(cs!=null && cs.length>0){ for(Cookie cookie:cs){ if("login_cookie".equals(cookie.getName())){ if("zhongke".equals(cookie.getValue())){ return "login/otherLogin"; } } } } return "login/login"; }
第一个登录入口
@RequestMapping(value = "/doLogin", method = RequestMethod.POST) public String login(String dlyhid, String yhmm, String code, ModelMap modelMap,HttpServletRequest request,HttpServletResponse response) throws Exception { Cookie[] cookies = request.getCookies(); if(cookies==null){ Cookie cookie = new Cookie("login_cookie", "rjxx"); cookie.setPath("/"); response.addCookie(cookie); }else{ boolean flag = true; for(Cookie cookie:cookies){ if("login_cookie".equals(cookie.getName())){ cookie.setValue("rjxx"); cookie.setPath("/"); response.addCookie(cookie); flag = false; break; } } if(flag){ Cookie cookie = new Cookie("login_cookie", "rjxx"); cookie.setPath("/"); response.addCookie(cookie); } } }
第二个登录入口的方法
@RequestMapping(value = "/doLogin", method = RequestMethod.POST) public String login(String dlyhid, String yhmm, String code, ModelMap modelMap,HttpServletRequest request,HttpServletResponse response) throws Exception { Cookie[] cookies = request.getCookies(); if(cookies==null){ Cookie cookie = new Cookie("login_cookie", "zhongke"); cookie.setPath("/"); response.addCookie(cookie); }else{ boolean flag = true; for(Cookie cookie:cookies){ if("login_cookie".equals(cookie.getName())){ cookie.setValue("zhongke"); cookie.setPath("/"); response.addCookie(cookie); flag = false; break; } } if(flag){ Cookie cookie = new Cookie("login_cookie", "zhongke"); cookie.setPath("/"); response.addCookie(cookie); } } }