完善登录功能
1 员工表中的密码是明文存储,安全性太低
解决办法
- 对前端提交的明文密码进行MD5加密后再与数据库中的密码进行对比。
- 利用SPring框架自带的工具类DigestUtils
password= DigestUtils.md5DigestAsHex(password.getBytes());
2 新增员工时,若录入的用户名已存在,抛出异常没有处理。
分析原因和解决方法
- 创建员工表时,规定用户名是唯一的。
- 若录入的用户名已存在,则会抛异常
- 通过全局异常处理器来统一捕获此问题引发的SQL异常:SQLIntegrityConstraintViolationException
@ExceptionHandlerpublic Result exceptionHandler(SQLIntegrityConstraintViolationException ex) {//Duplicate entry 'lisi' for key 'employee.idx_username'String message = ex.getMessage();if(message.contains("Duplicate entry")){String[] split = message.split(" ");String username=split[2];String msg=username+ MessageConstant.ALREADY_EXIST;return Result.error(msg);}else {return Result.error(MessageConstant.UNKNOWN_ERROR);}}
3 如何通过某种方式动态获取当前登录用户的id
解决方案
- 使用ThreadLocal存储登陆用户的id
- 封装ThreadLocal为一个工具类,利用get和set方法存值和取值
public class BaseContext {public static ThreadLocal<Long> threadLocal = new ThreadLocal<>();public static void setCurrentId(Long id) {threadLocal.set(id);}public static Long getCurrentId() {return threadLocal.get();}public static void removeCurrentId() {threadLocal.remove();}}
后端给前端响应的数据格式存在问题(日期格式)
注意:常用方式二