wuheng 1 жил өмнө
parent
commit
7453c5c2a3

+ 42 - 57
service/src/main/java/com/koobietech/eas/service/impl/AdminLoginServiceImpl.java

@@ -1,13 +1,8 @@
 package com.koobietech.eas.service.impl;
 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.constant.UserType;
 import com.koobietech.eas.common.exception.EasException;
 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.pojo.JwtUserDto;
-import com.koobietech.eas.common.result.JsonResult;
 import com.koobietech.eas.common.utils.JwtManager;
 import com.koobietech.eas.common.utils.JwtManager;
 import com.koobietech.eas.common.utils.PasswordManager;
 import com.koobietech.eas.common.utils.PasswordManager;
 import com.koobietech.eas.dao.Pojo.AdminPojo;
 import com.koobietech.eas.dao.Pojo.AdminPojo;
@@ -37,8 +32,6 @@ public class AdminLoginServiceImpl implements AdminLoginService {
     @Resource
     @Resource
     private EasSysUserinfoMapper easSysUserinfoMapper;
     private EasSysUserinfoMapper easSysUserinfoMapper;
     @Resource
     @Resource
-    private MessageConfigProperties messageConfigProperties;
-    @Resource
     private LoginRedisService loginRedisService;
     private LoginRedisService loginRedisService;
     @Resource
     @Resource
     private JwtManager jwtManager;
     private JwtManager jwtManager;
@@ -49,55 +42,47 @@ public class AdminLoginServiceImpl implements AdminLoginService {
             throw new EasException("用户不存在", 509);
             throw new EasException("用户不存在", 509);
         }
         }
 
 
-        try {
-            EasSysUserinfo easSysUserinfo = findAdminByUsername(adminPojo.getUsername(), adminPojo.getPasswd());
-
-            // 接收到对象,根据这个信息去查询数据库,然后自定义sql查用户信息,存在redis里面当做token
-            System.out.println(easSysUserinfo);
-            //先获取id,进行枚举判断
-            Long id = easSysUserinfo.getId();
-            //先调用自定义sql查询detail类中的数据 最后把这个类封装到redis里面
-            UserDetail userDetailInRedis = adminLoginMapper.getUserDetailById(id);
-            System.out.println("接收前" + userDetailInRedis);
-            userDetailInRedis.setDepartments(adminLoginMapper.getUserDepartmentsById(id));
-            userDetailInRedis.setPermissions(adminLoginMapper.getUserPermissionsById(id));
-            System.out.println("接收后" + userDetailInRedis);
-
-            // 先调用自定义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);
-            String refreshToken = jwtManager.createJwt(jwtUserDto, refreshToken_expires);
-
-            // 生成redis key
-            String redisTokenKey = loginRedisService.createJwtTokenKey(jwtUserDto);
-            String redisRefreshTokenKey = loginRedisService.createJwtRefreshTokenKey(jwtUserDto);
-
-            // 将token存入redis
-            loginRedisService.loginSaveCache(redisTokenKey, userDetailInRedis, token_expires);
-            loginRedisService.loginSaveCache(redisRefreshTokenKey, userDetailInRedis, refreshToken_expires);
-
-            // 登录成功
-            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);
+        EasSysUserinfo easSysUserinfo = findAdminByUsername(adminPojo.getUsername(), adminPojo.getPasswd());
+
+        // 接收到对象,根据这个信息去查询数据库,然后自定义sql查用户信息,存在redis里面当做token
+        System.out.println(easSysUserinfo);
+        //先获取id,进行枚举判断
+        Long id = easSysUserinfo.getId();
+        //先调用自定义sql查询detail类中的数据 最后把这个类封装到redis里面
+        UserDetail userDetailInRedis = adminLoginMapper.getUserDetailById(id);
+        System.out.println("接收前" + userDetailInRedis);
+        userDetailInRedis.setDepartments(adminLoginMapper.getUserDepartmentsById(id));
+        userDetailInRedis.setPermissions(adminLoginMapper.getUserPermissionsById(id));
+        System.out.println("接收后" + userDetailInRedis);
+
+        // 先调用自定义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);
+        String refreshToken = jwtManager.createJwt(jwtUserDto, refreshToken_expires);
+
+        // 生成redis key
+        String redisTokenKey = loginRedisService.createJwtTokenKey(jwtUserDto);
+        String redisRefreshTokenKey = loginRedisService.createJwtRefreshTokenKey(jwtUserDto);
+
+        // 将token存入redis
+        loginRedisService.loginSaveCache(redisTokenKey, userDetailInRedis, token_expires);
+        loginRedisService.loginSaveCache(redisRefreshTokenKey, userDetailInRedis, refreshToken_expires);
+
+        // 登录成功
+        return new LoginToken(token, refreshToken);
     }
     }
 
 
-    private EasSysUserinfo findAdminByUsername(String username, String password) throws InvalidUserException, NonUniqueResultException, PasswordIncorrectException {
+    private EasSysUserinfo findAdminByUsername(String username, String password)  {
         EasSysUserinfoExample easSysUserinfoExample = new EasSysUserinfoExample();
         EasSysUserinfoExample easSysUserinfoExample = new EasSysUserinfoExample();
         easSysUserinfoExample.createCriteria().andUsernameEqualTo(username);
         easSysUserinfoExample.createCriteria().andUsernameEqualTo(username);
         List<EasSysUserinfo> adminList = easSysUserinfoMapper.selectByExample(easSysUserinfoExample);
         List<EasSysUserinfo> adminList = easSysUserinfoMapper.selectByExample(easSysUserinfoExample);
@@ -106,10 +91,10 @@ public class AdminLoginServiceImpl implements AdminLoginService {
         Optional<EasSysUserinfo> optionalAdmin = adminList.stream().findFirst();
         Optional<EasSysUserinfo> optionalAdmin = adminList.stream().findFirst();
 
 
         if (optionalAdmin.isEmpty()) {
         if (optionalAdmin.isEmpty()) {
-            throw new InvalidUserException();
+            throw new EasException("用户不存在", 9902);
         }
         }
         if (adminList.size() > 1) {
         if (adminList.size() > 1) {
-            throw new NonUniqueResultException();
+            throw new EasException("用户数据异常", 9902);
         }
         }
 
 
         EasSysUserinfo easSysUserinfo = optionalAdmin.get();
         EasSysUserinfo easSysUserinfo = optionalAdmin.get();
@@ -118,12 +103,12 @@ public class AdminLoginServiceImpl implements AdminLoginService {
         return easSysUserinfo;
         return easSysUserinfo;
     }
     }
 
 
-    private void validatePassword(String inputPassword, String encryptedPassword) throws PasswordIncorrectException {
+    private void validatePassword(String inputPassword, String encryptedPassword){
         String decryptedPassword = passwordManager.decryptPassword(inputPassword);
         String decryptedPassword = passwordManager.decryptPassword(inputPassword);
         System.out.println("前端解密来的decryptedPassword: " + decryptedPassword);
         System.out.println("前端解密来的decryptedPassword: " + decryptedPassword);
         System.out.println("数据库查到的密码easSysUserinfo.getPasswd(): " + encryptedPassword);
         System.out.println("数据库查到的密码easSysUserinfo.getPasswd(): " + encryptedPassword);
         if (!passwordEncoder.matches(decryptedPassword, encryptedPassword)) {
         if (!passwordEncoder.matches(decryptedPassword, encryptedPassword)) {
-            throw new PasswordIncorrectException();
+            throw new EasException("密码不正确", 9901);
         }
         }
     }
     }
 }
 }