常言笑的家

          Spring, Hibernate, Struts, Ajax, RoR

          使用Acegi時獲取用戶信息的幾個函數(shù)

           

           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   }

          67

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

          Feedback

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

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

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


          My Links

          Blog Stats

          常用鏈接

          留言簿(5)

          隨筆分類

          隨筆檔案

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 会泽县| 法库县| 广州市| 谢通门县| 页游| 类乌齐县| 宁城县| 武宁县| 屏边| 莆田市| 卓资县| 肃宁县| 沙洋县| 甘德县| 元江| 阿瓦提县| 临猗县| 连云港市| 鄢陵县| 齐齐哈尔市| 伊川县| 莱州市| 上栗县| 新河县| 读书| 华坪县| 东莞市| 永丰县| 聂拉木县| 郑州市| 高清| 巨野县| 四川省| 营口市| 定远县| 贵定县| SHOW| 大田县| 宝山区| 临武县| 济南市|