`
Mybeautiful
  • 浏览: 297115 次
  • 性别: 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的威力。

 

增加一个annotation, 如下:

 

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequiresLogin {

}

 


ComponentRequestFilter接口一共有4个方法需要实现,具体代码如下:

 

 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来取得。

现在我们需要把刚定义的Filter注册到系统中,很简单,只要在AppModule中添加以下函数就行了:

 

1 public static void contributeComponentRequestHandler(
2                 OrderedConfiguration<ComponentRequestFilter> configuration) {
3         configuration.addInstance("RequiresLogin", RequiresLoginFilter.class);
4 }
5 

从本例子中我们可以看到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在...

    tapestry4和5学习资料

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

    Tapestry 5.4.1 相关jar文件

    3. **tapestry-webresources.jar**:处理静态资源如CSS、JavaScript和图片,支持压缩、合并和版本控制,优化Web应用的性能。 4. **tapestry-freemarker.jar**:集成FreeMarker模板引擎,用于生成HTML页面。 5. **...

Global site tag (gtag.js) - Google Analytics