常言笑的家

          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 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 常言笑 閱讀(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)

          隨筆分類

          隨筆檔案

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 舒兰市| 馆陶县| 湖北省| 金坛市| 隆子县| 日照市| 山西省| 西安市| 无锡市| 湘潭县| 龙里县| 云林县| 寻甸| 隆化县| 漳平市| 赣州市| 临朐县| 泊头市| 卓尼县| 小金县| 城口县| 齐河县| 齐齐哈尔市| 安阳县| 淮南市| 尉犁县| 庆云县| 唐山市| 中江县| 曲阳县| 大悟县| 玛纳斯县| 会东县| 辽中县| 仪征市| 东莞市| 松潘县| 南郑县| 平利县| 拉孜县| 永靖县|