随笔 - 10, 文章 - 0, 评论 - 7, 引用 - 0
数据加载中……

使用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   }

posted on 2007-05-20 12:43 LiuTing 阅读(348) 评论(0)  编辑  收藏 所属分类: 身份认证管理


只有注册用户登录后才能发表评论。


网站导航: