1.登录页面如图

2.业务准备

2.1.用户实体类

public class User {/*** 用户ID*/private Integer id;/*** 用户账号*/private String userCode;/*** 用户名*/private String userName;/*** 用户密码*/private String password;/*** 用户类型*/private String typeCode;/*** 用户所属组(科室、公司等)*/private String groupCode;/*** 用户地址*/private String address;/*** 手机号*/private String mobile;/*** 电话号码*/private String phoneNum;/*** 邮箱*/private String eMail;/*** 用户角色*/private Integer roleId;/*** 锁定状态1:未锁定2:锁定*/private Integer userLock;/*** 锁定时间*/private Date gmtUserLock;/*** 用户更新日期*/private Date gmtModified;/*** 创建时间*/private Date gmtCreated;/*** 删除状态*/private Integer isDel;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getUserCode() {return userCode;}public void setUserCode(String userCode) {this.userCode = userCode;}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;}public String getTypeCode() {return typeCode;}public void setTypeCode(String typeCode) {this.typeCode = typeCode;}public String getGroupCode() {return groupCode;}public void setGroupCode(String groupCode) {this.groupCode = groupCode;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}public String getMobile() {return mobile;}public void setMobile(String mobile) {this.mobile = mobile;}public String getPhoneNum() {return phoneNum;}public void setPhoneNum(String phoneNum) {this.phoneNum = phoneNum;}public String getEMail() {return eMail;}public void setEMail(String eMail) {this.eMail = eMail;}public Integer getRoleId() {return roleId;}public void setRoleId(Integer roleId) {this.roleId = roleId;}public Integer getUserLock() {return userLock;}public void setUserLock(Integer userLock) {this.userLock = userLock;}public Date getGmtUserLock() {return gmtUserLock;}public void setGmtUserLock(Date gmtUserLock) {this.gmtUserLock = gmtUserLock;}public Date getGmtModified() {return gmtModified;}public void setGmtModified(Date gmtModified) {this.gmtModified = gmtModified;}public Date getGmtCreated() {return gmtCreated;}public void setGmtCreated(Date gmtCreated) {this.gmtCreated = gmtCreated;}public Integer getIsDel() {return isDel;}public void setIsDel(Integer isDel) {this.isDel = isDel;}
}

2.2.创建用户表

此处只展示用户表 若需要全部角色菜单等表私信或评论

