Tapestry 最新版5.1.0.5教程(八):权限控制框架的实现-基础篇

 

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的代码:

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>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值