一. 登录需求分析
页面原型
1. 登录页面展示:项目路径(\resources\backend\page\login\login.html)
员工点击登录按钮进行后管平台的登录操作,登录正确以外方式不可登录。
登录处理逻辑
- 将页面提交的密码进行MD5加密
- 根据用户名查数据库(查不到返回结果)
- 比对密码(密码错误返回结果)
- 查询员工状态,员工状态禁用下不可登录
- 登录成功,写入session中,返回结果。
二. 配置返回通用结果类
package com.itheima.reggie.common;
import lombok.Data;
import java.util.HashMap;
import java.util.Map;
@Data
public class R<T> {
private Integer code;
private String msg;
private T data;
private Map map = new HashMap();
public static <T> R<T> success(T object) {
R<T> r = new R<T>();
r.data = object;
r.code = 1;
return r;
}
public static <T> R<T> error(String msg) {
R r = new R();
r.msg = msg;
r.code = 0;
return r;
}
public R<T> add(String key, Object value) {
this.map.put(key, value);
return this;
}
}
三. 登录请求API
说明 | 值 |
请求URL | /employee/login |
请求数据 | { "username": "admin", "password": "123456" } |
返回数据 | { "code": 0, "msg": "登录成功", "data": null, "map": {} } |
四. 创建实体类并实现登录逻辑
entity: 创建实体类
创建Employee.java(员工对象)
package com.itheima.reggie.entity;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import lombok.Data;
import java.io.Serializable;
import java.time.LocalDateTime;
@Data
public class Employee implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private String username;
private String name;
private String password;
private String phone;
private String sex;
private String idNumber;
private Integer status;
private LocalDateTime createTime;
private LocalDateTime updateTime;
@TableField(fill = FieldFill.INSERT)
private Long createUser;
@TableField(fill = FieldFill.INSERT_UPDATE)
private Long updateUser;
}
mapper数据库交互层
package com.itheima.reggie.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.itheima.reggie.entity.Employee;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface EmployeeMapper extends BaseMapper<Employee> {
}
service业务层接口
package com.itheima.reggie.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.itheima.reggie.entity.Employee;
import org.springframework.stereotype.Service;
public interface EmployeeService extends IService<Employee> {
}
业务层实现类
package com.itheima.reggie.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.mapper.EmployeeMapper;
import com.itheima.reggie.service.EmployeeService;
import org.springframework.stereotype.Service;
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {
}
controller控制层
package com.itheima.reggie.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.CommonsConst;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
@Resource
private EmployeeService employeeService = null;
@PostMapping("/login")
public R<Employee> login(HttpServletRequest request,
@RequestBody Employee employee) {
// 将页面提交的密码进行MD5加密
String password = employee.getPassword();
password = DigestUtils.md5DigestAsHex(password.getBytes());
// 根据用户名查数据库
LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<Employee>();
queryWrapper.eq(Employee::getUsername, employee.getUsername());
Employee emp = employeeService.getOne(queryWrapper);
// 查不到返回登录失败结果
if(emp == null){
return R.error(CommonsConst.LOGIN_FAIL);
}
// 比对密码
if(!emp.getPassword().equals(password)){
return R.error(CommonsConst.LOGIN_FAIL);
}
// 查看员工状态
if(emp.getStatus() == CommonsConst.EMPLOYEE_STATUS_NO){
return R.error(CommonsConst.LOGIN_ACCOUNT_STOP);
}
// 登录成功将员工的ID放入session中
request.getSession().setAttribute("employeeId",emp.getId());
return R.success(emp);
}
}
五. 功能测试
Debug测试时主要测试以下几点:
- 用户名与密码的校验
- 用户状态禁用情况下
- 数据是否正确返回
附录
常量类:
package com.itheima.reggie.common;
public class CommonsConst {
// 登录失败
public static final String LOGIN_FAIL = "登录失败";
// 账号禁用
public static final String LOGIN_ACCOUNT_STOP = "账号禁止使用";
// 员工账号禁用状态 0:禁用
public static final Integer EMPLOYEE_STATUS_NO = 0;
// 员工账号正常状态 1:正常使用
public static final Integer EMPLOYEE_STATUS_YES = 1;
}
到此这篇关于Java精品项目瑞吉外卖之后端登录功能篇的文章就介绍到这了,更多相关Java外卖后端登录内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!