|
@@ -52,26 +52,20 @@ public class StudentLoginServiceImpl implements StudentLoginService {
|
|
|
|
|
|
@Override
|
|
|
public LoginToken studentLogin(AdminPojo adminPojo) {
|
|
|
- if (adminPojo.getUsername() == null || adminPojo.getPasswd() == null || adminPojo.getUsername().equals("") || adminPojo.getPasswd().equals("")) {
|
|
|
+ if (adminPojo.getUsername() == null || adminPojo.getPasswd() == null
|
|
|
+ || adminPojo.getUsername().equals("") || adminPojo.getPasswd().equals("")) {
|
|
|
throw new EasException("用户不存在", 412);
|
|
|
}
|
|
|
EasSysStudent easSysStudent = findStudentByUsername(adminPojo.getUsername(), adminPojo.getPasswd());
|
|
|
|
|
|
- // 接收到对象,根据这个信息去查询数据库,然后自定义sql查用户信息,存在redis里面当做token
|
|
|
- System.out.println(easSysStudent);
|
|
|
-
|
|
|
Long adminId = easSysStudent.getId();
|
|
|
|
|
|
//先调用自定义sql查询detail类中的数据 最后把这个类封装到redis里面
|
|
|
UserDetail userDetail = adminLoginMapper.getStudentDetailById(adminId);
|
|
|
- System.out.println("接收前" + userDetail);
|
|
|
userDetail.setDepartments(adminLoginMapper.getStudentDepartmentsById(adminId));
|
|
|
userDetail.setPermissions(adminLoginMapper.getStudentPermissionsById(adminId));
|
|
|
- System.out.println("接收后" + userDetail);
|
|
|
|
|
|
- // 先调用自定义sql查询学生详细信息、部门信息 和 权限信息
|
|
|
UserType student = UserType.MEMBER;
|
|
|
-
|
|
|
// 生成token
|
|
|
JwtUserDto jwtUserDto = new JwtUserDto(userDetail.getUsername(), userDetail.getId(), student);
|
|
|
|
|
@@ -79,10 +73,6 @@ public class StudentLoginServiceImpl implements StudentLoginService {
|
|
|
|
|
|
String refreshToken = jwtManager.createJwt(jwtUserDto, refreshToken_expires);
|
|
|
|
|
|
- System.out.println("token" + token);
|
|
|
-
|
|
|
- System.out.println("refreshToken" + refreshToken);
|
|
|
-
|
|
|
// 生成redis key
|
|
|
String jwtTokenKey = studentLoginRedisService.createJwtTokenKey(jwtUserDto);
|
|
|
|
|
@@ -97,71 +87,10 @@ public class StudentLoginServiceImpl implements StudentLoginService {
|
|
|
return new LoginToken(token, refreshToken);
|
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
- public LoginToken refreshToken(String refreshToken) {
|
|
|
- // 解析refresh token
|
|
|
- JwtUserDto jwtUserDto = jwtManager.decodeJwt(refreshToken);
|
|
|
-
|
|
|
- //生成新的 刷新k值 (refresh_token)
|
|
|
- String newRedisRefreshTokenKey = studentLoginRedisService.createJwtRefreshTokenKey(jwtUserDto);
|
|
|
-
|
|
|
- //查询redis里面有没有这个k值
|
|
|
- UserDetail userDetail = studentLoginRedisService.loginGetCache(newRedisRefreshTokenKey);
|
|
|
-
|
|
|
- if (userDetail == null) {
|
|
|
- throw new EasException("token已过期", 412);
|
|
|
- }
|
|
|
-
|
|
|
- //如果不为空 那UserDetail里面就封装了用户的信息 生成新的token和refresh token
|
|
|
- String newToken = jwtManager.createJwt(jwtUserDto, token_expires);
|
|
|
- String newRefreshToken = jwtManager.createJwt(jwtUserDto, refreshToken_expires);
|
|
|
-
|
|
|
- //生成新的k值 (token)
|
|
|
- String newRedisTokenKey = studentLoginRedisService.createJwtTokenKey(jwtUserDto);
|
|
|
-
|
|
|
- // 更新Redis中的token和refresh token
|
|
|
- studentLoginRedisService.loginSavaCache(newRedisTokenKey, userDetail, token_expires);
|
|
|
- studentLoginRedisService.loginSavaCache(newRedisRefreshTokenKey, userDetail, refreshToken_expires);
|
|
|
- return new LoginToken(newToken, newRefreshToken);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public JsonResult verifyToJsonResult(ResponseModel response) {
|
|
|
- JsonResult result = new JsonResult();
|
|
|
-
|
|
|
- if (response != null) {
|
|
|
- result.setMsg(response.getRepMsg());
|
|
|
- result.setCode(Integer.parseInt(response.getRepCode()));
|
|
|
- result.setStatus(response.isSuccess());
|
|
|
- result.setData(response.getRepData());
|
|
|
- } else {
|
|
|
- throw new EasException("ResponseModel参数为空", 9000);
|
|
|
- }
|
|
|
-
|
|
|
- return result;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public UserDetail getStudentInfo(String token) {
|
|
|
-
|
|
|
- try {
|
|
|
- // 解析token
|
|
|
- JwtUserDto jwtUserDto = jwtManager.decodeJwt(token);
|
|
|
- // 封装成字符以便在redis中查找
|
|
|
- String jwtTokenKey = studentLoginRedisService.createJwtTokenKey(jwtUserDto);
|
|
|
- UserDetail userDetail = studentLoginRedisService.loginGetCache(jwtTokenKey);
|
|
|
- return userDetail;
|
|
|
- } catch (EasException e) {
|
|
|
- throw new EasException("Token获取学生信息失败", e);
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
private EasSysStudent findStudentByUsername(String username, String password) {
|
|
|
EasSysStudentExample easSysStudentExample = new EasSysStudentExample();
|
|
|
easSysStudentExample.createCriteria().andStudentNameEqualTo(username);
|
|
|
List<EasSysStudent> studentList = easSysStudentMapper.selectByExample(easSysStudentExample);
|
|
|
- // 使用Optional简化判断
|
|
|
Optional<EasSysStudent> optionalStudent = studentList.stream().findFirst();
|
|
|
|
|
|
if (optionalStudent.isEmpty()) {
|
|
@@ -170,7 +99,6 @@ public class StudentLoginServiceImpl implements StudentLoginService {
|
|
|
if (studentList.size() > 1) {
|
|
|
throw new EasException("用户数据异常", 9902);
|
|
|
}
|
|
|
-
|
|
|
EasSysStudent easSysStudent = optionalStudent.get();
|
|
|
// 验证密码 这个方法里面有解密 如果解密失败会抛出异常
|
|
|
validatePassword(password, easSysStudent.getPasswd());
|
|
@@ -179,8 +107,6 @@ public class StudentLoginServiceImpl implements StudentLoginService {
|
|
|
|
|
|
private void validatePassword(String inputPassword, String encryptedPassword) {
|
|
|
String decryptedPassword = passwordManager.decryptPassword(inputPassword);
|
|
|
- System.out.println("aes解密密码decryptedPassword: " + decryptedPassword);
|
|
|
- System.out.println("数据库里的加密密码easSysUserinfo.getPasswd(): " + encryptedPassword);
|
|
|
if (!passwordEncoder.matches(decryptedPassword, encryptedPassword)) {
|
|
|
throw new EasException("密码不正确", 9901);
|
|
|
}
|