当前位置:网站首页>Login function and logout function (St. Regis Takeaway)
Login function and logout function (St. Regis Takeaway)
2022-08-05 10:19:00 【Peter Pan who won't bend】
博客主页:@不会压弯的小飞侠欢迎关注:点赞收藏留言系列专栏:瑞吉外卖知足上进,不负野心.欢迎大佬指正,一起学习!一起加油!

目录
知识总结
@Log4j注解在类上,为类提供一个属性名为log的log4j日志对象
MP- Mapper层的接口,需要继承
BaseMapper<T>,并使用泛型!MP简化了单表CRUD的SQL操作,多表最好还是要自己写 SQL的. - service层需要继承
IService<T>实现层也要继承对应的实现类. - Implement layer inheritance
ServiceImpl<M extends BaseMapper, T>并实现Service接口 - ( 泛型:M 是 mapper 对象,T 是实体 )
- Mapper层的接口,需要继承
密码md5加密- 在使用Spring框架的时候,Some important passwords will be encrypted and can be usedMD5,Its tool class is DigestUtils;
- Springframe inherited:
import org.springframework.util.DigestUtils;
String inputMD = "123456";
DigestUtils.md5DigestAsHex(inputMD.getBytes());
掌握MyBatis-Plus 之LambdaQueryWrapper的使用可以在MyBatis-plus专栏学习.@Controller注解- 在一个类上添加@Controller注解,表明了这个类是一个控制器类.
@ResponseBody注解- @ResponseBody表示方法的返回值直接以指定的格式写入Http response body中,而不是解析为跳转路径.Format conversion is throughHttpMessageConverter中的方法实现的,因为它是一个接口,因此由其实现类完成转换.如果要求方法返回的是json格式数据,而不是跳转页面,可以直接在类上标注,@RestController,而不用在每个方法中标注,@ResponseBody,简化了开发过程.
@Controller和@RestController的区别:- @Controller:在对应的方法上,视图解析器可以解析return的jsp,html页面,并且跳转到相应页面,若返回json等内容到页面,则需要加@ResponseBody注解
- @RestController:相当于@[email protected]两个注解的结合,返回jsonData does not need to precede the [email protected]注解了,但使用@RestController这个注解,就不能返回jsp,html页面,视图解析器无法解析jsp,html页面
Controller 方法参数引入 HttpServletRequest- 在 Controller 方法开始处理请求时,Spring 会将 HttpServletRequest 对象自动赋值到方法参数中.除 HttpServletRequest 对象外,还有很多其它参数可以通过此方法获取.
比如访问一个网站,User information is saved to after loginsession中,在sessionBefore it expires or before the user closes the page,User information can be passedrequest.getSession().getAttribute()方式 获得request.getSession().removeAttribute()- 作用:
销毁当前会话域中的一个属性.
- 作用:
登录页面测试
- 通过浏览器调试工具(
F12快捷键),点击登陆后,页面会发送请求 - 请求地址为:
localhost:8080/employee/login - 请求方式:
post - 提交参数:
username,password

代码开发
创建实体类Employee和employee表进行映射
- 编写实体类,放在entity包下
package com.jkj.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;
}
编写EmployeeMapper接口
- EmployeeMapper接口,放在mapper包下
package com.jkj.reggie.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.jkj.reggie.entity.Employee;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface EmployeeMapper extends BaseMapper<Employee> {
}
编写EmployeeService接口
- EmployeeService接口放在service包下
package com.jkj.reggie.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.jkj.reggie.entity.Employee;
public interface EmployeeService extends IService<Employee> {
}
编写EmployeeServiceImpl实现类
- EmployeeServiceImpl实现类放在impl包下
package com.jkj.reggie.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jkj.reggie.entity.Employee;
import com.jkj.reggie.mapper.EmployeeMapper;
import com.jkj.reggie.service.EmployeeService;
import org.springframework.stereotype.Service;
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {
}
编写返回结果类R
- 此类是一个通用结果类,All results of the server response will eventually be wrapped in this type,返回给前端页面
- 放在common包下
package com.jkj.reggie.common;
import lombok.Data;
import java.util.HashMap;
import java.util.Map;
@Data
public class R<T> {
private Integer code; //编码:1成功,0和其它数字为失败
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;
}
}
编写EmployeeController类
- 处理逻辑
- 1、The password will be submitted straight to the pagepassword进行md5加密处理
- 2、根据页面提交的用户名username查询数据库
- 3、如果没有查询到则返回登录失败结果
- 4、密码比对,如果不一致则返回登录失败结果
- 5、查看员工状态,如果为已禁用状态,则返回员工已禁用结果、登录成功,将员工id存入Session并返回登录成功结果
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
/** * 员工登录 * @param request * @param employee * @return */
@PostMapping("/login")
public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
//1、将页面提交的密码password进行md5加密处理
String password = employee.getPassword();
password = DigestUtils.md5DigestAsHex(password.getBytes());
//2、根据页面提交的用户名username查询数据库
LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(Employee::getUsername,employee.getUsername());
Employee emp = employeeService.getOne(queryWrapper);
//3、如果没有查询到则返回登录失败结果
if(emp == null){
return R.error("登录失败");
}
//4、密码比对,如果不一致则返回登录失败结果
if(!emp.getPassword().equals(password)){
return R.error("登录失败");
}
//5、查看员工状态,如果为已禁用状态,则返回员工已禁用结果
if(emp.getStatus() == 0){
return R.error("账号已禁用");
}
//6、登录成功,将员工id存入Session并返回登录成功结果
request.getSession().setAttribute("employee",emp.getId());
return R.success(emp);
/** * 员工退出 * @param request * @return */
}
}
登录功能测试
- 启动项目之后,在浏览器输入
localhost:8080/backend/page/login/login.html

