Tag标签的一些使用实例

Tag标签定义:(<zhishen:auth funCode=""></zhishen:auth>)
1、tld文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE taglib  
    PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"  
    "http://java.sun.com/dtd/web-jsptaglibrary_1_1.dtd">
<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.2</jspversion>
<shortname>zhishen</shortname><!-- 标签名-->
<uri></uri>
<tag>
<name>auth</name><!--tag名-->
<tagclass>com.etom.zhishen.common.SysAuthTag</tagclass>//tag标签代码路径
<info></info>
<attribute>
<name>funCode</name><!--传入的属性值-->
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>userPk</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>


2、SysAuthTag类(com.etom.zhishen.common.SysAuthTag):类似于中间桥梁


package com.etom.zhishen.common;


import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;


import org.nw.utils.NWUtils;
import org.nw.web.utils.SpringContextHolder;
import org.nw.web.utils.WebUtilsFactory;


import com.etom.zhishen.constants.ZhishenConstants;
import com.etom.zhishen.sys.service.SysAuthService;


public class SysAuthTag extends TagSupport {//继承TagSupport


private static final long serialVersionUID = 2452367091471043220L;


/**
* 要校验的权限编码,多个用逗号隔开
*/
private String funCode;


/**
* 要校验的用户主键
*/
private String userPk;


/**
* 系统权限服务类
*/
SysAuthService sysAuthService = (SysAuthService) SpringContextHolder.getBean("sysAuthService");//权限服务接口


@Override
public int doStartTag() throws JspException {
String pk_user = userPk;
if(NWUtils.isEmpty(pk_user)) {
pk_user = WebUtilsFactory.getInstance().getLoginInfo().getPk_user();
}
if(NWUtils.isEmpty(pk_user)) {
// 找不到用户主键,无权限
return SKIP_BODY;
}
if(ZhishenConstants.SYSTEM_ADMIN_ID.equals(pk_user) || ZhishenConstants.TOP_CORP_ID.equals(pk_user)
|| ZhishenConstants.ADMIN_ID.equals(pk_user) || NWUtils.isEmpty(funCode)) {
// 系统管理员不做控制,权限编码为空不控制
return EVAL_BODY_INCLUDE;
} else {
// 判断当前用户是否有指定功能的权限
String[] funCodes = funCode.split(",");
for(String eachfunCode : funCodes) {
if(sysAuthService.hasAuthByFunCode(eachfunCode, pk_user)) {
return EVAL_BODY_INCLUDE;
}
}
}


return SKIP_BODY;
}


public String getFunCode() {
return funCode;
}


public void setFunCode(String funCode) {
this.funCode = funCode;
}


public String getUserPk() {
return userPk;
}


public void setUserPk(String userPk) {
this.userPk = userPk;
}
}




3权限服务(service)类SysAuthService(com.etom.zhishen.sys.service.SysAuthService)全部业务放入此类处理
package com.etom.zhishen.sys.service;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;


import org.nw.dao.NWDao;
import org.nw.utils.NWUtils;
import org.springframework.stereotype.Service;


import com.etom.zhishen.constants.AuthConstants;
import com.etom.zhishen.constants.ZhishenConstants;
import com.etom.zhishen.sys.vo.FunVO;
import com.etom.zhishen.sys.vo.UserVO;


/**
 * 系统权限服务类
 * 
 * @author Administrator
 *
 */
