diff --git a/aircraft-common/src/main/java/com/aircraft/utils/SecurityUtils.java b/aircraft-common/src/main/java/com/aircraft/utils/SecurityUtils.java index 4582472..0591349 100644 --- a/aircraft-common/src/main/java/com/aircraft/utils/SecurityUtils.java +++ b/aircraft-common/src/main/java/com/aircraft/utils/SecurityUtils.java @@ -75,25 +75,26 @@ public class SecurityUtils { userDetailsService = SpringBeanHolder.getBean("bUserDetailsService", UserDetailsService.class); } else if (UserTypeEnum.ADMIN.equals(UserTypeEnum.valueOf(userType))){ userDetailsService = SpringBeanHolder.getBean("userDetailsService", UserDetailsService.class); - } else { + } else if (UserTypeEnum.EMPLOYEES.equals(UserTypeEnum.valueOf(userType))) { userDetailsService = SpringBeanHolder.getBean("aUserDetailsService", UserDetailsService.class); + }else { + throw new RuntimeException("用户类型查询失败!"); } return userDetailsService.loadUserByUsername(getCurrentUsername()); } // 添加获取当前用户类型的方法 - public static String getCurrentUserTypeVo() { - final Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); + public static UserTypeEnum getCurrentUserTypeVo() { + Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); if (authentication == null) { throw new BadRequestException("当前登录状态过期"); } - // 从认证信息中获取用户类型(在登录时设置) if (authentication.getDetails() instanceof Map) { Map details = (Map) authentication.getDetails(); - return (String) details.get("userType"); + return UserTypeEnum.getByValue((Integer) details.get("userType")); } - return "ADMIN"; // 默认类型 + return UserTypeEnum.ADMIN; // 默认类型 } /** diff --git a/aircraft-system/src/main/java/com/aircraft/modules/security/rest/AuthController.java b/aircraft-system/src/main/java/com/aircraft/modules/security/rest/AuthController.java index 831acb5..254ee1a 100644 --- a/aircraft-system/src/main/java/com/aircraft/modules/security/rest/AuthController.java +++ b/aircraft-system/src/main/java/com/aircraft/modules/security/rest/AuthController.java @@ -98,13 +98,11 @@ public class AuthController { if (!passwordEncoder.matches(password, jwtUser.getPassword())) { throw new BadRequestException("登录密码错误"); } - Map details = new HashMap<>(); - details.put("userType", String.valueOf(jwtUser.getUserType().getValue())); UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(jwtUser, null, jwtUser.getAuthorities()); - authentication.setDetails(details); SecurityContextHolder.getContext().setAuthentication(authentication); // 生成令牌 - String token = tokenProvider.createToken(jwtUser); + String token = tokenProvider.createToken(jwtUser,Map.of( + "userType", jwtUser.getUserType().getValue())); // 返回 token 与 用户信息 Map authInfo = new HashMap(2) {{ put("token", properties.getTokenStartWith() + token); @@ -133,7 +131,8 @@ public class AuthController { Authentication authentication = new UsernamePasswordAuthenticationToken(jwtUser, null, jwtUser.getAuthorities()); SecurityContextHolder.getContext().setAuthentication(authentication); // 生成令牌 - String token = tokenProvider.createToken(jwtUser); + String token = tokenProvider.createToken(jwtUser,Map.of( + "userType", jwtUser.getUserType().getValue())); // 返回 token 与 用户信息 Map authInfo = new HashMap(2) {{ put("token", properties.getTokenStartWith() + token); @@ -165,17 +164,14 @@ public class AuthController { if (!passwordEncoder.matches(password, jwtUser.getPassword())) { throw new BadRequestException("登录密码错误"); } - Map details = new HashMap<>(); - details.put("userType", String.valueOf(jwtUser.getUserType().getValue())); // 设置认证信息 UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken( jwtUser, null, jwtUser.getAuthorities() ); - authentication.setDetails(details); SecurityContextHolder.getContext().setAuthentication(authentication); - // 生成令牌 - String token = tokenProvider.createToken(jwtUser); + String token = tokenProvider.createToken(jwtUser,Map.of( + "userType", jwtUser.getUserType().getValue())); Map authInfo = new HashMap<>(2) {{ put("token", properties.getTokenStartWith() + token); @@ -206,13 +202,11 @@ public class AuthController { if (!passwordEncoder.matches(password, jwtUser.getPassword())) { throw new BadRequestException("登录密码错误"); } - Map details = new HashMap<>(); - details.put("userType", String.valueOf(jwtUser.getUserType().getValue())); UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(jwtUser, null, jwtUser.getAuthorities()); - authentication.setDetails(details); SecurityContextHolder.getContext().setAuthentication(authentication); // 生成令牌 - String token = tokenProvider.createToken(jwtUser); + String token = tokenProvider.createToken(jwtUser,Map.of( + "userType", jwtUser.getUserType().getValue())); // 返回 token 与 用户信息 Map authInfo = new HashMap(2) {{ put("token", properties.getTokenStartWith() + token); diff --git a/aircraft-system/src/main/java/com/aircraft/modules/security/security/TokenProvider.java b/aircraft-system/src/main/java/com/aircraft/modules/security/security/TokenProvider.java index 1bdedf7..a6e588f 100644 --- a/aircraft-system/src/main/java/com/aircraft/modules/security/security/TokenProvider.java +++ b/aircraft-system/src/main/java/com/aircraft/modules/security/security/TokenProvider.java @@ -70,13 +70,14 @@ public class TokenProvider implements InitializingBean { * @param user / * @return / */ - public String createToken(JwtUserDto user) { + public String createToken(JwtUserDto user,Map values) { // 设置参数 Map claims = new HashMap<>(6); // 设置用户ID claims.put(AUTHORITIES_UID_KEY, user.getUser().getId()); // 设置UUID,确保每次Token不一样 claims.put(AUTHORITIES_UUID_KEY, IdUtil.simpleUUID()); + claims.put("userType", values.get("userType")); // 直接调用 Jwts.builder() 创建新实例 return Jwts.builder() // 设置自定义 Claims @@ -97,7 +98,12 @@ public class TokenProvider implements InitializingBean { Authentication getAuthentication(String token) { Claims claims = getClaims(token); User principal = new User(claims.getSubject(), "******", new ArrayList<>()); - return new UsernamePasswordAuthenticationToken(principal, token, new ArrayList<>()); + UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(principal, token, new ArrayList<>()); + // 从token claims中恢复details + Map details = new HashMap<>(); + details.put("userType", claims.get("userType")); + authentication.setDetails(details); + return authentication; } public Claims getClaims(String token) { diff --git a/aircraft-system/src/main/java/com/aircraft/modules/security/service/AUserDetailsService.java b/aircraft-system/src/main/java/com/aircraft/modules/security/service/AUserDetailsService.java index d80f4c9..6551bc0 100644 --- a/aircraft-system/src/main/java/com/aircraft/modules/security/service/AUserDetailsService.java +++ b/aircraft-system/src/main/java/com/aircraft/modules/security/service/AUserDetailsService.java @@ -39,7 +39,7 @@ public class AUserDetailsService implements UserDetailsService { // 使用构造函数传递必要的参数 LoginUserDto userDto = new LoginUserDto(); userDto.setId(emEmployees.getId()); - userDto.setUsername(emEmployees.getPhone()); + userDto.setUsername(emEmployees.getUsername()); userDto.setNickName(emEmployees.getName()); userDto.setDept(new Dept()); userDto.setPassword(emEmployees.getPassword()); diff --git a/aircraft-system/src/main/java/com/aircraft/modules/security/service/BUserDetailsService.java b/aircraft-system/src/main/java/com/aircraft/modules/security/service/BUserDetailsService.java index 258f89c..14b3175 100644 --- a/aircraft-system/src/main/java/com/aircraft/modules/security/service/BUserDetailsService.java +++ b/aircraft-system/src/main/java/com/aircraft/modules/security/service/BUserDetailsService.java @@ -38,7 +38,7 @@ public class BUserDetailsService implements UserDetailsService { // 使用构造函数传递必要的参数 LoginUserDto userDto = new LoginUserDto(); userDto.setId(customer.getId()); - userDto.setUsername(customer.getPhone()); + userDto.setUsername(customer.getUsername()); userDto.setNickName(customer.getName()); userDto.setDept(new Dept()); userDto.setPassword(customer.getPassword());