基于Cookie使用过滤器实现客户每次访问只登录一(2)

@Controller @RequestMapping("/common") public class CommonController{ /** * 自动登录方法 * @param request * @param response * @param username * @param pwd * @param ProviderInfo 供应商账户信息model * @return */ @RequestMapping("/automaticLogin") public String automaticLogin(HttpServletRequest request,ServletResponse response,@CookieValue(value = "username", required = false) String username,@CookieValue(value = "pwd", required = false) String pwd,ProviderInfo ProviderInfo) { // 保存需求登录前的链接 String goURL = (String) session.getAttribute("goURL"); if (username == null) {//cookies中没有用户名,肯定不需要自动登录 session.setAttribute("IsAutomaticLogin", "0"); return "redirect:" + goURL; } else { try { username = URLDecoder.decode(username, "UTF-8");//转义,防止中文 } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } // cookie失效 session一定为空,因为登录时,一定会把用户名保存在cookie中 if ("".equals(username) || username == null) {// 使用session登录不了,不进行任何操作,不在进入这个方法 session.setAttribute("IsAutomaticLogin", "0"); return "redirect:" + goURL; } else { // cookie中没有密码,判断session为不为空,如果为空,说明没有登录,如果不为空,说明,用户是选择不记住密码登录(所以cookie中没有密码) if ("".equals(pwd) || pwd == null) { ProviderInfo customer1 = (ProviderInfo) session.getAttribute("providerInfo_gys"); if (customer1 == null) {// 使用session登录不了,不进行任何操作,不在进入这个方法 session.setAttribute("IsAutomaticLogin", "0"); return "redirect:" + goURL; } else { // 已经登录,不再进入这个方法 return "redirect:" + goURL; } } else { // cookie中有密码,判断session为不为空,如果为空,说明没有登录,如果不为空,说明已经登录 ProviderInfo customer1 = (ProviderInfo) session.getAttribute("providerInfo_gys"); if (customer1 == null) {// 当前没有登录,调用cookies中的用户名跟密码进行登录 // 进行自动登录操作,登录成功后返回原来页面 ProviderInfo customer3 = ValidateDate(username); customer3.setPwd(pwd); customer3.setAccountType(6); ProviderInfo customer2 = infoService.login(customer3);//调用登录方法 if (customer2 == null) {// 自动登录失败,不再进入这个方法 session.setAttribute("IsAutomaticLogin", "0"); return "redirect:" + goURL; } else { // 登陆成功保存客户信息到session session.setAttribute("providerInfo_gys",customer2); return "redirect:" + goURL; } } else { return "redirect:" + goURL; } } } } /** * 用户登陆 * @param request * @param response * @param cus * @return */ @RequestMapping("/UserLogin") @ResponseBody public Map<String, Object> goLogin(HttpServletRequest request,HttpServletResponse response,@ModelAttribute("ProviderInfo") ProviderInfo cus) { /*省略一些逻辑判断*/ cus.setPwd(MD5Util.MD5(Pwd)); ProviderInfo providerInfo = infoService.login(cus); Map<String, Cookie> cookieMap = new HashMap<String, Cookie>(); if (providerInfo == null) { // 登陆失败,重新跳转到登陆页面 map.put("error", "密码错误"); return map; }else{ String newUserName = null; if (remember_me.equals("1")) {// 有选择一个月免登录 try { newUserName = URLEncoder.encode(username, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } Cookie nameCookie = new Cookie("username", newUserName); String pwdMd5Cook = MD5Util.MD5(Pwd); Cookie pwdCookie = new Cookie("pwd", pwdMd5Cook);// 保存加密后的密码+"create" nameCookie.setMaxAge(60 * 60 * 24 * 365);// 用户名保存一年 pwdCookie.setMaxAge(60 * 60 * 24 * 30);// 密码保存30天 // 发送Cookie信息到浏览器 response.addCookie(nameCookie); response.addCookie(pwdCookie); session.setAttribute("IsAutomaticLogin",null); }else{//没有选择,删除上次可能已经选择自动登录时的密码 Cookie[] cookies = request.getCookies(); if (null != cookies) { for (Cookie cookie : cookies) { cookieMap.put(cookie.getName(), cookie); } } if (cookies != null) { for (int i = 0; i < cookies.length; i++) { if (cookieMap.containsKey("pwd")) { Cookie cookie = new Cookie("pwd", null); cookie.setMaxAge(0);// 删除密码cookie response.addCookie(cookie); } } } } // 登陆成功,保存当前user信息,保存客户信息到session map.put("ProviderInfo", providerInfo); map.put("goURL", session.getAttribute("goURL")); session.setAttribute("providerInfo_gys", providerInfo); return map; }else { map.put("error", "该供应商账号不存在"); return map; } } /** * 注销 * @return */ @RequestMapping("/logout") public String logout(HttpServletResponse response) { Map<String, Cookie> cookieMap = new HashMap<String, Cookie>(); Cookie[] cookies = request.getCookies(); if (null != cookies) { for (Cookie cookie : cookies) { cookieMap.put(cookie.getName(), cookie); } } if (cookies != null) { for (int i = 0; i < cookies.length; i++) { if (cookieMap.containsKey("pwd")) { Cookie cookie = new Cookie("pwd", null); cookie.setMaxAge(0);// 删除密码cookie response.addCookie(cookie); } } } session.setAttribute("providerInfo_gys", null); return "/index"; } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wdpjwp.html