1
zj
2025-04-17 ff2d1f5acdadc466d7e199028ef385ae8ca277e7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package security;
 
import org.springframework.security.Authentication;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.security.userdetails.UserDetails;
 
public class SecurityAppUserHolder {
 
    public static SecUser getCurrentUser() {
        Authentication authentication = getAuthentication();
        if (null != authentication) {
            Object principal = authentication.getPrincipal();
            if (principal instanceof UserDetails) {
                return (SecUser) principal;
            }
        }
        return null;
    }
    
    public static String gettUsername() {
        Authentication authentication = getAuthentication();
        if (null != authentication) {
            Object principal = authentication.getPrincipal();
            if (principal instanceof UserDetails) {
                return ((SecUser) principal).getUsername();
            }
        }
        return null;
    }
 
    public static Authentication getAuthentication() {
        return SecurityContextHolder.getContext().getAuthentication();
    }
 
}