权限系统自定义标签

来源:互联网 发布:数控编程中各代码含义 编辑:程序博客网 时间:2024/06/11 06:41

在自己研发权限系统的时候因为自身原因不太喜欢使用现有的框架,但是在权限使用的时候发现会侵入过多的业务代码,这样系统耦合过多,所以自己模拟shiro进行权限系统判断。

这里做if else的标签代码


package com.tmall.lafite.web.admin.tag;import java.io.IOException;import java.util.Map;import javax.servlet.ServletContext;import javax.servlet.http.HttpServletRequest;import javax.servlet.jsp.JspException;import javax.servlet.jsp.tagext.BodyTagSupport;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.context.WebApplicationContext;import org.springframework.web.context.support.WebApplicationContextUtils;import com.alibaba.buc.sso.client.util.SimpleUserUtil;import com.alibaba.dubbo.config.annotation.Service;import com.alibaba.platform.buc.sso.common.dto.SimpleSSOUser;import com.tmall.lafite.core.manager.permit.PermitManager;import com.tmall.lafite.core.manager.permit.util.PermitSession;import com.tmall.lafite.core.manager.permit.util.PermitUtil;import com.tmall.lafite.dal.entity.permit.util.PermitResult;/** * 权限自定义标签 if 判断 * @author wangxiao * */@Servicepublic class IfPermit extends BodyTagSupport{protected final Logger logger = LoggerFactory.getLogger(getClass());private static final long serialVersionUID = -3923894643359138714L;private String check;@Autowiredprivate PermitManager permitManager;public String getCheck() {return check;}public void setCheck(String check) {this.check = check;}public IfPermit() {        super();        init();    }      @Override    public void release() {        super.release();        init();    }          @Override    public int doStartTag() throws JspException {    try {SimpleSSOUser user = SimpleUserUtil.findUser((HttpServletRequest) pageContext.getRequest());if(user == null) {test = false;} else {String opeator = user.getId().toString();Map<String, String> map = PermitUtil.parseCheckMsg(check);PermitSession algorithm = new PermitSession(map, opeator);PermitResult result = getPermitManager().hasPermit(algorithm);test = result.isAllow();}} catch (Exception e) {logger.error("IfPermit.doStartTag ", e);test = false;}            if(test){            this.succeeded();        }        return EVAL_BODY_BUFFERED;    }      @Override    public int doEndTag() throws JspException {        try {            if(subtagSucceeded)                pageContext.getOut().write(getBody());        } catch (IOException e) {            throw new JspException("IOError while writing the body: " + e.getMessage(), e);        }                  init();        return super.doEndTag();    }          private String body = null;     //用于存放成功条件后的内容    public void setBody(){        if(body == null){            body = bodyContent.getString().trim();        }    }          private String getBody(){        if(body == null)            return bodyContent.getString().trim();        else            return body;    }          /**     * 判断if 或者 子 else if是否提交成功     */    private boolean subtagSucceeded;          /**     * 子条件判断成功     */    public void succeeded(){        subtagSucceeded = true;    }    /**     * 是否已经执行完毕     * @return     */    public boolean isSucceeded(){        return subtagSucceeded;    }          private void init() {        test = false;        subtagSucceeded = false;        body = null;    }        private boolean test;            public void setTest(boolean test) {        this.test = test;    }public PermitManager getPermitManager() {ServletContext servletContext = pageContext.getServletContext();        WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);        permitManager = (PermitManager) wac.getBean("permitManager");return permitManager;}        }
else标签



package com.tmall.lafite.web.admin.tag;import java.io.IOException;import javax.servlet.jsp.JspException;import javax.servlet.jsp.JspTagException;import javax.servlet.jsp.tagext.BodyTagSupport;import javax.servlet.jsp.tagext.Tag;import org.apache.commons.lang.StringUtils;import com.alibaba.dubbo.config.annotation.Service;/** * 权限自定义标签 else 判断 * @author wangxiao * */@Servicepublic class ElsePermit extends BodyTagSupport{private static final long serialVersionUID = 2912994741944157118L;public void release() {        super.release();    }        public int doStartTag() throws JspException {        Tag parent = getParent();        if(parent==null || !(parent instanceof IfPermit)){            throw new JspTagException("else tag must inside if tag");        }                IfPermit IfPermit = (IfPermit)parent;        if(IfPermit.isSucceeded()){            // 已经有执行成功的条件,保存之前的html            IfPermit.setBody();        }else{            // 之前没有的判断没有成功条件,则清除之前的输出            IfPermit.getBodyContent().clearBody();            IfPermit.succeeded();            try {            String b = getBody();            if(!StringUtils.isEmpty(b)) {            pageContext.getOut().write(b);            }} catch (IOException e) { throw new JspException("IOError while writing the body: " + e.getMessage(), e);}        }                    return EVAL_BODY_BUFFERED;    }        private String body = null;        //    用于存放成功条件后的内容    public void setBody(){        if(body == null){            body = bodyContent.getString().trim();        }    }        private String getBody(){        if(body == null && bodyContent != null)            return bodyContent.getString().trim();        else            return body;    }    }

tld配置

<?xml version="1.0" encoding="UTF-8"?>  <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "web-jsptaglibrary_1_2.dtd" >  <taglib>    <tlibversion>tlibversion</tlibversion>  <!-- 你自定义标签的版本数 -->    <jsp-version>1.2</jsp-version>  <!-- 指定你的JSP版本,扩张标签是在JSP1.2之后兼容的 -->    <shortname>shortname</shortname> <!-- 标签的简写 -->    <tag>      <name>if</name><!-- 标签名字 -->      <tagclass>com.tmall.lafite.web.admin.tag.IfPermit</tagclass><!-- 指定你的标签的类 -->  <attribute> <description>description</description> <name>check</name> <required>true</required><rtexprvalue>true</rtexprvalue><type>java.lang.String</type></attribute>  </tag>  <tag>      <name>elseif</name><!-- 标签名字 -->      <tagclass>com.tmall.lafite.web.admin.tag.ElseIfPermit</tagclass><!-- 指定你的标签的类 -->  </tag>  <tag>      <name>else</name><!-- 标签名字 -->      <tagclass>com.tmall.lafite.web.admin.tag.ElsePermit</tagclass><!-- 指定你的标签的类 -->  </tag></taglib>
标签判断使用方式
<%@ taglib uri="http://admin.lafite.tmall.com/taglibs.tld" prefix="bit" %><bit:if check="p:10-11"><li class="submenu">            <a href="#"><i class="icon icon-inbox"></i><span> if </span></a>        </li><bit:else><li class="submenu">            <a href="#"><i class="icon icon-inbox"></i><span> else </span></a>        </li></bit:else>        </bit:if>

原创粉丝点击