Tapestry 最新版5.1.0.5教程(八):权限控制框架的实现-基础篇
来源:互联网 发布:ajax get传json 编辑:程序博客网 时间:2024/04/28 22:42
Tapestry中并没有类似于Spring Security这样的专门的权限框架。对此Tapestry的作者Lewis认为主要是用户对于权限的要求实在太多变化了。他认为很难抽象出一个通用的 权限框架来满足所有的用户,所以他干脆就不费事去做这件事了。但其实我们很容易就能利用Tapestry已有的工具来完成类似于 SpringSecurity的功能。
本文主要介绍如何实现类似于SpringSecurity的jsp tag的功能。在Tapestry中,利用Components实现这一点非常容易。
其基本原理是Tapestry5中一个页面或者组件的渲染生成过程是基于一个状态机和队列完成的。这样,渲染生成过程就被细分成了很多个小模块,我们可以 非常容易地覆写这些小模块。具体内容详见官方文档:http://tapestry.apache.org/tapestry5.1/guide /rendering.html。如果权限校验不通过,我们就可以控制不显示组件的内容。
我们这里就是主要依赖这个过程来实现在页面这一层面对权限进行校验和控制。
代码主要包含两大部分,一个组件和一个用于权限控制的服务。
参考了Tapestry-Spring-Security的实现,我也将组件命名为IfRole(当然,我们也可以和Tapestry-Spring- Security一样,也再生成一个IfLoggedIn组件)。权限控制的服务我命名为:AuthenticationService。
主要的实现思路:
将AuthenticationService申明为SessionState变量。这样这个变量就可以在所有的页面和组件之间很方便地共享了。一般情况 下,是在登录页面对AuthenticationService进行赋值,而在退出页面清空AuthenticationService这个变量。
代码(这部分代码完全根据应用的需求进自行更改):
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目录下):
/** *//**
* 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>
- Tapestry 最新版5.1.0.5教程(八):权限控制框架的实现-基础篇
- Tapestry最新版5.1.0.5教程(八):权限控制框架的实现-基础篇
- Tapestry 最新版5.1.0.5教程(九):权限控制框架的实现-进阶篇
- Tapestry最新版5.1.0.5教程(九):权限控制框架的实现-进阶篇
- Tapestry 最新版5.1.0.5教程(一)
- Tapestry最新版5.1.0.5教程(二)
- Tapestry 最新版5.1.0.5教程(三)
- Tapestry 最新版5.1.0.5教程(四)
- Tapestry最新版5.1.0.5教程(一)
- Tapestry最新版5.1.0.5教程(二)
- Tapestry最新版5.1.0.5教程(三)
- Tapestry最新版5.1.0.5教程(四)
- Tapestry 最新版5.1.0.5教程(二)续
- Tapestry 最新版5.1.0.5教程(十一):自定义组件CheckBoxGroup
- Tapestry 最新版5.1.0.5教程(十二):自定义前缀prefix
- Tapestry最新版5.1.0.5教程(二)续
- Tapestry 最新版5.1.0.5教程(十一):自定义组件CheckBoxGroup
- Tapestry 最新版5.1.0.5教程(十二):自定义前缀prefix
- 关于软件质量的思考 - Introduction
- linq 获取数据库时间 或者执行其他带返回值的sql查询
- c++zuoye
- 我再copy回来。中海真是有心人。只是,你们在哪里?
- 接口2
- Tapestry 最新版5.1.0.5教程(八):权限控制框架的实现-基础篇
- 真的有很好的感觉
- 数据库设计札记 - 存储过程
- Tapestry 最新版5.1.0.5教程(九):权限控制框架的实现-进阶篇
- python分析apache日志的脚本
- Java学习 从入门到精通
- Windows Mobile 上常见的 DirectShow 链接错误(转载)
- 为什么选择GIT--GIT与其他SCM工具的比较
- 让网马原代码隐藏