up:: 用户注册源码走读
用户登陆
实现登陆controller
//用户登陆接口
@RequestMapping(value = "/login",method = {RequestMethod.POST},consumes={CONTENT_TYPE_FORMED})
@ResponseBody
public CommonReturnType login(@RequestParam(name="telphone")String telphone,
@RequestParam(name="password")String password) throws BusinessException, UnsupportedEncodingException, NoSuchAlgorithmException {
//入参校验
if(org.apache.commons.lang3.StringUtils.isEmpty(telphone)||
StringUtils.isEmpty(password)){
throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR);
}
//用户登陆服务,用来校验用户登陆是否合法
UserModel userModel = userService.validateLogin(telphone,this.EncodeByMd5(password));
//将登陆凭证加入到用户登陆成功的session内
this.httpServletRequest.getSession().setAttribute("IS_LOGIN",true);
this.httpServletRequest.getSession().setAttribute("LOGIN_USER",userModel);
return CommonReturnType.create(null);
}
这里还是将登陆信息存放在session中,session具有这个优势。
service层进行入参检验
@Override
public UserModel validateLogin(String telphone, String encrptPassword) throws BusinessException {
//通过用户的手机获取用户信息
UserDO userDO = userDOMapper.selectByTelphone(telphone);
if(userDO == null){
throw new BusinessException(EmBusinessError.USER_LOGIN_FAIL);
}
UserPasswordDO userPasswordDO = userPasswordDOMapper.selectByUserId(userDO.getId());
UserModel userModel = convertFromDataObject(userDO,userPasswordDO);
//比对用户信息内加密的密码是否和传输进来的密码相匹配
if(!StringUtils.equals(encrptPassword,userModel.getEncrptPassword())){
throw new BusinessException(EmBusinessError.USER_LOGIN_FAIL);
}
return userModel;
}
private UserPasswordDO convertPasswordFromModel(UserModel userModel){
if(userModel == null){
return null;
}
UserPasswordDO userPasswordDO = new UserPasswordDO();
userPasswordDO.setEncrptPassword(userModel.getEncrptPassword());
userPasswordDO.setUserId(userModel.getId());
return userPasswordDO;
}
private UserDO convertFromModel(UserModel userModel){
if(userModel == null){
return null;
}
UserDO userDO = new UserDO();
BeanUtils.copyProperties(userModel,userDO);
return userDO;
}
private UserModel convertFromDataObject(UserDO userDO, UserPasswordDO userPasswordDO){
if(userDO == null){
return null;
}
UserModel userModel = new UserModel();
BeanUtils.copyProperties(userDO,userModel);
if(userPasswordDO != null){
userModel.setEncrptPassword(userPasswordDO.getEncrptPassword());
}
return userModel;
}
我们调用的BeanUtils是org.springframework.beans.BeanUtils;里的
关于UserModel:
说明:学习一下:
BeanUtil.copyProperties的小坑 - 简书
两个不同的包(springframework , apache)中有一个相同名字的类,相同的方法,方法的作用相同,参数个数相同。
就是参数位置不同 - -!
package org.springframework.beans;中的BeanUtils.copyProperties(A,B);
是A中的值赋给B
package org.apache.commons.beanutils;中的BeanUtils.copyProperties(A,B);
是B中的值赋给A
BeanUtils.copyProperties(a, b);
b中的存在的属性,a中一定要有,但是a中可以有多余的属性; a中与b中相同的属性都会被替换,不管是否有值; a、 b中的属性要名字相同,才能被赋值,不然的话需要手动赋值; Spring的BeanUtils的CopyProperties方法需要对应的属性有getter和setter方法; 如果存在属性完全相同的内部类,但是不是同一个内部类,即分别属于各自的内部类,则spring会认为属性不同,不会copy; spring和apache的copy属性的方法源和目的参数的位置正好相反,所以导包和调用的时候都要注意一下。
BeanUtils.copyProperties只对bean属性进行复制,这里的复制属于浅复制。且不能复制集合和数组。BeanUtils.copyProperties利用反射,直接将对象的引用set进去,并不是深拷贝。
测试登陆
假如没输密码呢?
说明:这里可以回去看看我们是如何统一处理异常的定义通用的返回对象之返回错误的信息
这里突然想到关于@ResquestParam的注解问题:用于防止传参顺序错误。。。
前端登陆看看
附上前端登陆代码
<html>
<head>
<meta charset="UTF-8">
<link href="static/assets/global/plugins/bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
<link href="static/assets/global/css/components.css" rel="stylesheet" type="text/css"/>
<link href="static/assets/admin/pages/css/login.css" rel="stylesheet" type="text/css"/>
<script src="static/assets/global/plugins/jquery-1.11.0.min.js" type="text/javascript"></script>
</head>
<body class="login">
<div class="content">
<h3 class="form-title">用户登陆</h3>
<div class="form-group">
<label class="control-label">手机号</label>
<div>
<input class="form-control" type="text" placeholder="手机号" name="telphone" id="telphone"/>
</div>
</div>
<div class="form-group">
<label class="control-label">密码</label>
<div>
<input class="form-control" type="password" placeholder="密码" name="password" id="password"/>
</div>
</div>
<div class="form-actions">
<button class="btn blue" id="login" type="submit">
登陆
</button>
<button class="btn green" id="register" type="submit">
注册
</button>
</div>
</div>
</body>
<script>
jQuery(document).ready(function(){
$("#register").on("click",function(){
window.location.href="getotp.html";
});
$("#login").on("click",function(){
var telphone = $("#telphone").val();
var password = $("#password").val();
if(telphone == null || telphone == ""){
alert("手机号不能为空");
return false;
}
if(password == null || password == ""){
alert("密码不能为空");
return false;
}
$.ajax({
type:"POST",
contentType:"application/x-www-form-urlencoded",
url:"http://localhost:8090/user/login",
data:{
"telphone":$("#telphone").val(),
"password":password
},
xhrFields:{withCredentials:true},
success:function(data){
if(data.status == "success"){
alert("登陆成功");
window.location.href="listitem.html";
}else{
alert("登陆失败,原因为"+data.data.errMsg);
}
},
error:function(data){
alert("登陆失败,原因为"+data.responseText);
}
});
return false;
});
});
</script>
</html>
商品列表展示
实现逻辑和前面的用户登陆这些没多大差别,没什么特别的新颖知识点,暂且放下。。。后面可能会进行一些补充。
主要的就是看看下面这张表: