@@ -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