(project)登陆模块的实现

4 篇文章 0 订阅
2 篇文章 0 订阅

登陆在项目开发中是在普通不过的一个功能了,然而在web端下该功能的实现也并不单单是将用户的账号和密码拿到后台的数据库中进行查询那么的简单。一般的登陆还包括了验证码的验证,保存用户登陆信息(Cookie技术),如果用户的信息验证通过,还要将用户的数据存放到session,返回到前端页面显示。
下面,就登陆模块用代码实现该功能。
1,获取用户信息,查询数据库中是否有这个用户的存在
1.1,将用户输入的密码进行md5加密,因为后台保存的也是加密的字符串
1.2,调用业务层的方法查询数据,如果返回的对象是空,则重定向到登陆界面。

     //获取页面用户输入的用户名和密码,并对密码进行加密处理
        String userName = request.getParameter("userName");
        String pwd = request.getParameter("password");
        String md5Pwd = MD5Encrypt.MD5Encode(pwd);
        //调用业务层的方法查找数据库是否存在一条数据
        User user = userService.findUserByNameAndPwd(userName,md5Pwd);
        if(user==null) {
            this.addFieldError("errorInfo", "用户名或密码输入错误,请再输入一遍!");
            return "login";
        }

    public User findUserByNameAndPwd(String userName, String md5Pwd) {
        //校验传入的数据是否为空值
        if(StringUtils.isNotBlank(userName) && StringUtils.isNotBlank(md5Pwd)){
            //组织好查询条件
            String condition = " and o.userName = ? and o.password = ?";
            Object[]params = {userName,md5Pwd};
            //调用dao的查询方法
            List<User> userList = userDao.findCollectionByConditionNoPage(condition, params, null);
            if(userList!=null && userList.size()==1 ){
                return userList.get(0);
            }

        }
        return null;
    }

2,记住我的功能实现

    //处理用是否记住密码,增加cookie功能
        //String rememberMe = request.getParameter("rememberMe");
        //System.out.println("是否记住我:"+rememberMe);
        try {
            userName = URLEncoder.encode(userName, "UTF-8");
            LoginUtil.addCookie(userName,pwd,request,response);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        public static void addCookie(String userName, String password,
            HttpServletRequest request, HttpServletResponse response) {
        if(StringUtils.isNotBlank(userName) && StringUtils.isNotBlank(password)){
            //创建Cookie对象,并创建Cookie的父路径
            Cookie userNameCookie = new Cookie("userName", userName);
            Cookie pwdCookie = new Cookie("password", password);
            //这行代码是程序员经常容易犯的错误,没有写下面的两行代码
            userNameCookie.setPath(request.getContextPath()+"/");
            pwdCookie.setPath(request.getContextPath()+"/");
            //根据numberMe的值,来判断cookie的生命周期
            String rememberMe = request.getParameter("rememberMe");
            if(rememberMe!=null && rememberMe.equals("yes")){
                userNameCookie.setMaxAge(7*24*60*60);
                pwdCookie.setMaxAge(7*24*60*60);
            }else{
                userNameCookie.setMaxAge(0);
                pwdCookie.setMaxAge(0);
            }
            response.addCookie(userNameCookie);
            response.addCookie(pwdCookie);
        }
    }

3,如果登陆成功之后,就将对象保存到session中

//登录成功,并将对象添加到session中
    SessionUtil.addUserToSession(request,user);

    public static void addUserToSession(HttpServletRequest request, User user) {
        HttpSession session = request.getSession();
        System.out.println("把user添加到session中");
        session.setAttribute("globle_user", user);

    }

4,有时候需要对保存的数据进行转换,比如保存性别不是以男或者女来保存数据,而是以1,2来表示,但是1,2实际上表示的是哪一个意思,就要到数据库中查询字典表。

//查询数据字段,对相应的数据进行转换
  SystemDDL genderDDL = systemDDLService.findSystemDDLbyCondition("gender", user.getGender());
  user.setGenderVO(genderDDL.getDdlName());
  SystemDDL statusDDL = systemDDLService.findSystemDDLbyCondition("status", user.getStatus());
  user.setStatusVO(statusDDL.getDdlName());

5,前端页面的展示,
如果使用cookie来实现记住我的功能,就要将用户本地的cookie取出来,来判断cookie是否有存取用户的账号和密码

<% 
    String userName = "";
    String password = "";
    String checked = "";
    Cookie[]cookies = request.getCookies();
    //判断cookie是否为null,且长度是否小于1
    if(cookies!=null && cookies.length>0){
        for(int i=0;i<cookies.length;i++){
            Cookie cookie = cookies[i];
            if("userName".equals(cookie.getName())){
                userName = URLDecoder.decode(cookie.getValue(), "UTF-8");
                checked = "checked";
            }
            if("password".equals(cookie.getName())){
                password = cookie.getValue();
            }
        }
    }else{
        checked = "";
    }
%>

上面的五个步骤基本上是一个登陆功能要实现的,但是还有一个没有去实现那就是验证码,不过后面有时间就会来完善这篇博客。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值