使用Acegi時獲取用戶信息的幾個函數
1
/** */ /**
2
* 取得當前用戶名
3
* @return
4
*/
5
public static String getUsername()
{
6
Context context = ContextHolder.getContext();
7
if (context != null )
{
8
if (context instanceof SecureContext)
{
9
SecureContext sc = (SecureContext)context;
10
Authentication auth = sc.getAuthentication();
11
if (auth != null )
{
12
Object principal = auth.getPrincipal();
13
if (principal instanceof UserDetails)
{
14
return ((UserDetails)principal).getUsername();
15
} else
{
16
return principal.toString();
17
}
18
}
19
}
20
}
21
return null ;
22
}
23
/** */ /**
24
* 取得當前用戶密碼
25
* @return
26
*/
27
public static String getPassword()
{
28
Context context = ContextHolder.getContext();
29
if (context != null )
{
30
if (context instanceof SecureContext)
{
31
SecureContext sc = (SecureContext)context;
32
Authentication auth = sc.getAuthentication();
33
if (auth != null )
{
34
Object principal = auth.getPrincipal();
35
if (principal instanceof UserDetails)
{
36
return ((UserDetails)principal).getPassword();
37
} else
{
38
return null ;
39
}
40
}
41
}
42
}
43
return null ;
44
}
45
/** */ /** */ /** */ /**
46
* 取得當前用戶session id
47
* @return sessionid or null
48
*/
49
public static String getSessionID()
{
50
Context context = ContextHolder.getContext();
51
if (context != null )
{
52
if (context instanceof SecureContext)
{
53
SecureContext sc = (SecureContext)context;
54
Authentication auth = sc.getAuthentication();
55
if (auth != null )
{
56
Object details = auth.getDetails();
57
if (details instanceof WebAuthenticationDetails)
{
58
return ((WebAuthenticationDetails)details).getSessionId();
59
} else
{
60
return null ;
61
}
62
}
63
}
64
}
65
return null ;
66
}


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



36

37



38

39

40

41

42

43

44

45


46

47

48

49



50

51



52



53

54

55



56

57



58

59



60

61

62

63

64

65

66

posted on 2007-05-20 12:43 LiuTing 閱讀(369) 評論(0) 編輯 收藏 所屬分類: 身份認證管理