近期接触到用户七天之内免登录的功能,参考网上查阅的资料,本人思路是将登录信息存放到Cookie中,登录表单中需填充的数据从Cookie中获取,从而实现此功能。下面是本人搭建的Demo源码:
login.jsp页面主要代码(校验代码就在此省略...):
<% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; String password=""; String name=""; String checked=""; Cookie[] cookies = request.getCookies(); //取出cookie对象组 for(int i = 0; cookies != null && i < cookies.length;i++){ Cookie cookie = cookies[i]; // 取出其中的一个对象,含有name ,value if(cookie != null && "name".equals(cookie.getName())){ //获取第一个cookie对象的name name = URLDecoder.decode(cookie.getValue(), "UTF-8");//进行解码 checked = "checked"; } if(cookie != null && "password".equals(cookie.getName())){ password = cookie.getValue(); } } %>
。。。。。。
<form class="form-signin" role="form" id="loginForm"> <h2 class="form-signin-heading"> <i class="glyphicon glyphicon-leaf"></i> 管理员登录 </h2> <div class="form-group has-success has-feedback"> <input type="text" class="form-control" id="floginacct" name =name value="<%=name%>" placeholder="请输入登录账号" autofocus> <span class="glyphicon glyphicon-ok form-control-feedback"></span> </div> <div class="form-group has-success has-feedback"> <input type="password" class="form-control" id="fuserpswd" name="password" value="<%=password%>" placeholder="请输入登录密码" style="margin-top: 10px;"> <span class="glyphicon glyphicon-lock form-control-feedback"></span> </div> <div class="checkbox"> <label> <input type="checkbox" id="status" name="remember" value="yes" <%=checked%>> 记住我 </label> </div> <a class="btn btn-lg btn-success btn-block" id="loginBtn"><i class="glyphicon glyphicon-log-in"></i> 登录</a> </form>
。。。。。。
/* 使用ajax实现数据的操作 */ $.ajax({ type : "POST", url : "${APP_PATH}/admin/login.do", data :$("#loginForm").serialize(), beforeSend : function() { loadingIndex = layer.load(2, {time : 10 * 1000}); }, success : function(result) { layer.close(loadingIndex); if (result.success) { window.location.href = "${APP_PATH }/index.jsp"; } else { layer.msg("用户信息不正确,请重新输入", {time:1500, icon:5, shift:6}, function(){ floginacct.focus(); }); } }//success 结束 });//ajax 结束
UserController后端主要代码:
@ResponseBody @RequestMapping("/login") public Object login(HttpServletRequest request,HttpServletResponse response) { start(); try { String name = request.getParameter("name"); //获取用户名 String password = request.getParameter("password"); //获取密码 String remember = request.getParameter("remember"); //获取是否打钩 String codeName = URLEncoder.encode(name, "UTF-8"); //对输入的中文进行编码,防止乱码出现 HashMap<String, String> map = new HashMap<>(); map.put("name",codeName); map.put("pwd",password); Admin admin =adminService.login(map); if(!"".equals(admin)&&admin!=null){ Cookie nameCookie = new Cookie("name", codeName); Cookie passwordCookie = new Cookie("password", password); nameCookie.setPath(request.getContextPath()+"/"); //设置Cookie的有效路径 passwordCookie.setPath(request.getContextPath()+"/");//设置Cookie的有效路径 if(remember != null && "yes".equals(remember)){ //有记住我,就设置cookie的保存时间 nameCookie.setMaxAge(7*24*60*60);//设置日期为一星期 passwordCookie.setMaxAge(7*24*60*60); }else{ //没有记住我,设置cookie的时间为0 nameCookie.setMaxAge(0); passwordCookie.setMaxAge(0); } response.addCookie(nameCookie); response.addCookie(passwordCookie); request.getSession().setAttribute("admin",admin.getName()); success(true); }else{ success(false); } } catch (Exception e) { e.printStackTrace(); success(false); } return end(); } }
index.jsp页面主要代码(获取用户信息,显示***登录),本人在这会有点疑问,如果没有勾选“记住我”,怎样获取到用户信息?因此,本人想借用session用于存储并页面获取:
<% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; String name=""; Cookie[] cookies=request.getCookies(); if(cookies==null){ response.sendRedirect(basePath+"/login.jsp"); }else {
name = (String) request.getSession().getAttribute("admin");
for (int i = 0; i < cookies.length - 1; i++) { Cookie cookie = cookies[i]; if (cookie != null && "name".equals(cookie.getName())) { //获取第一个cookie对象的name name = URLDecoder.decode(cookie.getValue(), "UTF-8");//进行解码 } } } %>
。。。。。
欢迎管理员:<%=name%>
本人只是“编制工”,希望对大家有用,同时,有什么问题,望指教~