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

网友投稿 264 2022-12-16


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

准备:SSM框架,mysql数据库

用户表 user

实体类:

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

PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

select * from s_user where username=#{username}

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

<%--验证码--%>



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

总结


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:java8新特性之接口的static和default的使用
下一篇:Spring Boot自定义Banner实现代码
相关文章

 发表评论

暂时没有评论,来抢沙发吧~