<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    云自無心水自閑

    天平山上白云泉,云自無心水自閑。何必奔沖山下去,更添波浪向人間!
    posts - 288, comments - 524, trackbacks - 0, articles - 6
      BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

    在上一篇中我們研究了如何實現(xiàn)SpringSecurity中Jsp Tag的<security:authorize ifAllGranted="ROLE_SUPERVISOR">的功能。這一次我們一起研究一下如何實現(xiàn)在Tapestry5.1中添加一個Filter來對所有的操作進行權(quán)限的過濾控制。
    在SpringSecurity中,我們一般是在application-context.xml中,添加一個SpringSecurity的Filter,然后在另外一個xml中詳細配置如何根據(jù)Url的規(guī)則進行權(quán)限的控制。而Tapestry的哲學是盡量減少Xml中的配置(其IOC容器也基本上是借鑒Guice而不Spring的),所以我們也是在代碼中實現(xiàn)權(quán)限規(guī)則的控制。
    總體上來看,可以用兩種方式來實現(xiàn)url規(guī)則,一種是Request級別的Filter,一種是頁面組件級別的Filter,如果是Request級別的話,可以從Request對象中獲取Url路徑,這樣就與SpringSecurity基本一樣了。本文主要介紹頁面組件級別的Filter,從中我們也可以體會到Tapestry5.1中的IOC容器的強大和便利。

    這就是Filter的代碼,這個Filter必須實現(xiàn)ComponentRequestFilter接口。值得注意的是其構(gòu)造函數(shù)所需要用到的4個參數(shù),這4個參數(shù)都是Tapestry5本身自有的服務,所以我們什么也不用做,Tapestry5自動會將服務的實例注入進來,這就是Tapestry-IOC的威力。
    ComponentRequestFilter接口一共有4個方法需要實現(xiàn),具體代碼如下:

     1 public class RequiresLoginFilter implements ComponentRequestFilter {
     2 
     3     private final PageRenderLinkSource renderLinkSource;
     4 
     5     private final ComponentSource componentSource;
     6 
     7     private final Response response;
     8    
     9     private final ApplicationStateManager appStateManager;
    10    
    11     public RequiresLoginFilter(PageRenderLinkSource renderLinkSource,
    12             ComponentSource componentSource, Response response,
    13             ApplicationStateManager appStateManager
    14             ) {
    15         this.renderLinkSource = renderLinkSource;
    16         this.componentSource = componentSource;
    17         this.response = response;
    18         this.appStateManager = appStateManager;
    19     }
    20 
    21     public void handleComponentEvent(
    22             ComponentEventRequestParameters parameters,
    23             ComponentRequestHandler handler) throws IOException {
    24 
    25         if (dispatchedToLoginPage(parameters.getActivePageName())) {
    26             return;
    27         }
    28 
    29         handler.handleComponentEvent(parameters);
    30 
    31     }
    32 
    33     public void handlePageRender(PageRenderRequestParameters parameters,
    34             ComponentRequestHandler handler) throws IOException {
    35         if (dispatchedToLoginPage(parameters.getLogicalPageName())) {
    36             return;
    37         }
    38         handler.handlePageRender(parameters);
    39 
    40     }
    41 
    42     private boolean dispatchedToLoginPage(String pageName) {
    43         Component page = componentSource.getPage(pageName);
    44        
    45         if (page.getClass().isAnnotationPresent(RequiresLogin.class)) {
    46             if ( ! appStateManager.exists(Authentication.class)) {
    47                 redirect();
    48                 return true;
    49             }
    50             Authentication auth = appStateManager.get(Authentication.class);
    51             if ( auth == null ) {
    52                 redirect();
    53                 return true;
    54             }
    55            
    56              if ( ! auth.isLoggedIn()) {
    57                  redirect();
    58                  return true;
    59              }
    60 
    61             RequiresLogin requireLogin = page.getClass().getAnnotation(
    62                     RequiresLogin.class);
    63             String ifNotGranted = requireLogin.ifNotGranted();
    64             String ifAllGranted = requireLogin.ifAllGranted();
    65             String ifAnyGranted = requireLogin.ifAnyGranted();
    66             boolean permitted = auth.checkPermission(ifNotGranted, ifAllGranted, ifAnyGranted);
    67             if ( ! permitted ) {
    68                 return true;
    69             }
    70         }
    71 
    72         return false;
    73     }
    74    
    75     private void redirect() {
    76          Link link = renderLinkSource.createPageRenderLink("Logout");
    77        
    78          try {
    79              response.sendRedirect(link);
    80          } catch (Exception e) {
    81          }
    82     }
    83 
    84 }

    在ComponentRequestFilter中,我們無法使用@SessionState注解來直接注入Session中的變量,但是我們可以通過ApplicationStateManager來取得。

    現(xiàn)在我們需要把剛定義的Filter注冊到系統(tǒng)中,很簡單,只要在AppModule中添加以下函數(shù)就行了:

    1 public static void contributeComponentRequestHandler(
    2                 OrderedConfiguration<ComponentRequestFilter> configuration) {
    3         configuration.addInstance("RequiresLogin", RequiresLoginFilter.class);
    4 }
    5 
    從本例子中我們可以看到Tapesty Ioc容器使用的便利性,也認識到了Ioc容器在Tapestry體系中的重要性


    評論

    # re: Tapestry最新版5.1.0.5教程(九):權(quán)限控制框架的實現(xiàn)-進階篇  回復  更多評論   

    2010-12-25 23:22 by linuxboy
    這個方法我正在使用,但是有一個不是很完美的地方:用戶登錄后不能返回上一個頁面,尤其是包含context參數(shù)的頁面或者是request parameters的頁面。
    比如:http://none.com/edituser/12
    http://none.com/edituser?userId=12
    主站蜘蛛池模板: 免费永久看黄在线观看app| 亚洲国产精品lv| 国产日韩一区二区三免费高清| 亚洲国产精品无码中文字| 四虎精品视频在线永久免费观看| 亚洲爆乳无码精品AAA片蜜桃| 亚洲综合精品香蕉久久网| 亚洲高清中文字幕免费| eeuss在线兵区免费观看| 亚洲精品资源在线| 亚洲天堂在线视频| 国拍在线精品视频免费观看| 特级毛片aaaa级毛片免费| 亚洲黄色免费网址| 亚洲欧洲日本在线| 无码免费午夜福利片在线| 天黑黑影院在线观看视频高清免费 | 吃奶摸下高潮60分钟免费视频| 97人妻精品全国免费视频| 亚洲精品乱码久久久久久蜜桃图片 | 9久热精品免费观看视频| 亚洲一区二区无码偷拍| 亚洲va中文字幕无码久久| 波多野结衣一区二区免费视频| 91成人免费观看| 皇色在线免费视频| 亚洲AⅤ男人的天堂在线观看 | 亚洲熟女一区二区三区| 免费无码看av的网站| 亚洲视频免费在线播放| 久久99精品免费一区二区| 亚洲国产午夜精品理论片在线播放| 亚洲国产精品不卡在线电影| 国产亚洲精品无码拍拍拍色欲| 日韩伦理片电影在线免费观看| ww在线观视频免费观看| 黄色视屏在线免费播放| 色吊丝免费观看网站| 亚洲精品无码av中文字幕| 亚洲免费网站在线观看| 亚洲精品免费视频|