Java CRM系统用户登录功能实现代码实例

所属分类: 软件编程 / java 阅读数: 56
收藏 0 赞 0 分享

用户登录功能后台代码实现:

UserMapper接口查询方法定义

/**
 * 
 * @param userName
 * @param userPwd
 * @param roleName
 * 查询用户记录
 * @return
 */
User queryUser(@Param("userName")String userName);
 
<!-- 查询用户 -->
<select id="queryUser" resultMap="BaseResultMap">
   select <include refid="Base_Column_List"/>
    from t_user where user_name =#{userName} 
</select>

service层登录方法实现

 

@Resource
private UserDao userDao;
 
 /**
 * 用户登录
 * @param userName
 * @param userPwd
 * @param roleName
 * @return
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public UserModel userLoginCheck(String userName,String userPwd){
 /**
   * 1.参数合法性校验
   * 2.查询用户 有效性校验
   * 3.密码校验
   * 4.返回用户模型信息
   */
 // 参数校验
  checkParam(userName,userPwd);
 // 执行查询
 
  User user=userDao.queryUser(userName);
 // 用户存在性校验
  AssertUtil.isTrue(null==user, "该用户不存在!"); 
  AssertUtil.isTrue(user.getIsValid()==0, "给用户已注销!");
 //密码校验
  AssertUtil.isTrue(!user.getUserPwd().equals(MD5Util.md5Method(userPwd)),"密码不正确!");
 // 构建用户信息模型
  UserModel userModel=buildUserModel(user);
 return userModel; 
}
 
/**
 * 登录参数校验
 * @param userName
 * @param userPwd
 * @param roleName
 */
private void checkParam(String userName, String userPwd, Integer roleId) {
  AssertUtil.isTrue(StringUtil.isNullOrEmpty(userName), "用户名非空!");
  AssertUtil.isTrue(StringUtil.isNullOrEmpty(userPwd), "密码非空!");
}
 
/**
 * 构建用户登录数据模型
 * @param user
 * @return
 */
private UserModel buildUserModel(User user) {
  UserModel userModel=new UserModel(); 
  userModel.setRoleName(user.getRoleName());
  userModel.setUserName(user.getUserName());
  userModel.setTrueName(user.getTrueName());
  String userIdStr=UserIDBase64.encoderUserID(user.getId());
  userModel.setUserIdStr(userIdStr);
 return userModel;
}

UserController层

package com.shsxt.crm.controller;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.shsxt.base.BaseController;
import com.shsxt.base.ResultInfo;
import com.shsxt.base.exception.ParamException;
import com.shsxt.crm.model.UserModel;
import com.shsxt.crm.service.UserService;
 
@Controller
@RequestMapping("user")
public class UserController extends BaseController {
 
  @Resource
private UserService userService;
@RequestMapping("userLogin")
public @ResponseBody MessageModel userLogin(String userName,
    String userPwd){
  MessageModel resultInfo=null;
 try {
    UserModel userModel= userService.userLoginCheck(userName, userPwd);
    resultInfo=success();
    resultInfo.setResult(userModel);
 } catch (ParamsException e) {
    e.printStackTrace();
    resultInfo=failed(e.getMessage());
 } 
 return resultInfo;
} 
 
}

前台js控制表单提交

绑定提交按钮点击事件

// 提交绑定事件
$(function(){
  $("#btnLogin").click(function(){ 
 var userName=$("#userName").val();
 var userPwd=$("#userPwd").val();
 if(isEmpty(userName)){
 alert("用户名非空!");
 return;
 }
 
 if(isEmpty(userPwd)){
 alert("密码非空!");
 return;
 } 
 
 var param={};
    param.userName=userName;
    param.userPwd=userPwd; 
       $.ajax({
      type:"post",
      url:"user/userLogin",
      data:param,
      dataType:"json",
      success:function(data){
 if(data.resultCode==200){
 /**
           * 登录成功
           * 写入cookie
           */
          $.cookie("userName",data.result.userName);
          $.cookie("trueName",data.result.trueName);
          $.cookie("userIdStr",data.result.userIdStr); 
 // 执行跳转
 window.location.href="main" rel="external nofollow" ;
 }else{
 alert(data.msg);
 }
 } 
 })
 })
})

部署项目,执行登录操作

登录成功 cookie 写入成功

登录成功,跳转至后台管理页面

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

JavaWeb项目部署到服务器详细步骤详解

这篇文章主要介绍了JavaWeb项目如何部署到服务器,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

IDEA基于支付宝小程序搭建springboot项目的详细步骤

这篇文章主要介绍了IDEA基于支付宝小程序搭建springboot项目的详细步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解SpringBoot应用服务启动与安全终止

这篇文章主要介绍了SpringBoot应用服务启动与安全终止,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Boot启动及退出加载项的方法

这篇文章主要介绍了Spring Boot启动及退出加载项的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Data Jpa 自动生成表结构的方法示例

这篇文章主要介绍了Spring Data Jpa 自动生成表结构的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

IDEA中osgi的开发应用指南详解

这篇文章主要介绍了IDEA中osgi的开发应用指南详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解用maven将dubbo工程打成jar包运行

这篇文章主要介绍了详解用maven将dubbo工程打成jar包运行,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

详解Java合并数组的两种实现方式

这篇文章主要介绍了Java合并数组的两种实现方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

使用Jenkins Pipeline自动化构建发布Java项目的方法

这篇文章主要介绍了使用Jenkins Pipeline自动化构建发布Java项目的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

使用Maven配置Spring的方法步骤

这篇文章主要介绍了使用Maven配置Spring的方法步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享
查看更多