隨筆 - 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 閱讀(369) 評論(0)  編輯  收藏 所屬分類: 身份認證管理


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 彩票| 新干县| 麻栗坡县| 乌拉特前旗| 郴州市| 遂川县| 同心县| 潜山县| 林州市| 精河县| 清水河县| 宁武县| 彰武县| 嘉善县| 岳西县| 磐安县| 隆化县| 通州区| 房产| 仁寿县| 永丰县| 北宁市| 若尔盖县| 临高县| 横峰县| 石台县| 云林县| 微山县| 临洮县| 天长市| 临桂县| 丰台区| 瑞昌市| 团风县| 静海县| 太谷县| 公主岭市| 鸡东县| 铅山县| 南城县| 海宁市|