共分为4大部分:
1、自定义标签PermissionTagHandler应用场景
2、PermissionTagHandler类的实现
3、配置PermissionTagHandler
4、使用PermissionTagHandler
1、自定义标签PermissionTagHandler应用场景
用户列表userList.do需要,查看权限UserList
2、PermissionTagHandler类的实现
import com.cicc.admin.AdminConstant;
import com.cicc.enums.Permission;
import java.io.IOException;
import java.util.Arrays;
import java.util.Set;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.jsp.JspContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;
/**
*
* @author superxg
*/
public class PermissionTagHandler extends SimpleTagSupport {
private String permissions;
@Override
public void doTag() throws JspException, IOException {
Set<Permission> granted = getGrantedPermissions();
if (permissions != null && granted != null) {
List<Permission> permissionList = Arrays.asList(permissions.split(",")).stream().map(x -> Permission.valueOf(x)).collect(Collectors.toList());
if (permissionList.stream().anyMatch(x -> granted.contains(x))) {
getJspBody().invoke(null);
}
}
}
public void setPermissions(String permissions) {
this.permissions = permissions;
}
/**
* 获得当前登录员工的权限
*
* @return
*/
private Set<Permission> getGrantedPermissions() {
EmployeeBeanSession session = getSession();
if (session != null) {
return session.getPermissions();
} else {
return null;
}
}
/**
* 获取当前Session
*
* @return
*/
private EmployeeBeanSession getSession() {
JspContext context = getJspContext();
Object session = context.getAttribute(AdminConstant.SESSION_KEY, PageContext.SESSION_SCOPE);
if (session != null && session instanceof EmployeeBeanSession) {
return (EmployeeBeanSession) session;
}
return null;
}
}
3、配置PermissionTagHandler
<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.1" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd">
<tlib-version>1.0</tlib-version>
<short-name>gg</short-name>
<uri>/WEB-INF/tld/platform</uri>
<tag>
<name>permission</name>
<tag-class>com.cicc.admin.web.PermissionTagHandler</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>permissions</name>
<required>false</required>
<type>java.lang.String</type>
</attribute>
</tag>
</taglib>
4、使用PermissionTagHandler
<gg:permission permissions="USER_LIST"> <li id="li_users_list" <c:if test="${modelType=='userList'}">class="active"</c:if> > <a href="user/list.do"><span class="glyphicons glyphicons-group"></span> 用户列表 </a> </li> </gg:permission>