当前位置:网站首页>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("退出成功");
}
边栏推荐
- 2022华数杯数学建模思路分析交流
- FPGA: Basic Getting Started LED Lights Blinking
- LeetCode 216. Combined Sum III (2022.08.04)
- IO stream articles -- based on io stream to realize folder copy (copy subfolders and files in subfolders) full of dry goods
- Can MySQL use aggregate functions without GROUP BY?
- 项目成本控制如何帮助项目成功?
- 第五章:多线程通信—wait和notify
- 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
- Pytorch Deep Learning Quick Start Tutorial -- Mound Tutorial Notes (3)
- Common operations of oracle under linux and daily accumulation of knowledge points (functions, timed tasks)
猜你喜欢
JS introduction to reverse the recycling business network of learning, simple encryption mobile phone number
Create a Dapp, why choose Polkadot?
[Strong Net Cup 2022] WP-UM
three objects are arranged in a spherical shape around the circumference
NowCoderTOP35-40 - continuous update ing
Confessing in the era of digital transformation: Mai Cong Software allows enterprises to use data in the easiest way
还在找网盘资源吗?快点收藏如下几个值得收藏的网盘资源搜索神器吧!
Microservice Technology Stack
产品太多了,如何实现一次登录多产品互通?
5. Deploy the web project to the cloud server
随机推荐
阿里顶级架构师多年总结的JVM宝典,哪里不会查哪里!
我们的Web3创业项目,黄了
NowCoderTOP35-40 - continuous update ing
语音社交软件开发——充分发挥其价值
【翻译】混沌网+SkyWalking:为混沌工程提供更好的可观察性
[Strong Net Cup 2022] WP-UM
用户考试分数大于单科科目平均分的查询
创建一个 Dapp,为什么要选择波卡?
Egg framework usage (1)
[Unity] [UGUI] [Display text on the screen]
JS introduction to reverse the recycling business network of learning, simple encryption mobile phone number
Voice-based social software development - making the most of its value
Go编译原理系列6(类型检查)
2022 Huashu Cup Mathematical Modeling Question A Optimization Design Ideas for Ring Oscillators Code Sharing
2022华数杯数学建模思路分析交流
STM32+ULN2003 drives 28BYJ4 stepper motor (forward and reverse according to the number of turns)
如何选币与确定对应策略研究
Where is your most secretive personality?
Getting started with Polkadot parachain development, this article is enough
公众号如何运维?公众号运维专业团队