(转载)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体系中的重要性。

[size=medium][color=blue]原文地址:[url]http://www.blogjava.net/usherlight/archive/2010/02/04/312016.html[/url][/color][/size]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值