未验证 提交 11bf5868 编写于 作者: W wuyc 提交者: GitHub

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

上级 f56006f6
...@@ -41,16 +41,8 @@ public class SecurityUtils { ...@@ -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(); UserDetailsService userDetailsService = SpringContextHolder.getBean(UserDetailsService.class);
if (authentication == null) { return userDetailsService.loadUserByUsername(getCurrentUsername());
throw new BadRequestException(HttpStatus.UNAUTHORIZED, "当前登录状态过期");
}
if (authentication.getPrincipal() instanceof UserDetails) {
UserDetails userDetails = (UserDetails) authentication.getPrincipal();
UserDetailsService userDetailsService = SpringContextHolder.getBean(UserDetailsService.class);
return userDetailsService.loadUserByUsername(userDetails.getUsername());
}
throw new BadRequestException(HttpStatus.UNAUTHORIZED, "找不到当前登录的信息");
} }
/** /**
...@@ -63,8 +55,11 @@ public class SecurityUtils { ...@@ -63,8 +55,11 @@ public class SecurityUtils {
if (authentication == null) { if (authentication == null) {
throw new BadRequestException(HttpStatus.UNAUTHORIZED, "当前登录状态过期"); throw new BadRequestException(HttpStatus.UNAUTHORIZED, "当前登录状态过期");
} }
UserDetails userDetails = (UserDetails) authentication.getPrincipal(); if (authentication.getPrincipal() instanceof UserDetails) {
return userDetails.getUsername(); UserDetails userDetails = (UserDetails) authentication.getPrincipal();
return userDetails.getUsername();
}
throw new BadRequestException(HttpStatus.UNAUTHORIZED, "找不到当前登录的信息");
} }
/** /**
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册