当前位置: 首页 > news >正文

开始尝试从0写一个项目--后端(四)

借出,归还,管理

学生和管理员登录分离

学生登录到用户界面

管理员到后台

后台和用户分离

添加代码

sems-server/src/main/java/com/ljc/controller/user/UserStudentController.java

package com.ljc.controller.user;import com.ljc.constant.JwtClaimsConstant;
import com.ljc.dto.StudentLoginDTO;
import com.ljc.entity.Student;
import com.ljc.properties.JwtProperties;
import com.ljc.result.Result;
import com.ljc.service.UserStudentService;
import com.ljc.utils.JwtUtil;
import com.ljc.vo.StudentLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
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 java.util.HashMap;
import java.util.Map;@RestController
@RequestMapping("/user")
@Slf4j
@Api(tags = "用户界面")
public class UserStudentController {@Autowiredprivate JwtProperties jwtProperties;@Autowiredprivate UserStudentService userStudentService;/*** 登录** @param studentLoginDTO* @return*/@ApiOperation("用户登录")@PostMapping("/login")public Result<StudentLoginVO> login(@RequestBody StudentLoginDTO studentLoginDTO) {log.info("学生登录:{}", studentLoginDTO);Student student = userStudentService.login(studentLoginDTO);//登录成功后,生成jwt令牌Map<String, Object> claims = new HashMap<>();claims.put(JwtClaimsConstant.EMP_ID, student.getId());String token = JwtUtil.createJWT(jwtProperties.getAdminSecretKey(),jwtProperties.getAdminTtl(),claims);StudentLoginVO studentLoginVO = StudentLoginVO.builder().id(student.getId()).userName(student.getUsername()).name(student.getName()).token(token).build();return Result.success(studentLoginVO);}/*** 退出** @return*/@ApiOperation("用户退出")@PostMapping("/logout")public Result<String> logout() {return Result.success();}}

sems-server/src/main/java/com/ljc/service/UserStudentService.java

package com.ljc.service;import com.ljc.dto.StudentLoginDTO;
import com.ljc.entity.Student;
import org.springframework.stereotype.Service;public interface UserStudentService {Student login(StudentLoginDTO studentLoginDTO);
}

sems-server/src/main/java/com/ljc/service/impl/UserStudentServiceImpl.java

package com.ljc.service.impl;import com.ljc.constant.MessageConstant;
import com.ljc.constant.StatusConstant;
import com.ljc.dto.StudentLoginDTO;
import com.ljc.entity.Student;
import com.ljc.exception.AccountLockedException;
import com.ljc.exception.AccountNotFoundException;
import com.ljc.exception.PasswordErrorException;
import com.ljc.mapper.UserStudentMapper;
import com.ljc.service.UserStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;import java.util.Objects;@Service
public class UserStudentServiceImpl implements UserStudentService {@Autowiredprivate UserStudentMapper userStudentMapper;/*** 学生登录** @param studentLoginDTO* @return*/public Student login(StudentLoginDTO studentLoginDTO) {String username = studentLoginDTO.getUsername();String password = studentLoginDTO.getPassword();//1、根据用户名查询数据库中的数据Student student = userStudentMapper.getByUsername(username);//2、处理各种异常情况(用户名不存在、密码不对、账号被锁定)if (student == null) {//账号不存在throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);}//密码比对// 进行md5加密,然后再进行比对password = DigestUtils.md5DigestAsHex(password.getBytes());if (!password.equals(student.getPassword())) {//密码错误throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);}if (Objects.equals(student.getStatus(), StatusConstant.DISABLE)) {//账号被锁定throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);}//3、返回实体对象return student;}
}

sems-server/src/main/java/com/ljc/mapper/UserStudentMapper.java

package com.ljc.service.impl;import com.ljc.constant.MessageConstant;
import com.ljc.constant.StatusConstant;
import com.ljc.dto.StudentLoginDTO;
import com.ljc.entity.Student;
import com.ljc.exception.AccountLockedException;
import com.ljc.exception.AccountNotFoundException;
import com.ljc.exception.PasswordErrorException;
import com.ljc.mapper.UserStudentMapper;
import com.ljc.service.UserStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;import java.util.Objects;@Service
public class UserStudentServiceImpl implements UserStudentService {@Autowiredprivate UserStudentMapper userStudentMapper;/*** 学生登录** @param studentLoginDTO* @return*/public Student login(StudentLoginDTO studentLoginDTO) {String username = studentLoginDTO.getUsername();String password = studentLoginDTO.getPassword();//1、根据用户名查询数据库中的数据Student student = userStudentMapper.getByUsername(username);//2、处理各种异常情况(用户名不存在、密码不对、账号被锁定)if (student == null) {//账号不存在throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);}//密码比对// 进行md5加密,然后再进行比对password = DigestUtils.md5DigestAsHex(password.getBytes());if (!password.equals(student.getPassword())) {//密码错误throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);}if (Objects.equals(student.getStatus(), StatusConstant.DISABLE)) {//账号被锁定throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);}//3、返回实体对象return student;}
}

测试

未完。。。。。 


http://www.mrgr.cn/news/3976.html

相关文章:

  • Java面试题———热门篇①
  • fastapi+react实现第三方登录功能示例
  • Android系统和开发--安全性和权限管理 SELinux 策略 安全架构
  • Linux ssh 免密失效
  • linux:线程及其相关函数,线程的回收
  • Java语聊大厅个人厅陪玩厅陪玩系统小程序源码
  • <数据集>无人机航拍不同高度牧羊识别数据集<目标检测>
  • 数据结构——顺序栈和链式栈
  • 鸿蒙内核源码分析(重定位篇) | 与国际接轨
  • 详解Element-UI el-table表格中勾选checkbox(selection)多选删除
  • FL Studio24.1.1中文汉化版本下载 附带注册机
  • Midjourney进阶-反推与优化提示词(案例实操)
  • MongoDB如何实现大于小于查询
  • 自然语言处理(NLP)大模型
  • 测量 Redis 服务器的固有延迟
  • yolov10 瑞芯微RKNN、地平线Horizon芯片部署、TensorRT部署,部署工程难度小、模型推理速度快
  • BaseCTF2024 WP (主要为Rev)
  • 简单的jar包重打包Failed to get nested archive for entry 报错处理
  • 【数据结构篇】~双向链表(附源码)
  • Python爬虫:网页脚注与引用的智能捕手