|
@@ -1,13 +1,8 @@
|
|
|
package com.koobietech.eas.service.impl;
|
|
|
|
|
|
-import com.koobietech.eas.common.config.MessageConfigProperties;
|
|
|
import com.koobietech.eas.common.constant.UserType;
|
|
|
import com.koobietech.eas.common.exception.EasException;
|
|
|
-import com.koobietech.eas.common.exception.InvalidUserException;
|
|
|
-import com.koobietech.eas.common.exception.NonUniqueResultException;
|
|
|
-import com.koobietech.eas.common.exception.PasswordIncorrectException;
|
|
|
import com.koobietech.eas.common.pojo.JwtUserDto;
|
|
|
-import com.koobietech.eas.common.result.JsonResult;
|
|
|
import com.koobietech.eas.common.utils.JwtManager;
|
|
|
import com.koobietech.eas.common.utils.PasswordManager;
|
|
|
import com.koobietech.eas.dao.Pojo.AdminPojo;
|
|
@@ -21,6 +16,7 @@ import com.koobietech.eas.service.AdminLoginService;
|
|
|
import com.koobietech.eas.service.LoginRedisService;
|
|
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
import javax.annotation.Resource;
|
|
|
import java.util.List;
|
|
|
import java.util.Optional;
|
|
@@ -37,16 +33,18 @@ public class AdminLoginServiceImpl implements AdminLoginService {
|
|
|
@Resource
|
|
|
private EasSysUserinfoMapper easSysUserinfoMapper;
|
|
|
@Resource
|
|
|
- private MessageConfigProperties messageConfigProperties;
|
|
|
- @Resource
|
|
|
private LoginRedisService loginRedisService;
|
|
|
@Resource
|
|
|
private JwtManager jwtManager;
|
|
|
|
|
|
+ // token过期时间 单位:s
|
|
|
+ private final Integer token_expires = 30;
|
|
|
+ private final Integer refreshToken_expires = 60;
|
|
|
+
|
|
|
@Override
|
|
|
public LoginToken adminLogin(AdminPojo adminPojo) {
|
|
|
if (adminPojo.getUsername() == null || adminPojo.getPasswd() == null || adminPojo.getUsername().isEmpty() || adminPojo.getPasswd().isEmpty()) {
|
|
|
- throw new EasException("用户不存在", 509);
|
|
|
+ throw new EasException("用户不存在", 412);
|
|
|
}
|
|
|
|
|
|
try {
|
|
@@ -63,16 +61,12 @@ public class AdminLoginServiceImpl implements AdminLoginService {
|
|
|
userDetailInRedis.setPermissions(adminLoginMapper.getUserPermissionsById(id));
|
|
|
System.out.println("接收后" + userDetailInRedis);
|
|
|
|
|
|
- // 先调用自定义sql查询用户详细信息、部门信息和权限信息
|
|
|
+ // 先调用自定义sql查询用户详细信息、部门信息 和 权限信息
|
|
|
UserType userType = UserType.TEACHER;
|
|
|
if (userDetailInRedis.getUsername().equals("admin")) {
|
|
|
userType = UserType.ADMIN;
|
|
|
}
|
|
|
|
|
|
- // token过期时间
|
|
|
- Integer token_expires = 30;
|
|
|
- Integer refreshToken_expires = 60;
|
|
|
-
|
|
|
// 生成token
|
|
|
JwtUserDto jwtUserDto = new JwtUserDto(userDetailInRedis.getUsername(), userDetailInRedis.getId(), userType);
|
|
|
String token = jwtManager.createJwt(jwtUserDto, token_expires);
|
|
@@ -88,16 +82,41 @@ public class AdminLoginServiceImpl implements AdminLoginService {
|
|
|
|
|
|
// 登录成功
|
|
|
return new LoginToken(token, refreshToken);
|
|
|
- } catch (InvalidUserException e) {
|
|
|
- throw new EasException("用户不存在", 409);
|
|
|
- } catch (NonUniqueResultException e) {
|
|
|
- throw new EasException("用户不存在", 409);
|
|
|
- } catch (PasswordIncorrectException e) {
|
|
|
- throw new EasException("用户不存在", 409);
|
|
|
+ } catch (EasException e) {
|
|
|
+ throw new EasException("登录失败", 500);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public LoginToken refreshToken(String refreshToken) {
|
|
|
+ // 解析refresh token
|
|
|
+ JwtUserDto jwtUserDto = jwtManager.decodeJwt(refreshToken);
|
|
|
+
|
|
|
+ //生成新的 刷新k值 (refresh_token)
|
|
|
+ String newRedisRefreshTokenKey = loginRedisService.createJwtRefreshTokenKey(jwtUserDto);
|
|
|
+
|
|
|
+ //查询redis里面有没有这个k值
|
|
|
+ UserDetail userDetailInRedis = loginRedisService.loginGetCache(newRedisRefreshTokenKey);
|
|
|
+ if (userDetailInRedis == null) {
|
|
|
+ throw new EasException("refresh token已过期", 412);
|
|
|
}
|
|
|
+ //如果不为空 那UserDetail里面就封装了用户的信息 生成新的token和refresh token
|
|
|
+ String newToken = jwtManager.createJwt(jwtUserDto, token_expires);
|
|
|
+ String newRefreshToken = jwtManager.createJwt(jwtUserDto, refreshToken_expires);
|
|
|
+
|
|
|
+ //生成新的k值 (token)
|
|
|
+ String newRedisTokenKey = loginRedisService.createJwtTokenKey(jwtUserDto);
|
|
|
+
|
|
|
+ // 更新Redis中的token和refresh token
|
|
|
+ loginRedisService.loginSaveCache(newRedisTokenKey, userDetailInRedis, token_expires);
|
|
|
+ loginRedisService.loginSaveCache(newRedisRefreshTokenKey, userDetailInRedis, refreshToken_expires);
|
|
|
+ return new LoginToken(newToken, newRefreshToken);
|
|
|
}
|
|
|
|
|
|
- private EasSysUserinfo findAdminByUsername(String username, String password) throws InvalidUserException, NonUniqueResultException, PasswordIncorrectException {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private EasSysUserinfo findAdminByUsername(String username, String password) throws EasException {
|
|
|
EasSysUserinfoExample easSysUserinfoExample = new EasSysUserinfoExample();
|
|
|
easSysUserinfoExample.createCriteria().andUsernameEqualTo(username);
|
|
|
List<EasSysUserinfo> adminList = easSysUserinfoMapper.selectByExample(easSysUserinfoExample);
|
|
@@ -106,24 +125,26 @@ public class AdminLoginServiceImpl implements AdminLoginService {
|
|
|
Optional<EasSysUserinfo> optionalAdmin = adminList.stream().findFirst();
|
|
|
|
|
|
if (optionalAdmin.isEmpty()) {
|
|
|
- throw new InvalidUserException();
|
|
|
+ throw new EasException("用户不存在", 409);
|
|
|
}
|
|
|
if (adminList.size() > 1) {
|
|
|
- throw new NonUniqueResultException();
|
|
|
+ throw new EasException("用户不唯一", 410);
|
|
|
}
|
|
|
|
|
|
EasSysUserinfo easSysUserinfo = optionalAdmin.get();
|
|
|
+
|
|
|
+ // 验证密码 这个方法里面有解密 如果解密失败会抛出异常
|
|
|
validatePassword(password, easSysUserinfo.getPasswd());
|
|
|
|
|
|
return easSysUserinfo;
|
|
|
}
|
|
|
|
|
|
- private void validatePassword(String inputPassword, String encryptedPassword) throws PasswordIncorrectException {
|
|
|
+ private void validatePassword(String inputPassword, String encryptedPassword) throws EasException {
|
|
|
String decryptedPassword = passwordManager.decryptPassword(inputPassword);
|
|
|
System.out.println("前端解密来的decryptedPassword: " + decryptedPassword);
|
|
|
System.out.println("数据库查到的密码easSysUserinfo.getPasswd(): " + encryptedPassword);
|
|
|
if (!passwordEncoder.matches(decryptedPassword, encryptedPassword)) {
|
|
|
- throw new PasswordIncorrectException();
|
|
|
+ throw new EasException("密码错误", 411);
|
|
|
}
|
|
|
}
|
|
|
}
|