@Service
public class SysAuthService {


/**
* 用户的权限对照,key是用户id,List<FunVO>是用户具有的所有权限
*/
private static ConcurrentHashMap<String, List<FunVO>> auth_map;


/**
* 获取用户权限对照

* @return key是用户id,List<FunVO>是用户具有的所有权限
*/
public static ConcurrentHashMap<String, List<FunVO>> getAuth_map() {
return auth_map;
}


/**
* 系统功能列表
*/
private static List<FunVO> fun_list;


/**
* 获取系统权限列表

* @return
*/
public List<FunVO> getSysFunList() {
if(fun_list == null) {
String sql = "select * from sys_fun where ifnull( jl_is_del , 0 ) = 0";
fun_list = NWDao.getInstance().queryForList(sql, FunVO.class);
if(NWUtils.isEmpty(fun_list))
fun_list = new ArrayList<>();
}


return fun_list;
}


/**
* 缓存用户权限信息

* @param user
*/
public void cacheAuth(UserVO user) {
// 1.获取用户的所有权限
List<FunVO> authList = this.getAuthList(user);


// 2.缓存用户的所有权限
if(auth_map == null) {
auth_map = new ConcurrentHashMap<String, List<FunVO>>();
}
if(NWUtils.isEmpty(authList)) {
authList = new ArrayList<FunVO>();
}
auth_map.put(user.getPk_user(), authList);
}


/**
* 判断用户是否有路径权限

* @param servletPath
*            访问路径
* @param pk_user
*            用户主键
* @return true-有权限;false-无权限
*/
public boolean hasAuth(String servletPath, String pk_user) {
// 1.系统超级管理员和管理员有全部权限,直接返回true
if(ZhishenConstants.TOP_CORP_ID.equals(pk_user) || ZhishenConstants.SYSTEM_ADMIN_ID.equals(pk_user)
|| ZhishenConstants.ADMIN_ID.equals(pk_user)) {
return true;
}
// 2.遍历用户权限集合,有权限直接返回true
List<FunVO> authList = new ArrayList<>();
if(!NWUtils.isEmpty(auth_map)) {
authList = auth_map.get(pk_user);
}
for(FunVO auth : authList) {
// 只判断子模块权限和功能权限
if(AuthConstants.FUN_TYPE.MODULE.equals(auth.getType())) {
continue;
}
String[] authPaths = auth.getPath().split(AuthConstants.FUN_PATH_SPLIT_STR);
for(String path : authPaths) {
if(servletPath.startsWith(path))
return true;
}
}


// 3.遍历系统权限集合,有找到则表示无权限,返回false
for(FunVO fun : getSysFunList()) {
if(AuthConstants.FUN_TYPE.MODULE.equals(fun.getType())) {
continue;
}
String[] paths = fun.getPath().split(AuthConstants.FUN_PATH_SPLIT_STR);
for(String path : paths) {
if(!NWUtils.isEmpty(path) && servletPath.startsWith(path))
return false;
}
}


// 4.未定义则默认有权限
return true;
}


/**
* 判断用户是否有指定功能权限

* @param funCode
*            功能编号
* @param pk_user
*            用户主键
* @return true-有权限;false-无权限
*/
public boolean hasAuthByFunCode(String funCode, String pk_user) {
// 1.系统超级管理员有全部权限,直接返回true
if(ZhishenConstants.TOP_CORP_ID.equals(pk_user) || ZhishenConstants.SYSTEM_ADMIN_ID.equals(pk_user)
|| ZhishenConstants.ADMIN_ID.equals(pk_user) || NWUtils.isEmpty(funCode)) {
return true;
}
// 2.遍历用户权限集合,有权限直接返回true
List<FunVO> authList = new ArrayList<FunVO>();
if(!NWUtils.isEmpty(auth_map)) {
authList = auth_map.get(pk_user);
}
for(FunVO auth : authList) {
if(funCode.equals(auth.getCode()))
return true;
}


// 3.遍历系统权限集合,有找到则表示无权限,返回false
for(FunVO fun : getSysFunList()) {
if(funCode.equals(fun.getCode()))
return false;
}


// 4.未定义则默认有权限
return true;
}


/**
* 根据系统权限编码获取权限VO

* @param funCode
* @return
*/
public FunVO getFunVOByCode(String funCode) {
if(NWUtils.isEmpty(funCode)) {
return null;
}


List<FunVO> sysFunList = getSysFunList();
FunVO funVO = null;
for(FunVO sysFun : sysFunList) {
if(funCode.equals(sysFun.getCode())) {
funVO = sysFun;
break;
}
}


return funVO;
}


/**
* 通过用户查询数据库获取用户的所有功能权限

* @param user
* @return
*/
private List<FunVO> getAuthList(UserVO user) {
List<FunVO> authList = new ArrayList<>();
List<String> roleIdList = new ArrayList<>();
// 1.获取用户角色id
String sql = "select pk_role from sys_role_assign where ifnull(jl_is_del,0) = 0 and ifnull(item_type,?) = ? and pk_item = ? ";
List<String> userRoleIdList = NWDao.getInstance().queryForList(sql, String.class,
AuthConstants.ROLE_ASSIGN_ITEM_TYPE.USER.toString(),
AuthConstants.ROLE_ASSIGN_ITEM_TYPE.USER.toString(), user.getPk_user());
roleIdList.addAll(userRoleIdList);


// 2.获取用户角色和部门角色所拥有的权限
if(!NWUtils.isEmpty(roleIdList)) {
sql = "select * from sys_fun where ifnull(jl_is_del,0) = 0 and pk_fun in "
+ "(select pk_fun from sys_role_fun where ifnull(jl_is_del,0) = 0 and type = ? and pk_role in "
+ NWUtils.buildConditionString(roleIdList.toArray(new String[0])) + ")";
authList = NWDao.getInstance().queryForList(sql, FunVO.class, AuthConstants.FUN_TYPE.AUTH.toString());
}


// 3.有权限的功能所有上级节点(模块/子模块)也拥有权限
// 要加到权限列表的上级节点,key: FunVO主键
HashMap<String, FunVO> funsEx = new HashMap<>();
for(FunVO funVO : authList) {
// 若父级节点已添加则跳过
String pk_parent = funVO.getPk_parent();
if(funsEx.containsKey(pk_parent)) {
continue;
}


// 添加父级节点
addParentFuns(funVO, funsEx);
}


authList.addAll(funsEx.values());


// 4.返回权限列表
return authList;
}


/**
* 添加功能的所有上级节点

* @param funVO
* @param funsEx
*            保存上级节点
*/
private void addParentFuns(FunVO funVO, HashMap<String, FunVO> funsEx) {
String pk_parent = funVO.getPk_parent();
if(NWUtils.isEmpty(pk_parent)) {
return;
}


for(FunVO sysFun : getSysFunList()) {
if(pk_parent.equals(sysFun.getPk_fun())) {
funsEx.put(sysFun.getPk_fun(), sysFun);
addParentFuns(sysFun, funsEx);
break;
}
}


}
}

猜你喜欢

转载自blog.csdn.net/u014450465/article/details/79914199
今日推荐