之前在公司做的权限管理系统,页面鉴权用了beetl的模板解析引擎,但是beetl不支持jsp,一直想找时间优化。经过相关百度出来的资料综合整理了一下,打算记录一下相关内容。
首先,在鉴权的jar包里面的resources目录,也就是放配置文件的目录,新增名称为META-INF的目录(一定要注意,配合下面pom.xml配置,让tld文件自动打包到jar包里面,方便调用),在里面新建自定义标签模板(aut-security.tld)如下:
<?xml version="1.0" encoding="UTF-8"?>
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>2.0</jsp-version>
<short-name>ast</short-name><!-- taglib的名称,也称为前缀。比如<c:out value=""/> 里的“c” -->
<!-- autSecurityTag -->
<uri>aut-security</uri><!-- 标签引入使用uri -->
<tag>
<name>resAuthCheck</name><!-- tag的名字。例如<c:out value=""/> 里的"out”,我们的类也取名为out-->
<tag-class>com.smy.aut.security.interceptor.SmyAutCheckTag</tag-class><!-- 标签处理类 -->
<attribute><!-- 标签传参设置 -->
<name>resId</name> <!-- 标签传参名 -->
<required>false</required><!-- 是否必填 -->
<rtexprvalue>true</rtexprvalue><!-- 是否支持恶劣表达式 -->
<type>java.lang.String</type><!-- 字段类型 -->
<description>例如:AUTA000001</description><!-- 描述 -->
</attribute>
</tag>
</taglib>
属性 | 描述 |
---|---|
name | 定义属性的名称。每个标签的是属性名称必须是唯一的。 |
required | 指定属性是否是必须的或者可选的,如果设置为false为可选。 |
rtexprvalue | 声明在运行表达式时,标签属性是否有效。 |
type | 定义该属性的Java类类型 。默认指定为 String |
description | 描述信息 |
fragment | 如果声明了该属性,属性值将被视为一个 JspFragment。 |
在 Eclipse 相应工程中右键单击目标目录弹出选项框,依次选:
->New->Other->XML->XML File->next->要取的文件名.tld
->next->Create XML File from an XML schema file
->next->Select XML Catalog entry
选择 http://xmlns.jcp.org/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd 这一项,点击 Finish 即可。
然后,在项目的pom.xml配置中加上这一段(build->plugins),目的是为了不让引用的项目单独引入tld文件。
<!-- 自定义文件放置在META-INF将META-INF放置main/resources目录 -->
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<classesDirectory>target/classes/</classesDirectory>
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
</archive>
</configuration>
</plugin>
其次,新增自己的标签处理类,代码如下:
package com.smy.aut.security.interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.servlet.jsp.tagext.TagSupport;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.smy.aut.security.constant.ConstantSecurity;
import com.smy.aut.security.util.SmyAutCheck;
/**
* 权限鉴权jsp标签支持
* @author guoyunfeng
*
*/
public class SmyAutCheckTag extends TagSupport {
private static Logger log = LoggerFactory.getLogger(SmyAutCheckTag.class);
/**
* 资源编号
*/
private String resId;
@Override
public int doStartTag() throws JspException {
log.info("jsp按钮鉴权开始 ... resId:" + resId );
boolean reStatus = false;
HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
String userId = SmyAutCheck.getCookiesValues(request, "userId");
String userType = SmyAutCheck.getCookiesValues(request, "userType");
String resType = "A";
// 资源编号不为空
if (StringUtils.isNotBlank(resId)) {
// 鉴权
if (SmyAutCheck.hasAuthority(userId, resId, resType)) {
log.info("权限验证通过");
reStatus = true;
} else {
log.info("权限验证不通过");
}
}
log.info("jsp按钮鉴权结果:" + reStatus);
if (reStatus) {
// 允许访问标签body
return BodyTagSupport.EVAL_BODY_INCLUDE;// 返回此则执行标签body中内容,SKIP_BODY则不执行
} else {
return BodyTagSupport.SKIP_BODY;
}
}
@Override
public int doEndTag() throws JspException {
return BodyTagSupport.EVAL_BODY_INCLUDE;
}
public String getResId() {
return resId;
}
public void setResId(String resId) {
this.resId = resId;
}
}
到这里基本就把验证这边写完了,如下在 调用即可:
<!-- jsp最上面引入 -->
<%@ taglib prefix="ast" uri="aut-security" %>
<!-- 需要鉴权的地方加上自定义标签 -->
<ast:resAuthCheck resId="resId" >
<button type="button" id="updateBtn" >修改状态</button>
</ast:resAuthCheck>
更深度的用法参考:http://blog.csdn.net/lyddite_luo/article/details/3344102
以上仅为个人百度整合,加自己实际情况的一下资料,仅供参考