当前位置:网站首页>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("退出成功");
}
边栏推荐
- This notebook of concurrent programming knowledge points strongly recommended by Ali will be a breakthrough for you to get an offer from a big factory
- ffmpeg drawtext add text watermark
- What is SPL?
- IO stream articles -- based on io stream to realize folder copy (copy subfolders and files in subfolders) full of dry goods
- 用户考试分数大于单科科目平均分的查询
- PHP operation mangoDb
- 一文道清什么是SPL
- [Strong Net Cup 2022] WP-UM
- 产品太多了,如何实现一次登录多产品互通?
- three.js调试工具dat.gui使用
猜你喜欢
数据中台建设(十):数据安全管理
用KUSTO查询语句(KQL)在Azure Data Explorer Database上查询LOG实战
上位机开发C#语言:模拟STC串口助手接收单片机发送数据
哪位大佬有20年4月或者1月的11G GI和ojvm补丁呀,帮忙发下?
012_SSS_ Improving Diffusion Model Efficiency Through Patching
告白数字化转型时代:麦聪软件以最简单的方式让企业把数据用起来
技术干货 | 基于 MindSpore 实现图像分割之豪斯多夫距离
Our Web3 Entrepreneurship Project, Yellow
Custom filters and interceptors implement ThreadLocal thread closure
NowCoderTOP35-40——持续更新ing
随机推荐
How does the official account operate and maintain?Public account operation and maintenance professional team
2022华数杯数学建模A题环形振荡器的优化设计思路思路代码分享
Open Source Summer | How OpenHarmony Query Device Type (eTS)
js hijacks the array push method
入门 Polkadot 平行链开发,看这一篇就够了
19. Server-side session technology Session
第七章,activiti个人任务分配,动态指定和监听器指定任务委派人「建议收藏」
企业的数字化转型到底是否可以买来?
FPGA: Use of the development environment Vivado
STM32+ULN2003驱动28BYJ4步进电机(根据圈数正转、反转)
The JVM collection that Alibaba's top architects have summarized for many years, where can't I check it!
FPGA: Basic Getting Started LED Lights Blinking
MySQL之数据视图
Microservice Technology Stack
uniapp 连接ibeacon
[强网杯2022]WP-UM
Pycharm 常用外部工具
Can MySQL use aggregate functions without GROUP BY?
第九章:activit内置用户组设计与组任务分配和IdentityService接口的使用
Where is your most secretive personality?