`
guofengcn
  • 浏览: 50728 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

(转载)Tapestry最新版5.1.0.5教程(九):权限控制框架的实现-进阶篇

阅读更多
    在上一篇中我们研究了如何实现SpringSecurity中Jsp Tag的<security:authorize ifAllGranted="ROLE_SUPERVISOR">的功能。这一次我们一起研究一下如何实现在Tapestry5.1中添加一个 Filter来对所有的操作进行权限的过滤控制。
    在SpringSecurity中,我们一般是在application-context.xml中,添加一个SpringSecurity的 Filter,然后在另外一个xml中详细配置如何根据Url的规则进行权限的控制。而Tapestry的哲学是尽量减少Xml中的配置(其IOC容器也基本上是借鉴Guice而不Spring的),所以我们也是在代码中实现权限规则的控制。
    总体上来看,可以用两种方式来实现url规则,一种是Request级别的Filter,一种是页面组件级别的Filter,如果是Request级别的话,可以从Request对象中获取Url路径,这样就与SpringSecurity基本一样了。本文主要介绍页面组件级别的Filter,从中我们也可以体会到Tapestry5.1中的IOC容器的强大和便利。

   下面就是Filter的代码,这个Filter必须实现ComponentRequestFilter接口。值得注意的是其构造函数所需要用到的4个参数,这4个参数都是Tapestry5本身自有的服务,所以我们什么也不用做,Tapestry5自动会将服务的实例注入进来,这就是 Tapestry-IOC的威力。
ComponentRequestFilter接口一共有4个方法需要实现,具体代码如下:
public class RequiresLoginFilter implements ComponentRequestFilter {
    private final PageRenderLinkSource renderLinkSource;
    
    private final ComponentSource componentSource;
    
    private final Response response;
  
    private final ApplicationStateManager appStateManager;
    
    public RequiresLoginFilter(PageRenderLinkSource renderLinkSource,
           ComponentSource componentSource, Response response,
             ApplicationStateManager appStateManager) {
         this.renderLinkSource = renderLinkSource;
         this.componentSource = componentSource;
         this.response = response;
         this.appStateManager = appStateManager;
    }
 
    public void handleComponentEvent(
           ComponentEventRequestParameters parameters,
             ComponentRequestHandler handler) throws IOException {
 
       if (dispatchedToLoginPage(parameters.getActivePageName())) {
             return;
       }
 
       handler.handleComponentEvent(parameters);
 
    }
 
    public void handlePageRender(PageRenderRequestParameters parameters, ComponentRequestHandler handler) throws IOException {
        if (dispatchedToLoginPage(parameters.getLogicalPageName())) {
             return;
        }
        handler.handlePageRender(parameters);
 
    }
 
    private boolean dispatchedToLoginPage(String pageName) {
         Component page = componentSource.getPage(pageName);
        
         if (page.getClass().isAnnotationPresen(RequiresLogin.class)){
             if ( ! appStateManager.exists(Authentication.class)) {
                 redirect();
                 return true;
             }
             Authentication auth = appStateManager.get(Authentication.class);
             if ( auth == null ) {
                 redirect();
                 return true;
             }
            
             if ( ! auth.isLoggedIn()) {
                 redirect();
                 return true;
             }
 
             RequiresLogin requireLogin = page.getClass().getAnnotation(RequiresLogin.class);
             String ifNotGranted = requireLogin.ifNotGranted();
             String ifAllGranted = requireLogin.ifAllGranted();
             String ifAnyGranted = requireLogin.ifAnyGranted();
             boolean permitted = auth.checkPermission(ifNotGranted,ifAllGranted, ifAnyGranted);
             if ( ! permitted ) {
                 return true;
             }
         }
 
        return false;
    }
    
    private void redirect() {
         Link link = renderLinkSource.createPageRenderLink("Logout");
        
         try {
             response.sendRedirect(link);
         } catch (Exception e) {

         }
    }
}


   在ComponentRequestFilter中,我们无法使用@SessionState注解来直接注入Session中的变量,但是我们可以通过ApplicationStateManager来取得。

   现在我们需要把刚定义的Filter注册到系统中,很简单,只要在AppModule中添加以下函数就行了:
public static void contributeComponentRequestHandler(
   OrderedConfiguration<ComponentRequestFilter> configuration) {
     configuration.addInstance("RequiresLogin",RequiresLoginFilter.class);
}


  从本例子中我们可以看到Tapesty Ioc容器使用的便利性,也认识到了Ioc容器在Tapestry体系中的重要性。

原文地址:http://www.blogjava.net/usherlight/archive/2010/02/04/312016.html
分享到:
评论

相关推荐

    tapestry-bin-5.1.0.5

    【描述】"tapestry-bin-5.1.0.5" 描述了该版本是Tapestry框架的5.1.0.5版,这通常意味着它包含了该框架在那个时间点的所有功能和修复的错误。这个版本可能比之前的版本有所改进,包括性能优化、新功能添加以及对已有...

    tapestry5.1.0.5中文实例教程

    tapestry5.1.0.5中文实例教程,对于目前国内tapestry学习资源紧缺的情况,可谓填补了中文学习最大的空白,对于想学习tapestry5框架的人来说可谓字字如金,内容详尽,由浅入深,pdf格式

    tapestry 5.1.0.5 官方组件文档 天涯浪子

    来自:http://tapestry.apache.org/tapestry5.1/tapestry-core/ref

    tapestry5.1.0.5 官方api doc 文档 chm版 天涯浪子

    在官方下载的最新的tapestry5的api文档。。。。。。

    tapestry-src-5.1.0.5.zip

    包含: tapestry 的源代码, tapestry集成spring2.0 tapestry快速启动 tapestry upload tapestry hibernate tapestry annotations

    Tapestry5.0.16_API文档

    Tapestry5.0.16文档和大家一起学习

    tapestry教程资料文档合集

    Tapestry5最新中文教程.doc 作者 Renat Zubairov & Igor Drobiazko译者 沙晓兰 发布于 2008年7月2日 下午9时30分 社区 Java 主题 Web框架 ----------------------------------------- Tapestry5.1实例教程.pdf ...

    tapestry5以上的帮助事例,帮助文档与spring衔接文档

    Tapestry是一个基于控件的框架以致于用它开发Web应用类似开发传统的GUI应用。你用Tapestry开发Web应用时你无需关注以操作为中心的(Operation-centric) Servlet API.引用Tapestry网站上的一句话:"Tapestry用对象...

    tapestry5.1实例教程.docx

    《Tapestry5.1实例教程》是一份深入讲解Java Web开发框架Tapestry5.1的实践指导文档。Tapestry5.1是Apache软件基金会的一个开源项目,它为开发者提供了一种高度模块化、组件化的Web应用程序开发方式,强调了代码的...

    apache-tapestry-5.3.8-bin.zip

    2. **Tapestry Core**:`tapestry-core-5.3.8.jar`是Tapestry框架的核心库,包含了框架的基础组件、服务和API。它提供了页面、组件、事件处理、URL映射等功能,是构建Tapestry应用的基础。 3. **Plastic**:`...

    Tapestry5, Packt.Publishing.Tapestry.5.Building.Web.Applications.Jan.2008.RETAiL.eBOOk-sUppLeX.pdf

    5. **安全性与权限控制**:为了确保Web应用的安全性,Tapestry5内置了一套完善的安全机制,包括用户认证、授权以及细粒度的权限控制。这些功能可以帮助开发者构建安全可靠的Web应用。 #### 五、学习资源与实践建议 ...

    史上最全面Tapestry学习教程

    - 访问 [http://tapestry.apache.org/](http://tapestry.apache.org/) 下载 tapestry-bin-5.1.0.5.zip。 - 备份以备后续使用。 ##### 1.5 新建 Web 工程 - **步骤**: - 打开 Eclipse,选择 File &gt; New &gt; ...

    Tapestry5.1实例教程

    【Tapestry 5.1 实例教程】 Tapestry 5.1 是一个基于Java的Web应用程序框架,由Apache软件基金会开发。它提供了一种高效、声明式的方法来构建动态、高性能的Web应用,强调组件化和强类型的安全性。Tapestry 5.1在...

    Tapestry 5.1 实例教程(全部)

    **Tapestry 5.1 实例教程全览** Tapestry 5.1 是一个强大的Java Web应用程序框架,由Apache软件基金会维护。这个框架以其组件化、类型安全的URL映射、内置的依赖注入以及丰富的功能而备受赞誉。本教程全面地涵盖了...

    tapestry4和5学习资料

    Tapestry是一个强大的Java Web应用程序框架,由Apache软件基金会维护。它主要专注于提供组件化、模块化的开发方式,以及高度的可维护性和可扩展性。本压缩包包含了一系列的学习资源,帮助开发者深入理解和掌握...

Global site tag (gtag.js) - Google Analytics