java自动登录功能和记住用户名功能 您所在的位置:网站首页 自动登录功能 java自动登录功能和记住用户名功能

java自动登录功能和记住用户名功能

2024-01-09 00:27| 来源: 网络整理| 查看: 265

实现在这两个功能,都需要用到coolie技术,将所需要的用户名或者密码带到浏览器端,实现用户名和密码的记录,然后完成所要的功能。 一、自动登录功能

后台的代码 1.1、后台servlet中的代码 //1、判断是否勾选了自动登录的复选框 String autoLogin = request.getParameter("autoLogin"); //自动登录 if("autoLogin".equals(autoLogin)){ //勾选了复选框 回写一个带有用户名和密码的cooke Cookie cookie = new Cookie("autoLogin", user.getUsername() + "#" + user.getPassword()); //设置存活的时间 cookie.setMaxAge(60*60*12*7);//设置7天 //设置路径 cookie.setPath("/"); //回写cookie response.addCookie(cookie); response.sendRedirect(request.getContextPath()+"/index.jsp"); return; }

1.2、 后台filter中的代码

public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException { // 将reqeust, 先强转 HttpServletRequest request = (HttpServletRequest) req; // 判断用户是否已经登录, 如果已经登录, 直接放行 User existUser = (User) request.getSession().getAttribute("user"); if(existUser!=null){ //进来, 则说明 已经登录,则 直接放行 chain.doFilter(request,resp); return; }else{ //说明没有登录, 那么就 帮助 用户自动登录 //获得目标的 coookie Cookie[] cookies = request.getCookies(); Cookie targetCookie = CookieUtils.findTargetCookie(cookies, "autoLogin"); if(targetCookie==null){ //进来,则说明根本都没有带 目标 cookie 过来, 那么 不能 自动登录, 则 // 直接放行 chain.doFilter(request,resp); return; }else{ // 进来,则说明找到了 目标名称的cookie , 进一步获得 // username 和password // autologin = zs#itheima#202cb962ac59075b964b07152d234b70 String value = targetCookie.getValue(); String username = value.split("#")[0]; String password = value.split("#")[1]; System.out.println("username : " + username); System.out.println("password : " + password); // 还是要再次 查询一下. UserService us = new UserServiceImpl(); User loginUser = us.findUserPwd(username, password); if(loginUser!=null){ //则说明, 找到了, 意味着 带过来的cookie 信息是没有问题的 // 帮助 用户自动登录 //注:登陆成功的标志是 session 域中有 user 对象 此处登录成功之后,将user对象存入到 session域中 request.getSession().setAttribute("user",loginUser); //放行 chain.doFilter(request,resp); return; }else{ //说明 cookie的信息有问题... 登录不了 //放行 chain.doFilter(request,resp); return; } } } } 3、后台工具类 util 中的代码 public class CookieUtils { /** * 调用这个 api ,可以从 cookies的数组中找到 目标名称的cookie 对象 * * @param cookies . cookie的数组 * @param name , 目标cookie的 name, 名称 * @return */ public static Cookie findTargetCookie(Cookie[] cookies , String name){ if(cookies==null){ return null; } for (Cookie cookie: cookies ) { if(name.equals(cookie.getName())){ //进来,则说明找到了 目标名称的cookie对象 return cookie; } } return null; } }

二、记住用户名功能 1.1. 后台的代码

//获取前台传过来的记住用户名的input中的name的属性 String remeber = request.getParameter("remeber"); //回写用户名 if("remeber".equals(remeber)){//当勾选的记住按钮的时候 System.out.println(remeber+"我是remeber"); Cookie cookie = new Cookie("remeber", user.getUsername()); //设置存货时间 cookie.setMaxAge(60*60*24*4);//存货4天的时间 //设置访问的路径 cookie.setPath("/"); //回写cooki response.addCookie(cookie); }else {//当没有记住勾选了按钮的时候,清除cookie的信息 Cookie cookie = new Cookie("remeber", ""); cookie.setMaxAge(0); cookie.setPath("/"); response.addCookie(cookie); }

1.2 前台的代码

1.2.1 、JSP 的代码 0){ //遍历Cookie for(int i=0;i 1.2.2 、html 的代码 记住用户名


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有