完成用户模块
统一响应对象
注册
登录
1.创建 统一响应对象类 :com.example.mall.common.ApiResponse
package com.example.mall.common;
public class ApiResponse<T> {
private boolean success;
private String msg;
private T data;
public ApiResponse() {
}
public ApiResponse(boolean success, String msg, T data) {
this.success = success;
this.msg = msg;
this.data = data;
}
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}
2.注册(用户名:不能为空、数据库进行重名校验 密码和密码确认:不能为空、两次密码输入是否一致、密码加密存储)
1)注册 controller 层
@Autowired
private UserService userService;
@PostMapping("/register")
public ApiResponse register(@RequestParam("username") String username,
@RequestParam("password") String password,
@RequestParam("checkPassword") String checkPassword) {
ApiResponse apiResponse=new ApiResponse();
apiResponse.setData(null);
apiResponse.setSuccess(false);
if(username==null || password.equals("")){
apiResponse.setMsg("注册失败,用户名不能为空");
return apiResponse;
}
if (password==null|| username.equals("")){
apiResponse.setMsg("注册失败,密码不能为空");
return apiResponse;
}
if (password.equals(checkPassword)==false){
apiResponse.setMsg("两次密码输入的不一致,请重新输入");
return apiResponse;
}
return userService.register(username,password);
}
2)注册 service 层
@Autowired
private UserMapper userMapper;
public ApiResponse register(String username,String password){
User user=userMapper.selectByUsername(username);
ApiResponse apiResponse=new ApiResponse();
apiResponse.setSuccess(false);
apiResponse.setData(null);
if (user!=null){
apiResponse.setMsg("注册失败,该用户已经被注册!");
}else {
user=new User();
user.setUsername(username);
//对密码进行md5加密
try {
user.setPassword(MD5Utils.getMD5Str(password));
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
//调用mapper层去数据库添加一条新的用户记录
int count=userMapper.insertSelective(user);
if (count!=0){
apiResponse.setSuccess(true);
apiResponse.setMsg("注册成功,请登录!");
}else{
apiResponse.setMsg("注册失败,插入数据库失败!");
}
}
return apiResponse;
}
3)注册 dao/mapper 层
dao
User selectByUsername(String Username);
mapper
<select id="selectByUsername" parameterType="java.lang.String" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from user
where username = #{username,jdbcType=VARCHAR}
</select>
4)密码md5加密
package com.example.mall.Util;
import org.apache.tomcat.util.codec.binary.Base64;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
//MD5工具,严格说 MD5 不是一种加密算法,是一种摘要算法,摘要算法的主要特征是加密过程不需要密钥,并且经过加密的数据无法被解密, 无论多长的输入, MD5 都会输出长度为 16字节的字节数组
public class MD5Utils {
//盐值常量
private static final String SALT = "hdiwh6dhweuhid";
//接收一个未加密的字符串,返回一个加密后的字符串
public static String getMD5Str(String strValue) throws NoSuchAlgorithmException {
//返回实现MD5摘要算法的MessageDigest对象
MessageDigest md5 = MessageDigest.getInstance("MD5");
//生成的哈希值的字节数组
byte[] md5Bytes = md5.digest((strValue+SALT).getBytes());
//Base64 编码的目的是把任意二进制编码成文本
return Base64.encodeBase64String(md5Bytes);
}
}
5)postman测试结果
register
3.登录(用户名:不能为空、数据库一致性校验 密码:不能为空、数据库一致性校验)
1)登录 controller 层
@PostMapping("/login")
public ApiResponse login(@RequestParam("username") String username,
@RequestParam("password") String password
){
ApiResponse apiResponse=new ApiResponse();
apiResponse.setData(null);
apiResponse.setSuccess(false);
if(username==null || password.equals("")){
apiResponse.setMsg("注册失败,用户名不能为空");
return apiResponse;
}
if (password==null|| username.equals("")){
apiResponse.setMsg("注册失败,密码不能为空");
return apiResponse;
}
return userService.login(username,password);
}
2)登录 service 层
public ApiResponse login(String username,String password){
String md5Password="";
try {
md5Password=MD5Utils.getMD5Str(password);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
User res=userMapper.selectByUsername(username);
ApiResponse apiResponse=new ApiResponse();
if (res==null){
apiResponse.setMsg("用户为注册,请先注册");
return apiResponse;
}else{
if (res.getPassword().equals(md5Password)){
res.setPassword(null);
apiResponse.setSuccess(true);
apiResponse.setMsg("登录成功");
return apiResponse;
}else {
apiResponse.setSuccess(false);
apiResponse.setMsg("密码错误");
return apiResponse;
}
}
3)postman测试结果
login