码迷,mamicode.com
首页 > Web开发 > 详细

基于jsp自定义标签,实现用户鉴权

时间:2017-09-26 16:10:39      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:1.0   get   dota   contain   oca   type   slist   auth   turn   

共分为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>

 

基于jsp自定义标签,实现用户鉴权

标签:1.0   get   dota   contain   oca   type   slist   auth   turn   

原文地址:http://www.cnblogs.com/JosephWang/p/7596789.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!