常言笑的家

          Spring, Hibernate, Struts, Ajax, RoR

          使用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?。椋?br>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   }

          67

          posted on 2006-12-16 22:46 常言笑 閱讀(351) 評論(1)  編輯  收藏 所屬分類: JAVA/J2EE

          Feedback

          # re: 使用Acegi時獲取用戶信息的幾個函數 2006-12-18 09:41 一餐三碗[匿名]

          老大,引用的我文章好歹要說一聲吧?

          http://www.aygfsteel.com/youlq/archive/2006/03/03/33487.html  回復  更多評論   


          My Links

          Blog Stats

          常用鏈接

          留言簿(5)

          隨筆分類

          隨筆檔案

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 抚顺市| 马山县| 崇仁县| 永善县| 岑溪市| 外汇| 山东省| 资中县| 西乌| 金坛市| 卢氏县| 县级市| 邳州市| 靖宇县| 阆中市| 南岸区| 济源市| 高唐县| 扎兰屯市| 双鸭山市| 大丰市| 嘉峪关市| 新营市| 铜川市| 义马市| 怀柔区| 陇川县| 溧阳市| 昌吉市| 双牌县| 安新县| 会理县| 星座| 宣恩县| 岐山县| 肥乡县| 泗水县| 南乐县| 枣阳市| 咸丰县| 崇阳县|