1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.student.common.util.security;
17
18 import org.springframework.security.core.Authentication;
19 import org.springframework.security.core.context.SecurityContextHolder;
20 import org.springframework.security.core.userdetails.UserDetails;
21
22 public class SecurityUtils {
23
24
25
26
27
28
29 public static String getCurrentUserId() {
30 String username=null;
31 Authentication auth = SecurityContextHolder.getContext().getAuthentication();
32 if(auth!=null){
33 Object obj = auth.getPrincipal();
34 if(obj instanceof UserWithId){
35
36 username = ((UserWithId)obj).getUserId();
37 }else if (obj instanceof UserDetails) {
38 username = ((UserDetails)obj).getUsername();
39 } else {
40 username = obj.toString();
41 }
42 }
43 return username;
44 }
45
46
47
48
49
50 @Deprecated
51 public static String getCurrentPrincipalId() {
52 return getCurrentUserId();
53 }
54
55 public static String getPrincipalUserName(){
56 String username = "unknown";
57
58 Authentication auth = SecurityContextHolder.getContext().getAuthentication();
59 if (auth != null) {
60 Object obj = auth.getPrincipal();
61 if (obj instanceof UserDetails) {
62 username = ((UserDetails)obj).getUsername();
63 } else {
64 username = obj.toString();
65 }
66 }
67
68 return username;
69 }
70
71
72
73
74
75
76 @Deprecated
77 public static String getCurrentPrincipalName(){
78 return getPrincipalUserName();
79 }
80 }