-
业务层要抛出可能存在的异常,而用户注册过程中可能出现的异常有用户名已被其他用户使用,服务器宕机,数据库宕机等问题,都是运行时异常,所以创建一个业务层异常基类,继承运行时异常,重写它的方法
package com.cy.store.service.ex; public class ServiceException extends RuntimeException { public ServiceException() { super(); } public ServiceException(String message) { super(message); } public ServiceException(String message, Throwable cause) { super(message, cause); } public ServiceException(Throwable cause) { super(cause); } protected ServiceException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { super(message, cause, enableSuppression, writableStackTrace); } } -
用户名已经存在的异常,继承业务层异常,并重写父类的方法
package com.cy.store.service.ex; public class UsernameDuplicatedException extends ServiceException { public UsernameDuplicatedException() { super(); } public UsernameDuplicatedException(String message) { super(message); } public UsernameDuplicatedException(String message, Throwable cause) { super(message, cause); } public UsernameDuplicatedException(Throwable cause) { super(cause); } protected UsernameDuplicatedException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { super(message, cause, enableSuppression, writableStackTrace); } } -
在插入过程中可能存在的其他异常,同样也继承业务层的异常基类,重写父类的方法
package com.cy.store.service.ex; public class InsertException extends ServiceException{ public InsertException() { super(); } public InsertException(String message) { super(message); } public InsertException(String message, Throwable cause) { super(message, cause); } public InsertException(Throwable cause) { super(cause); } protected InsertException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { super(message, cause, enableSuppression, writableStackTrace); } }
- 判断用户的用户名是否已被使用,如果是,抛出UsernameDuplicatedException异常
- 如果在注册过程中出现异常,抛出InsertException异常
- 用户注册前,对密码进行md5加密
package com.cy.store.service;
import com.cy.store.entity.User;
public interface IUserService {
void registerUser(User user);
}
业务层接口的实现类
package com.cy.store.service.impl;
import com.cy.store.dao.UserDao;
import com.cy.store.entity.User;
import com.cy.store.service.IUserService;
import com.cy.store.service.ex.InsertException;
import com.cy.store.service.ex.UsernameDuplicatedException;
import com.cy.store.util.Encryption;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.UUID;
@Service
public class UserServiceImpl implements IUserService{
@Autowired
private UserDao userDao;
@Override
public void registerUser(User user) {
// 首先判断用户名是否已被占用,占用的话抛出异常
User resuUser = userDao.selectUserByUsername(user.getUsername());
if (resuUser != null) {
throw new UsernameDuplicatedException("用户名已经被占用");
}
// 用户注册之前,补全除了用户输入的其他信息
user.setIsDelete(0);
user.setCreatedUser(user.getUsername());
user.setModifiedUser(user.getUsername());
Date date = new Date();
user.setCreatedTime(date);
user.setModifiedTime(date);
// 用户密码加密(md5算法)
String prePassword = user.getPassword();
// 获取盐值(随机生成)
String salt = UUID.randomUUID().toString().toUpperCase();
// 密码加密处理
String encryptedPassword = Encryption.getEncryptedPassword(prePassword, salt);
user.setPassword(encryptedPassword);
// 补全盐值,作用: 用户在登录的时候,通过前端输入的密码和后台取出来的盐值进行md5加密后,比对后台存储的加密过的密码,进行密码验证
user.setSalt(salt);
// 用户注册,注册失败,抛出注册过程中未知异常
Integer row = userDao.insertUser(user);
if (row != 1) {
throw new InsertException("在插入数据的过程中发生了未知异常");
}
}
}
加密工具类
package com.cy.store.util;
import org.springframework.util.DigestUtils;
public class Encryption {
public static String getEncryptedPassword(String password, String salt) {
// md5三次加密
for (int i = 0; i < 3; i++) {
password = DigestUtils.md5DigestAsHex( (salt+password+salt).getBytes() ).toUpperCase();
}
return password;
}
}



