Skip to content

Commit 11bf586

Browse files
author
wuyc
authored
[代码优化](v2.6): 代码优化 (elunez#526)
1 parent f56006f commit 11bf586

1 file changed

Lines changed: 7 additions & 12 deletions

File tree

eladmin-common/src/main/java/me/zhengjie/utils/SecurityUtils.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,8 @@ public class SecurityUtils {
4141
* @return UserDetails
4242
*/
4343
public static UserDetails getCurrentUser() {
44-
final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
45-
if (authentication == null) {
46-
throw new BadRequestException(HttpStatus.UNAUTHORIZED, "当前登录状态过期");
47-
}
48-
if (authentication.getPrincipal() instanceof UserDetails) {
49-
UserDetails userDetails = (UserDetails) authentication.getPrincipal();
50-
UserDetailsService userDetailsService = SpringContextHolder.getBean(UserDetailsService.class);
51-
return userDetailsService.loadUserByUsername(userDetails.getUsername());
52-
}
53-
throw new BadRequestException(HttpStatus.UNAUTHORIZED, "找不到当前登录的信息");
44+
UserDetailsService userDetailsService = SpringContextHolder.getBean(UserDetailsService.class);
45+
return userDetailsService.loadUserByUsername(getCurrentUsername());
5446
}
5547

5648
/**
@@ -63,8 +55,11 @@ public static String getCurrentUsername() {
6355
if (authentication == null) {
6456
throw new BadRequestException(HttpStatus.UNAUTHORIZED, "当前登录状态过期");
6557
}
66-
UserDetails userDetails = (UserDetails) authentication.getPrincipal();
67-
return userDetails.getUsername();
58+
if (authentication.getPrincipal() instanceof UserDetails) {
59+
UserDetails userDetails = (UserDetails) authentication.getPrincipal();
60+
return userDetails.getUsername();
61+
}
62+
throw new BadRequestException(HttpStatus.UNAUTHORIZED, "找不到当前登录的信息");
6863
}
6964

7065
/**

0 commit comments

Comments
 (0)