登录成功

In case of wrong password,会提示登录失败

In case of wrong user name,会提示登录失败

将用户status属性修改为0,Prompt account is disabled information

退出功能
- in the upper right corner of the system home page,Click the shutdown button,You can exit the system,跳转到登录页面.

- 通过浏览器调试工具(
F12快捷键),After clicking the shutdown button,页面会发送请求 - 请求地址为:
localhost:8080/employee/logout - 请求方式:
post - 逻辑:
清理session中的id返回结果

- 编写退出方法
/** * 员工退出 * @param request * @return */
@PostMapping("/logout")
public R<String> logout(HttpServletRequest request){
//清理Session中保存的当前登录员工的id
request.getSession().removeAttribute("employee");
return R.success("退出成功");
}
边栏推荐
- The fuse: OAuth 2.0 four authorized login methods must read
- 你最隐秘的性格在哪?
- High-quality DeFi application building guide to help developers enjoy DeFi Summer
- In-depth understanding of timeout settings for Istio traffic management
- [Office] Collection of Microsoft Office download addresses (offline installation and download of Microsoft's official original version)
- Still looking for a network backup resources?Hurry up to collect the following network backup resource search artifact it is worth collecting!
- 【综合类型第 35 篇】程序员的七夕浪漫时刻
- js劫持数组push方法
- Data Middle Office Construction (10): Data Security Management
- Which big guy has the 11G GI and ojvm patches in April or January 2020, please help?
猜你喜欢

Open Source Summer | How OpenHarmony Query Device Type (eTS)

leetcode: 529. Minesweeper Game

2022华数杯数学建模A题环形振荡器的优化设计思路思路代码分享

three.js debugging tool dat.gui use

MySQL advanced (twenty-seven) database index principle

还在找网盘资源吗?快点收藏如下几个值得收藏的网盘资源搜索神器吧!

【AGC】增长服务1-远程配置示例

How can project cost control help project success?

Microservice Technology Stack

Qiu Jun, CEO of Eggplant Technology: Focus on users and make products that users really need
随机推荐
RT-Thread记录(一、RT-Thread 版本、RT-Thread Studio开发环境 及 配合CubeMX开发快速上手)
Go compilation principle series 6 (type checking)
GCC编译的时候头文件搜索规则
LeetCode 216. Combined Sum III (2022.08.04)
egg框架使用(一)
技术干货 | 基于 MindSpore 实现图像分割之豪斯多夫距离
Handwriting Currying - toString Comprehension
The founder of the DFINITY Foundation talks about the ups and downs of the bear market, and where should DeFi projects go?
电竞、便捷、高效、安全,盘点OriginOS功能的关键词
浅析WSGI协议
What is the function of the regular expression replaceFirst() method?
登录功能和退出功能(瑞吉外卖)
hcip BGP enhancement experiment
首次去中心化抢劫?近2亿美元损失:跨链桥Nomad 被攻击事件分析
静态链接和动态链接
What is the function of the regular expression replaceAll() method?
【MindSpore易点通机器人-01】你也许见过很多知识问答机器人,但这个有点不一样
多线程(进阶) - 2.5w字总结
2022华数杯数学建模思路分析交流
Wei Dongshan Digital Photo Frame Project Learning (6) Transplantation of tslib
