Home > Java > javaTutorial > body text

How to implement Java backend login function

WBOY
Release: 2023-05-04 23:25:13
forward
1762 people have browsed it

1. Login requirements analysis

Page prototype

How to implement Java backend login function

1. Login page display: project path (\resources\backend\page\login\login .html)

How to implement Java backend login function

# Employees click the login button to log in to the back-end management platform. Login is not allowed unless the login is correct.

Login processing logic

How to implement Java backend login function

  • Encrypt the password submitted on the page with MD5

  • Check the database based on the user name (no result returned)

  • Compare the password (result returned if the password is incorrect)

  • Query employee status, employee Login is not allowed when the status is disabled

  • The login is successful, written to the session, and the result is returned.

2. Configure return general result class

package com.itheima.reggie.common;
import lombok.Data;
import java.util.HashMap;
import java.util.Map;
/**
 * 返回通用类
 * @author jekong
 * @date 2022/4/22
 */
@Data
public class R<T> {
    /** 编码:1成功,0和其它数字为失败*/
    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;
    }
}
Copy after login

3. Login request API

Description Value
Request URL/employee/login
Request data{
"username": "admin",
"password": "123456"
}
Return data{
"code ": 0,
"msg": "Login successful",
"data": null,
"map": {}
}

4. Create entity class and implement login logic

entity: Create entity class

Create Employee.java (employee object)

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;
/**
 * 员工实体类
 * @author jektong
 * @date 2022/4/21
 */
@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;
}
Copy after login

mapper database interaction layer

package com.itheima.reggie.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.itheima.reggie.entity.Employee;
import org.apache.ibatis.annotations.Mapper;
/**
 * EmployeeMapper
 * @author jektong
 * @date 2022/4/21
 */
@Mapper
public interface EmployeeMapper extends BaseMapper<Employee> {
}
Copy after login

service business layer interface

package com.itheima.reggie.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.itheima.reggie.entity.Employee;
import org.springframework.stereotype.Service;
/**
 * @author jektong
 * @date 2022/4/21
 */
public interface EmployeeService extends IService<Employee> {
}
Copy after login

Business layer implementation class

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;
/**
 * @author jektong
 * @date 2022/4/21
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {
}
Copy after login

controller control layer

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;
/**
 * 员工控制类
 *
 * @author tongbing
 * @date 2022/4/21
 */
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Resource
    private EmployeeService employeeService = null;
    /**
     * 登录请求处理
     * TODO 后续改进将业务处理的代码放入业务层,这里只做数据请求与返回
     * @param request
     * @param employee
     * @return
     */
    @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);
    }
}
Copy after login

5. Functional test

Debug test Mainly test the following points:

  1. Verification of user name and password

  2. When the user status is disabled

  3. Whether the data is returned correctly

Appendix

Constant class:

package com.itheima.reggie.common;
/**
 * 常量定义
 * @author jektong
 * @date 2022/4/23
 */
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;
}
Copy after login

The above is the detailed content of How to implement Java backend login function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!