借出,归还,管理
学生和管理员登录分离
学生登录到用户界面
管理员到后台
后台和用户分离
添加代码
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 {
@Autowired
private JwtProperties jwtProperties;
@Autowired
private 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 {
@Autowired
private 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 {
@Autowired
private 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;
}
}
测试
未完。。。。。