[代码优化](v2.6): 代码优化

pull/526/head
Wu Yuncheng 2020-11-13 13:28:30 +08:00
parent 80e1ba560a
commit 91e9033881
1 changed files with 7 additions and 12 deletions

View File

@ -41,16 +41,8 @@ public class SecurityUtils {
* @return UserDetails * @return UserDetails
*/ */
public static UserDetails getCurrentUser() { public static UserDetails getCurrentUser() {
final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication == null) {
throw new BadRequestException(HttpStatus.UNAUTHORIZED, "当前登录状态过期");
}
if (authentication.getPrincipal() instanceof UserDetails) {
UserDetails userDetails = (UserDetails) authentication.getPrincipal();
UserDetailsService userDetailsService = SpringContextHolder.getBean(UserDetailsService.class); UserDetailsService userDetailsService = SpringContextHolder.getBean(UserDetailsService.class);
return userDetailsService.loadUserByUsername(userDetails.getUsername()); return userDetailsService.loadUserByUsername(getCurrentUsername());
}
throw new BadRequestException(HttpStatus.UNAUTHORIZED, "找不到当前登录的信息");
} }
/** /**
@ -63,9 +55,12 @@ public class SecurityUtils {
if (authentication == null) { if (authentication == null) {
throw new BadRequestException(HttpStatus.UNAUTHORIZED, "当前登录状态过期"); throw new BadRequestException(HttpStatus.UNAUTHORIZED, "当前登录状态过期");
} }
if (authentication.getPrincipal() instanceof UserDetails) {
UserDetails userDetails = (UserDetails) authentication.getPrincipal(); UserDetails userDetails = (UserDetails) authentication.getPrincipal();
return userDetails.getUsername(); return userDetails.getUsername();
} }
throw new BadRequestException(HttpStatus.UNAUTHORIZED, "找不到当前登录的信息");
}
/** /**
* ID * ID