欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JAVA记住密码功能的实现代码

程序员文章站 2022-08-06 21:25:39
准备:ssm框架,mysql数据库 用户表 user 实体类: public class user { /** * 主键id */ p...

准备:ssm框架,mysql数据库

用户表 user

JAVA记住密码功能的实现代码

实体类:

public class user {
  /**
   * 主键id
   */
  private integer userid;
  /**
   * 账号
   */
  private string username;
  /**
   * 密码
   */
  private string password;
  public integer getuserid() {
    return userid;
  }
  public void setuserid(integer userid) {
    this.userid = userid;
  }
  public string getusername() {
    return username;
  }
  public void setusername(string username) {
    this.username = username;
  }
  public string getpassword() {
    return password;
  }
  public void setpassword(string password) {
    this.password = password;
  }

}

usermapper

<?xml version="1.0" encoding="utf-8" ?>
<!doctype mapper
    public "-//mybatis.org//dtd mapper 3.0//en"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- 命名空间 -->
<mapper namespace="com.luowx.mapper.usermapper">

  <resultmap id="usermap" type="user">
    <id property="userid" column="user_id"/>
    <result property="username" column="username"/>
    <result property="password" column="password"/>
  </resultmap>

  <select id="getuserbyname" resultmap="usermap">
    select * from s_user where username=#{username}
  </select>
</mapper>

mapper层

public interface usermapper {

 user getuserbyname(string username);
}

service层

public interface userservice {
  user getuserbyname(string username, string password, httpsession session, httpservletrequest httpservletrequest, httpservletresponse httpservletresponse);
}

impl

@service
public class userserviceimpl implements userservice {
  @autowired
  private usermapper usermapper;
  
  @override
  public resultvo getuserbyname(string username, string password, httpsession session, httpservletrequest httpservletrequest, httpservletresponse httpservletresponse) {
    string remember = httpservletrequest.getparameter("remember");
    if (username!= null && username!= 0){
      user user = usermapper.getuserbyname(username);

      if (user != null && user.getpassword().equals(password)){
        session.setattribute("user", user);
        if (remember != null){
          
          cookie cookieuser = new cookie("username", username);
          cookie cookiepass = new cookie("password", password);
          cookieuser.setmaxage(60 * 60 * 24);
          cookiepass.setmaxage(60 * 60 * 24);
          httpservletresponse.addcookie(cookieuser);
          httpservletresponse.addcookie(cookiepass);
          return resultvo.success(user);
       }
        return resultvo.error(1, "用户名或密码错误");
      }
    return resultvo.error(3, "用户名或密码不能为空");
  }
}

controller

@restcontroller
public class usercontroller {

  @autowired
  private userservice userservice;
  //登录
  @requestmapping("/getuserbyname")
  public resultvo getuserbyname(string username, string password, httpsession session, httpservletrequest httpservletrequest, httpservletresponse httpservletresponse){
    return userservice.getuserbyname(username, password, session, httpservletrequest, httpservletresponse);
  }
}

前端代码(jsp)

html的自己修改下,样式是bootstrap的

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contenttype="text/html;charset=utf-8" language="java" %>
<html>
<head>
  <title>登录</title>
  <link rel="stylesheet" href="/bootstrap4/css/bootstrap.min.css" rel="external nofollow" >
  <style>
    body{
      background-color: #fafafa;
    }
    .nice{
      position: relative;
      display: flex;
      align-items: center;
      justify-content: center;
    }
    .container{
      margin-top: 100px;
      display: flex;
      justify-content: space-between;
      border: #b3b7bb 2px solid;
      border-radius: 5px;
    }
    #loginform{
      width: 300px;
      background-color: rgba(255,255,255,0.7);
      margin-top: 30px;
      border: #b3b7bb 1px solid;
      border-radius: 5px;
    }
  </style>
</head>
<body>
<div class="container">
  <div><img src="img/login_bg_pic.jpg"></div>
  <form action="getuserbyname" method="post" id="loginform">
    <div class="form-group">
      <label for="exampleinputemail1">用户名</label>
      <input type="text" class="form-control" id="exampleinputemail1" name="userid" autocomplete="off" value="${userid}">
    </div>
    <div class="form-group">
      <label for="exampleinputpassword1">密码</label>
      <input type="password" class="form-control" id="exampleinputpassword1" name="password" value="${password}">
    </div>
    <%--验证码--%>
    <div class="form-group">
      <div class="input-icon" style="display: flex;justify-content: space-around;align-items: center">
        <i class="fa fa-picture-o"></i>
        <input class="form-control" style="width:180px;" type="text" id="verifycode" name="verifycode" placeholder="验证码" maxlength="4" autocomplete="off">
        <img src="${pagecontext.request.contextpath }/getverifycode" width="110" height="34" id="verifycodeimage">
      </div>
    </div>
    <div class="custom-control custom-checkbox mb-3 was-validated">
      <input type="checkbox" class="custom-control-input" id="rem" name="remember" checked>
      <label class="custom-control-label" for="rem">记住密码</label>
    </div>
    <button type="button" class="btn btn-primary login">登 录</button>
    <a href="/forgotpass" rel="external nofollow" ><button type="button" class="btn btn-danger">忘记密码</button></a>
    <br><br>
    <div class="nice">
      欢迎来到:<br>教务综合信息服务平台
    </div>
  </form>
  <script src="/js/jquery-3.4.1.min.js"></script>
  <script src="/bootstrap4/js/bootstrap.min.js"></script>
  <script>
    $(function () {
      $(".login").click(function () {
        //发送ajax请求
        $.ajax({
          url:'getuserbyname',
          type:'post',
          data:$("#loginform").serialize(),
          success:function (res) {
            console.log(res);
            if (res.status === 0){
              if(res.data.role === 0) {
                location.href = "overview";
              }else if (res.data.role === 1){
                location.href = "teacher";
              }else if (res.data.role === 2){
                location.href = "teacher";
              }else if (res.data.role === 3){
                location.href = "student";
              }
            } else {
              $(".nice").html("<div>" + res.message + "</div>");
            }
          }
        });
      });

    });

  </script>

</div>
</body>
</html>

总结

以上所述是小编给大家介绍的java记住密码功能的实现代码,希望对大家有所帮助