(转载)Tapestry最新版5.1.0.5课程(八):权限控制框架的实现-基础篇
(转载)Tapestry最新版5.1.0.5教程(八):权限控制框架的实现-基础篇
Tapestry中并没有类似于Spring Security这样的专门的权限框架。对此Tapestry的作者Lewis认为主要是用户对于权限的要求实在太多变化了。他认为很难抽象出一个通用的权限框架来满足所有的用户,所以他干脆就不费事去做这件事了。但其实我们很容易就能利用Tapestry已有的工具来完成类似于 SpringSecurity的功能。
本文主要介绍如何实现类似于SpringSecurity的jsp tag的功能。在Tapestry中,利用Components实现这一点非常容易。
其基本原理是Tapestry5中一个页面或者组件的渲染生成过程是基于一个状态机和队列完成的。这样,渲染生成过程就被细分成了很多个小模块,我们可以非常容易地覆写这些小模块。具体内容详见官方文档:[url]http://tapestry.apache.org/tapestry5.1/guide /rendering.html[/url]。如果权限校验不通过,我们就可以控制不显示组件的内容。
我们这里就是主要依赖这个过程来实现在页面这一层面对权限进行校验和控制。
代码主要包含两大部分,一个组件和一个用于权限控制的服务。
参考了Tapestry-Spring-Security的实现,我也将组件命名为IfRole(当然,我们也可以和Tapestry-Spring- Security一样,也再生成一个IfLoggedIn组件)。权限控制的服务我命名为:AuthenticationService。
主要的实现思路:
将AuthenticationService申明为SessionState变量。这样这个变量就可以在所有的页面和组件之间很方便地共享了。一般情况下,是在登录页面对AuthenticationService进行赋值,而在退出页面清空AuthenticationService这个变量。
代码(这部分代码完全根据应用的需求进自行更改):
AuthenticationService的代码:
IfRole的代码(这个类需要放在Components目录下):
示例:
1. 在登录页面:
2. 在需要权限控制的页面模板中:
原文地址:http://www.blogjava.net/usherlight/archive/2010/01/12/309226.html
Tapestry中并没有类似于Spring Security这样的专门的权限框架。对此Tapestry的作者Lewis认为主要是用户对于权限的要求实在太多变化了。他认为很难抽象出一个通用的权限框架来满足所有的用户,所以他干脆就不费事去做这件事了。但其实我们很容易就能利用Tapestry已有的工具来完成类似于 SpringSecurity的功能。
本文主要介绍如何实现类似于SpringSecurity的jsp tag的功能。在Tapestry中,利用Components实现这一点非常容易。
其基本原理是Tapestry5中一个页面或者组件的渲染生成过程是基于一个状态机和队列完成的。这样,渲染生成过程就被细分成了很多个小模块,我们可以非常容易地覆写这些小模块。具体内容详见官方文档:[url]http://tapestry.apache.org/tapestry5.1/guide /rendering.html[/url]。如果权限校验不通过,我们就可以控制不显示组件的内容。
我们这里就是主要依赖这个过程来实现在页面这一层面对权限进行校验和控制。
代码主要包含两大部分,一个组件和一个用于权限控制的服务。
参考了Tapestry-Spring-Security的实现,我也将组件命名为IfRole(当然,我们也可以和Tapestry-Spring- Security一样,也再生成一个IfLoggedIn组件)。权限控制的服务我命名为:AuthenticationService。
主要的实现思路:
将AuthenticationService申明为SessionState变量。这样这个变量就可以在所有的页面和组件之间很方便地共享了。一般情况下,是在登录页面对AuthenticationService进行赋值,而在退出页面清空AuthenticationService这个变量。
代码(这部分代码完全根据应用的需求进自行更改):
AuthenticationService的代码:
public class AuthenticationService { private List<String> privilegeList; // privilegeList 的getter and setter public boolean checkPermission(String ifNotGranted, String ifAllGranted, String ifAnyGranted) { if (((null == ifAllGranted) || "".equals(ifAllGranted)) && ((null == ifAnyGranted) || "".equals(ifAnyGranted)) && ((null == ifNotGranted) || "".equals(ifNotGranted))) { return false; } if ((null != ifNotGranted) && !"".equals(ifNotGranted)) { StringTokenizer st = new StringTokenizer(ifNotGranted, ","); while (st.hasMoreTokens()) { String value = st.nextToken(); if (privilegeList.contains(value)) { return false; } } } if ((null != ifAllGranted) && !"".equals(ifAllGranted)) { StringTokenizer st = new StringTokenizer(ifAllGranted, ","); while (st.hasMoreTokens()) { String value = st.nextToken(); if (!privilegeList.contains(value)) { return false; } } } if ((null != ifAnyGranted) && !"".equals(ifAnyGranted)) { StringTokenizer st = new StringTokenizer(ifAnyGranted, ","); while (st.hasMoreTokens()) { String value = st.nextToken(); if (privilegeList.contains(value)) { return true; } } return false; } return true; } }
IfRole的代码(这个类需要放在Components目录下):
public class IfRole { /** *//** * A comma-separated list of roles is supplied to one or more of the * following parameters. If none are supplied, the default behavior is to * forbid access. Behavior should be self-explanatory. */ @Parameter(required = false, defaultPrefix = "literal") private String ifAllGranted; @Parameter(required = false, defaultPrefix = "literal") private String ifAnyGranted; @Parameter(required = false, defaultPrefix = "literal") private String ifNotGranted; /** *//** * An alternate {@link Block} to render if the test parameter is false. The default, null, means * render nothing in that situation. */ @Parameter(name = "else") private Block elseBlock; private boolean test; @SessionState private AuthenticationService auth; private boolean checkPermission() { return auth.checkPermission(ifNotGranted, ifAllGranted, ifAnyGranted); } void setupRender() { test = checkPermission(); } /** *//** * Returns null if the test method returns true, which allows normal * rendering (of the body). If the test parameter is false, returns the else * parameter (this may also be null). */ Object beginRender() { return test ? null : elseBlock; } /** *//** * If the test method returns true, then the body is rendered, otherwise not. The component does * not have a template or do any other rendering besides its body. */ boolean beforeRenderBody() { return test; } }
示例:
1. 在登录页面:
@SessionState private Authentication auth; ...... // if user name and password is valid: auth.setPrivliegeList(.....);
2. 在需要权限控制的页面模板中:
<t:ifRole ifAllGranted="admin"> administrator can see this block </t:ifRole>
原文地址:http://www.blogjava.net/usherlight/archive/2010/01/12/309226.html