CREATE TABLE `user`  (`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户ID',`user_code` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL COMMENT '用户账号',`user_name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL COMMENT '用户名',`password` varchar(35) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL COMMENT '用户密码',`type_code` varchar(12) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL COMMENT '用户类型',`group_code` varchar(12) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL COMMENT '用户所属组(科室、公司等)',`address` text CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL COMMENT '用户地址',`mobile` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL COMMENT '手机号',`phone_num` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL COMMENT '电话号码',`e_mail` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL COMMENT '邮箱',`role_id` int(11) NULL DEFAULT NULL COMMENT '用户角色',`user_lock` int(1) NOT NULL COMMENT '锁定状态1:未锁定2:锁定',`gmt_user_lock` datetime(0) NULL DEFAULT NULL COMMENT '锁定时间',`gmt_modified` datetime(0) NOT NULL COMMENT '用户更新日期',`gmt_created` datetime(0) NOT NULL COMMENT '创建时间',`is_del` int(1) NOT NULL DEFAULT 1 COMMENT '删除状态  1正常  2删除',PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 15 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_bin COMMENT = '系统用户表' ROW_FORMAT = Dynamic;

3.登录业务实现

3.1.Controller

@RequestMapping("dologin")@ResponseBodypublic Response dologin(UserDto user, HttpServletRequest request) {Integer flag = -1;JSONObject json = new JSONObject();do {if (StringUtils.isEmpty(user.getUserCode()) || StringUtils.isEmpty(user.getPassword())) {// 用户信息不完整;break;}// 判断用户是否存在User userResult=new User();userResult.setUserCode(user.getUserCode());User usertemp = userService.findOne(userResult, null);try {if (usertemp == null) {// 账号不存在的场合flag = 0;json.put("flag", flag);break;}user.setPassword(MD5Utils.md5Encode(user.getPassword()));if (!user.getPassword().equals(usertemp.getPassword())) {// 密码错误的场合flag = 1;json.put("flag", flag);json.put("account", usertemp.getUserCode());} else {if (usertemp.getIsDel() == 3) {flag = 4;json.put("flag", flag);} else {JSONObject jsonList = new JSONObject();if(usertemp.getRoleId()!=null){//单一角色jsonList = menuService.queryMenuByRole(Integer.valueOf(usertemp.getRoleId()));}if (jsonList != null && jsonList.size() != 0 &&jsonList.getJSONArray("@menu@000").size() > 0) {// 成功登录HttpSession session = request.getSession();// 存储用户到sessionsession.setAttribute(Constant.SESSION_USER, usertemp);session.setAttribute("menu", jsonList);flag = 3;json.put("flag", flag);} else {//无一级菜单权限flag = 2;json.put("flag", flag);}}}} catch (Exception e) {e.printStackTrace();}} while (false);json.put("flag", flag);return Response.success(json);}

3.2.Service+ServiceImpl+Dao

/***@Description 通过角色查询菜单表数据*@param*@return*@author wang hq*/JSONObject queryMenuByRole(Integer roleId);
@Overridepublic JSONObject queryMenuByRole(Integer roleId) {return setMenuJson(menuDao.queryMenuByRole(roleId));}
/***@Description 通过角色查询菜单*@param*@return*@author wang hq*/List<MenuDto> queryMenuByRole(@Param("roleId")  Integer roleId);

3.3.Mapper

<select id="queryMenuByRole" resultMap="menuDtoMap" parameterType="java.lang.Integer">SELECT DISTINCTm.id id,m.menu_parent_code menu_parent_code,m.menu_name menu_name,m.menu_code menu_code,m.url url,m.img img,m.sort sort,m.button_type button_typeFROMmenu mINNER JOIN role_relate_menu rrm ON rrm.menu_id = m.idAND rrm.is_del = 1WHEREm.is_del = 1AND rrm.role_id = #{roleId}ORDER BY menu_parent_code,sort</select>

3.4.点击登录按钮结果展示

3.4.1.flag为3即为登录成功

3.4.2.页面也已登录成功

4.修改密码

4.1.Controller

 // 修改密码@RequestMapping("updatePassword")@ResponseBodypublic Response updatePassword(HttpServletRequest request, String oldPassword,String newPassword) {Integer flag = -1;JSONObject json = new JSONObject();User user = (User) request.getSession().getAttribute(Constant.SESSION_USER);if (StringUtils.isNotEmpty(oldPassword)) {try {String psw = MD5Utils.md5Encode(oldPassword);if (psw.equals(user.getPassword())) {newPassword = MD5Utils.md5Encode(newPassword);User user1 = new User();user1.setId(user.getId());user1.setPassword(newPassword);flag = userService.update(user1);HttpSession session = request.getSession();user.setPassword(newPassword);session.setAttribute(Constant.SESSION_USER, user);if (flag > 0) {flag = 2;// 修改成功}} else {flag = 0;// 旧密码错误}} catch (Exception e) {e.printStackTrace();}}json.put("flag", flag);return Response.success(json);}

4.2.Service+ServiceImpl+Dao

Integer update(User user);
 @Overridepublic Integer update(User user) {return userDao.update(user);}
Integer update(User user);

4.3.Mapper

<update id="update">update user<trim prefix="set" suffixOverrides=","><if test="id != null">id = #{id},</if><if test="userCode != null and ''!= userCode">user_code = #{userCode},</if><if test="userName != null and ''!= userName">user_name = #{userName},</if><if test="password != null and ''!= password">password = #{password},</if><if test="typeCode != null and ''!= typeCode">type_code = #{typeCode},</if><if test="groupCode != null and ''!= groupCode">group_code = #{groupCode},</if><if test="address != null and ''!= address">address = #{address},</if><if test="mobile != null and ''!= mobile">mobile = #{mobile},</if><if test="phoneNum != null and ''!= phoneNum">phone_num = #{phoneNum},</if><if test="eMail != null and ''!= eMail">e_mail = #{eMail},</if><if test="roleId != null">role_id = #{roleId},</if><if test="userLock != null">user_lock = #{userLock},</if><if test="gmtUserLock != null">gmt_user_lock = #{gmtUserLock},</if>gmt_modified=now(),<if test="gmtCreated != null">gmt_created = #{gmtCreated},</if><if test="isDel != null">is_del = #{isDel},</if></trim>where id = #{id}</update>

4.4.测试

4.4.1.填写新密码与输入正确旧密码

4.4.2.点击确定确认修改密码

此时返回到登录页面重新输入新密码正确登录即可

5.退出登录

// 退出登录@RequestMapping("loginOut")public String loginOut(HttpServletRequest request) {HttpSession session = request.getSession();session.removeAttribute(Constant.SESSION_USER);return "redirect:/login";}

至此 相应登录修改密码功能及代码讲解完毕 由于本文主要写后台业务代码实现 若需要前台页面Js等可私信或评论哦

Java实现登录功能(含修改密码 退出登录等)相关推荐

  1. 04 | 后台登录:基于账号密码的登录方式(上)

    你好, 我是程序猿零壹. 在上一篇文章如何快速部署一个基于laravel框架开发的网站中,我们一起使用laravel框架快速部署了一个网站.但是目前网站上只有一个默认的页面,显得有点孤单寂寞冷,是时候 ...

  2. 用户第一次登录后要求修改密码

    对于linux管理员而言,一些开发人员在便于排查故障时候需要服务器的登录权限,除了网络权限开放外,第二道防线就是服务器的用户登录权限,,而开发人员的安全意识单薄下,需要进一步保护普通用户的登录权限,因 ...

  3. Win11电脑上登录的微软账号怎么退出登录?

    Win11电脑上登录的微软账号怎么退出登录?在电脑上登录微软账号的时候,很多用户都会去选择记住账号登录.那么记住登录状态之后,我们怎么去进行退出账号的登录状态呢?接下来我们一起来看看详细的微软账号退出 ...

  4. 怎么登录远程服务器修改密码,远程服务器修改登录密码

    远程服务器修改登录密码 内容精选 换一换 本节操作以使用"Microsoft Remote Desktop for Mac"工具远程连接"Windows Server 2 ...

  5. 在Exchange 2013 OWA登录页面中修改密码

    透过OWA登录界面改密码对于使用Exchange的用户来说是一个很有有用的功能. 因为如果用户不在公司域环境中,当密码已经到期登录不了OWA,就没有办法通过OWA中的[选项]来改密码,当开启这项功能后 ...

  6. 云服务器修改密码无法登录,云服务器修改登录密码

    云服务器修改登录密码 内容精选 换一换 Linux云服务器常用的登录方式是SSH,对于密码登录方式创建的云服务器,如何保证登录安全性呢?本文以CentOS 7.6为例,对SSH登录进行安全加固.通过S ...

  7. 服务器登录密码 被修改密码,服务器登录密码被人改

    服务器登录密码被人改 内容精选 换一换 登录Windows操作系统的弹性云服务器时,需使用密码方式登录.因此,用户需先根据创建弹性云服务器时使用的密钥文件,获取该弹性云服务器初始安装时系统生成的管理员 ...

  8. java 自动登录功能_jsp实现用户自动登录功能

    理解并掌握cookie的作用以及利用cookie实现用户的自动登录功能,实现下图效果 当服务器判断出该用户是首次登录的时候,会自动跳转到登录界面等待用户登录,并填入相关信息.通过设置cookie的有效 ...

  9. 华为服务器如何登录修改密码,服务器登录修改密码

    服务器登录修改密码 内容精选 换一换 如果Windows操作系统云服务器未安装密码重置插件,可以参见本节内容重新设置密码.本节操作介绍的方法仅适用于修改Windows本地账户密码,不能修改域账户密码. ...

最新文章

  1. wpf 绘制rectangle 代码
  2. python运行过程中会被编译成二进制_Python代码在运行过程中,会被编译成二进制代码。_学小易找答案...
  3. Android NotificationCompat通知消息
  4. 认识 Linux 系统结构
  5. 计算机二级目录设置,word2设置标题格式,生成目录,奇偶页设置等等,适用考计算机二级办公软件,也适用于毕业论文格式设置...
  6. 计算机科学和建筑设计结合,智能化建筑中计算机科学与技术的应用
  7. 使用C#和ASP.NET Core的PayPal智能按钮的客户端/服务器实现
  8. php 怎么看nginx,查看nginx是否启动
  9. matlab调和均值滤波_MATLAB--数字图像处理 均值滤波
  10. 首席架构师眼中的架构应该是怎样的?
  11. 1.软件工程--软件建模与文档协作 --- 软件开发过程
  12. 桌面程序 取色器 colors.exe
  13. visio流程图的叉号_常用的流程图软件有哪些?这3款软件不可错过!
  14. 跟我学c++中级篇——decay
  15. 如何取消OPPOA33Android系统,OPPO A33系统降级教程_OPPO A33怎么回退到原来旧版本的系统...
  16. 服务器CPU占用率高,如何排查?
  17. 巨准拓客CRM【工商财税】行业获客解决方案
  18. Rancher 和 KubeSphere 的对比
  19. 香港科技大学计算机专业博士申请,协助申请研究生MSc博士PhD,香港高校【计算机2021提前批】已经开放,含【港府奖学金】...
  20. 【可达编程】最高的分数

热门文章

  1. 从卖产品到建电站 我国光伏企业海外战略升级
  2. 用EXECEL的VBE编写小学混合加减法自动习题集
  3. rw_程序是怎样跑起来的 7-12章
  4. checklistbox用法总结
  5. 网上商城的制作中需要注意哪些方面的安全问题?
  6. 非传统的“易观”,和他的技术驱动之路
  7. 获取钉钉花名册接口和枚举类
  8. “土豪金”的正确打开方式:18K黄金定制,iPhone13 Pro起售价27万元
  9. 网易云音乐歌曲没歌词用酷狗展现的方式
  10. python哨兵循环_Python哨兵控制循环