您的位置:首页 > 编程语言 > Java开发

Java TagSupport实现Web权限验证标签

2014-03-03 17:08 351 查看
权限控制:

jsp

<%@ taglib prefix="priveliege" uri="/WEB-INF/tlds/priveliege.tld" %>

     <priveliege:priveliege funCode="switchSite">

       <input type="button" value='<s:text name="msg.portalMS.iepg.site.change"/>' id="button" onclick="showPageTask2('<s:text name="msg.portalMS.iepg.site.change"/>','<%=request.getContextPath()%>/Site/queryValidateSites.action',652,466,'<%=request.getContextPath()%>/Site/queryIepg.action');"/>

     </priveliege:priveliege>

     create table T_SYS_RIGHT

(

  right_id        NUMBER(13) not null,

  right_name      VARCHAR2(100) not null,

  right_url       VARCHAR2(2000),

  parent_right_id NUMBER(13),

  right_level     NUMBER(1),

  status          NUMBER(2) not null,

  function_code   VARCHAR2(50),

  level_type      VARCHAR2(2)

)

function_code = 'switchSite' 则有权限

priveliege.tld:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"

"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">

<taglib>

    <tlib-version>1.0</tlib-version>

    <jsp-version>1.1</jsp-version>

    <short-name>priveliegeTag</short-name>

    <uri>priveliegeTag</uri>

    <tag>

        <name>priveliege</name><!--另一个标签的别名-->

        <tag-class>

            com.xxx.sdp.rights.view.action.taglib.PrivilegeTag

        </tag-class><!--标签类文件的物理地址-->

        <body-content>Jsp</body-content><!--看看是不是具有标签体-->

        <attribute><!--带参数的标签必须写-->

            <name>funCode</name><!--属性名称-->

            <required>false</required><!--当前的属性是否是必须的-->

            <rtexprvalue>true</rtexprvalue><!-- 是否通过程序代码进行赋值-->

        </attribute>

    </tag>

</taglib>

package com.coship.dhm.portalMS.common.taglib;

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

import javax.servlet.jsp.JspException;

import javax.servlet.jsp.tagext.TagSupport;

import com.xxx.dhm.portalMS.common.Constants;

import com.xxx.sdp.rights.model.domain.Admin;

/**

 *

 * 权限标签类,通过引用此标签可以屏蔽不需要的功能. 1、在jsp页面中引入:<%@taglib uri="/WEB-INF/priveliege.tld"

 * prefix="priveliege" %> 权限标签的TLD文件 2、在需要配制权限控制的地方加入:<priveliege:priveliege

 * funCode="ModifyRegion">标签体</priveliege:priveliege>

 * 3、funCode的值需与数据库权限表的function_code字段值一致,才能显示标签体,否屏蔽标签体

 *

 */

public class PrivilegeTag extends TagSupport {

    /**

     *

     */

    private static final long serialVersionUID = 1L;

    private List<Privilege> privilegeList = new ArrayList<Privilege>();

    private String funCode;

    private Privilege privielege;

    public int doStartTag() throws JspException {

        // 从容器内存中读取用户的功能列表

        Admin loginUser = (Admin) this.pageContext.getSession().getAttribute(

                Constants.LOGIN_USER.getStringValue());

        /*

         * 不再使用session "assignedPrivileges"了 modify by

         * privilegeList = (List) this.pageContext.getSession().getAttribute(

         * "assignedPrivileges");

         */

        // 若session失效,则屏蔽功能

        if (null == privilegeList) {

            return TagSupport.SKIP_BODY;

        }

        privilegeList = PrivilegeURLCache.getPrivilegeList(loginUser

                .getLoginName());

        Iterator<Privilege> it = privilegeList.iterator();

        funCode = funCode.trim();

        if (null != funCode) {

            while (it.hasNext()) {

                privielege = it.next();

                // 若此用户存此功能code,界面显示操作按钮

                if ((privielege.getFunCode().equals(funCode))) {

                    return TagSupport.EVAL_PAGE;

                }

            }

        }

        return TagSupport.SKIP_BODY;

    }

    public int doEndTag() throws JspException {

        // TODO Auto-generated method stub

        return TagSupport.EVAL_PAGE;

    }

    public List<Privilege> getPrivilegeList() {

        return privilegeList;

    }

    public void setPrivilegeList(List<Privilege> privilegeList) {

        this.privilegeList = privilegeList;

    }

    public String getFunCode() {

        return funCode;

    }

    public void setFunCode(String funCode) {

        this.funCode = funCode;

    }

    public Privilege getPrivielege() {

        return privielege;

    }

    public void setPrivielege(Privilege privielege) {

        this.privielege = privielege;

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: