重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

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

准备:SSM框架,MySQL数据库

创新互联建站长期为1000+客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为望花企业提供专业的成都网站设计、网站建设、外贸网站建设望花网站改版等技术服务。拥有10多年丰富建站经验和众多成功案例,为您定制开发。

用户表 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" ?>




  
    
    
    
  

  

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" %>


  登录
  
  


<%--验证码--%>


欢迎来到:
教务综合信息服务平台

总结

以上所述是小编给大家介绍的JAVA记住密码功能的实现代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对创新互联网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!


分享标题:JAVA记住密码功能的实现代码
文章地址:http://cqcxhl.cn/article/igeohe.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP