自定义权限模块2——自定义tld标签

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xyc_csdn/article/details/77141320

本篇博客接着上篇博客自定义权限模块1——SpringBoot支持JSP,并在上篇博客项目的基础上进行改造。

  • 目录结构

这里写图片描述

  • LoginUserInfo.java
package com.xyc.security.bo;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * 登录用户信息
 * Created by xyc on 2017/8/13 0013.
 */
public class LoginUserInfo implements Serializable {
    private static final long serialVersionUID = 4925564717462126047L;
    /**
     * 登录用户的权限
     */
    private Map<String, List<String>> mpInfoMap;

    public LoginUserInfo(Map<String, List<String>> mpInfoMap) {
        this.mpInfoMap = mpInfoMap;
    }

    public Map<String, List<String>> getMpInfoMap() {
        return mpInfoMap;
    }

    public void setMpInfoMap(Map<String, List<String>> mpInfoMap) {
        this.mpInfoMap = mpInfoMap;
    }
}
  • Constant.java
package com.xyc.security.common;

/**
 * 常量类
 * Created by xyc on 2017/8/13 0013.
 */
public class Constant {
    public static final String LOGIN_USER_INFO = "LOGIN_USER_INFO"; //登录用户信息
}
  • UserController.java
package com.xyc.security.controller;

import com.xyc.security.bo.LoginUserInfo;
import com.xyc.security.common.Constant;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * Created by xyc on 2017/8/13 0013.
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @GetMapping("/login")
    public String login(String module, String permission, HttpSession session) {
        if (module != null && !module.isEmpty() && permission != null && !permission.isEmpty()) {
            session.setAttribute(Constant.LOGIN_USER_INFO, new LoginUserInfo(new HashMap<String, List<String>>() {    //将权限放入session中
                {
                    put(module, Arrays.asList(permission));
                }
            }));
        }
        return "index";
    }
}
  • SecurityTag.java
package com.xyc.security.tag;

import com.xyc.security.bo.LoginUserInfo;
import com.xyc.security.common.Constant;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 开发jsp系统时,我们经常会用到tag来写java的逻辑代码,一般会继承两个类,一个是SimpleTagSupport,另一个是TagSupport,由于TagSupport书写配置比较复杂,
 * 一般采用的继承SimpleTagSupport的时候比较多。
 * Created by xyc on 2017/8/13 0013.
 */
public class SecurityTag extends SimpleTagSupport {
    /**
     * 权限模块值
     */
    private String module;
    /**
     * 权限值
     */
    private String permission;

    public String getModule() {
        return module;
    }

    public void setModule(String module) {
        this.module = module;
    }

    public String getPermission() {
        return permission;
    }

    public void setPermission(String permission) {
        this.permission = permission;
    }

    /**
     * 注意:
     * 在TagSupport当中有pageContext属性就和好获取session,request对象,但是SimpleTagSupport 当中只有jspContext 我们就将jspContext对象转换为pageContext对象,然后就可以调用session,request对象,
     * 比如:
     * HttpSession session=((PageContext)this.getJspContext()).getSession();
     * HttpServletRequest request=(HttpServletRequest) ((PageContext)this.getJspContext()).getRequest();
     *
     * @throws JspException
     * @throws IOException
     */
    @Override
    public void doTag() throws JspException, IOException {
        /**
         * getJspContext()返回的jspContext是PageContext的一个子类,里面包含了获取各个作用域的属性的方法
         * 1.getJspContext().getAttribute(name, scope)  两个参数的scope 可以取值page ,request ,session ,context
         * 2.getJspContext().findAttribute(name) 这个方法会按page -->request -->session -- >context 去查找,如果查询到对应Name的值,立即返回
         * 3.getJspContext().getAttribute(name) 默认只会在page 作用域去查找属性
         * getJspContext() 本身就是一个PageContext,如果你是获取request,session,serlvetContext .这些对象执行方法的时候那么你需要把这个对象强制转型为pageContext..比如使用PageContext context = (PageContext) getJspContext();
         */
        LoginUserInfo loginUserInfo = (LoginUserInfo) this.getJspContext().findAttribute(Constant.LOGIN_USER_INFO);
        if (loginUserInfo != null) {
            if (this.verifySecurity(loginUserInfo.getMpInfoMap())) {
                this.getJspBody().invoke(null); //默认输出标签体
            }
        }
    }

    private boolean verifySecurity(Map<String, List<String>> mpInfoMap) {
        if (mpInfoMap == null || mpInfoMap.isEmpty() || this.getModule() == null || this.getModule().isEmpty() || this.getPermission() == null || this.getPermission().isEmpty()) {
            return false;
        }
        List<String> pInfoList = mpInfoMap.get(this.getModule());
        if (pInfoList == null || pInfoList.isEmpty()) {
            return false;
        }
        if (pInfoList.contains(this.getPermission())) {
            return true;
        }
        return false;
    }
}
  • index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://security.xyc.com/mytag" prefix="mt" %>
<html>
<head>
    <title>主页</title>
</head>
<body>
<mt:security module="user" permission="query">
    你有权限进行用户查询
</mt:security>
</body>
</html>
  • mytag.tld
<?xml version="1.0" encoding="UTF-8"?>

<taglib 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"
        version="2.1">

    <!--TLD:Tag Library Descriptor/标签库描述符-->
    <tlib-version>1.0</tlib-version><!--标签库的版本-->
    <short-name>xyc</short-name><!--这个标签是指定我们定义标签的简称,这个作用不大-->
    <uri>http://security.xyc.com/mytag</uri><!--这个标签是给这个标签文件指定一个访问路径,这个路径我们在Jsp页面中引入这个标签的时候需要用到-->

    <!--权限控制-->
    <tag>
        <description><!--标签的描述-->
            自定义权限标签
        </description>
        <name>security</name><!--标签的名称-->
        <tag-class>com.xyc.security.tag.SecurityTag</tag-class><!--这个标签就是指定我们自定义的标签类的全称-->
        <!--
        tagdependent:标签体内容直接被写入BodyContent,由自定义标签类来进行处理,而不被JSP容器解释
        jsp:接受所有JSP语法,如定制的或内部的tag、scripts、静态HTML、脚本元素、JSP指令和动作
        empty:空标记,即起始标记和结束标记之间没有内容
        scriptless:接受文本、EL和JSP动作
        -->
        <body-content>scriptless</body-content>
        <attribute>
            <description><!--属性的描述-->
                权限模块值
            </description>
            <name>module</name><!--属性的名称-->
            <required>true</required><!--表示这个属性是不是必须的-->
            <rtexprvalue>true</rtexprvalue><!--全称是 Run-time Expression Value, 它用于表示是否能够利用JSP表达式-->
        </attribute>
        <attribute>
            <description><!--属性的描述-->
                权限值
            </description>
            <name>permission</name><!--属性的名称-->
            <required>true</required><!--表示这个属性是不是必须的-->
            <rtexprvalue>true</rtexprvalue><!--全称是 Run-time Expression Value, 它用于表示是否能够利用JSP表达式-->
        </attribute>
    </tag>
</taglib>
  • 测试

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

  • 参考

http://www.cnblogs.com/quxiangfu/p/5184842.html
http://blog.csdn.net/white__cat/article/details/41647347
http://blog.csdn.net/white__cat/article/details/41647373

猜你喜欢

转载自blog.csdn.net/xyc_csdn/article/details/77141320
今日推荐