Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the login function of Java backend

2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "how to achieve the Java back-end login function". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

one。 Login requirement analysis

Page prototype

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

The employee clicks the login button to log in to the rear management platform, and cannot log in except in the correct way.

Login processing logic

Encrypt the password submitted by the page with MD5

Check the database according to the user name (no result can be found)

Compare passwords (password error returns results)

Query employee status. You cannot log in if employee status is disabled.

Log in successfully, write it to session, and return the result.

two。 Configuration returns generic result class package com.itheima.reggie.common;import lombok.Data;import java.util.HashMap;import java.util.Map;/** * returns generic class * @ author jekong * @ date 2022-4-22 * / @ Datapublic class R {/ * * Encoding: 1 success, 0 and other numbers return failure * / private Integer code; / * * Information return * / private String msg / * * Information return data * / private T data; / * * dynamic data * / private Map map = new HashMap (); public static R success (T object) {R r = new R (); r.data = object; r.code = 1; return r;} public static R error (String msg) {R r = new R (); r.msg = msg R.code = 0; return r;} public R add (String key, Object value) {this.map.put (key, value); return this;}} 3. Login request API description value request URL/employee/login request data {

"username": "admin"

"password": "123456"

} return data {

"code": 0

"msg": "login succeeded"

"data": null

"map": {}

} four. Create entity classes and implement login logic

Entity: create entity classes

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;/** * employee entity class * @ author jektong * @ date 2022-4-21 * / @ Datapublic class Employee implements Serializable {/ * * Serial number * / private static final long serialVersionUID = 1L; / * unique primary key * / private Long id / * * user name * / private String username; / * * name * / private String name; / * * password * / private String password; / * * phone * / private String phone; / * * gender * / private String sex; / * * ID card number * / private String idNumber; / * * status * / private Integer status; / * * creation time * / private LocalDateTime createTime / * * Update time * / private LocalDateTime updateTime; / * * use * / @ TableField (fill = FieldFill.INSERT) private Long createUser; / * * to update users using * / @ TableField (fill = FieldFill.INSERT_UPDATE) private Long updateUser;} when adding users

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 * / @ Mapperpublic interface EmployeeMapper extends BaseMapper {}

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 {}

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 * / @ Servicepublic class EmployeeServiceImpl extends ServiceImpl implements EmployeeService {}

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;/** * employee control class * * @ author tongbing * @ date 2022-4-21 * / @ Slf4j@RestController@RequestMapping ("/ employee") public class EmployeeController {@ Resource private EmployeeService employeeService = null / * * Login request processing * TODO subsequent improvements put the business processing code into the business layer Here only do data request and return * @ param request * @ param employee * @ return * / @ PostMapping ("/ login") public R login (HttpServletRequest request, @ RequestBody Employee employee) {/ / MD5 encrypt the password submitted on the page String password = employee.getPassword () Password = DigestUtils.md5DigestAsHex (password.getBytes ()); / / look up the database LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper () by user name; queryWrapper.eq (Employee::getUsername, employee.getUsername ()); Employee emp = employeeService.getOne (queryWrapper); / / No login failure result if (emp = = null) {return R.error (CommonsConst.LOGIN_FAIL) } / / compare password if (! emp.getPassword (). Equals (password)) {return R.error (CommonsConst.LOGIN_FAIL);} / / View employee status if (emp.getStatus () = = CommonsConst.EMPLOYEE_STATUS_NO) {return R.error (CommonsConst.LOGIN_ACCOUNT_STOP) Login successfully put the employee's ID in session request.getSession (). SetAttribute ("employeeId", emp.getId ()); return R.success (emp);}} five. Function test

The main points of Debug testing are as follows:

Verification of user name and password

When the user status is disabled

Whether the data is returned correctly

Appendix

Constant class:

Package com.itheima.reggie.common;/** * constant definition * @ author jektong * @ date 2022-4-23 * / public class CommonsConst {/ / login failed public static final String LOGIN_FAIL = "login failed"; / / account disabled public static final String LOGIN_ACCOUNT_STOP = "account prohibited"; / / employee account disabled status 0: disable public static final Integer EMPLOYEE_STATUS_NO = 0 / / employee account normal status 1: normal use public static final Integer EMPLOYEE_STATUS_YES = 1;} "how to implement the Java backend login function" is introduced here. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report