您的位置:首页 > 编程语言 > Java开发

Spring MVC+FreeMarker实现页面按钮权限控制1

2015-07-17 09:35 681 查看
通常我们在做B/S系统时会涉及到需要控制页面上按钮的权限,也就是不同的用户登录系统,在页面上看到的按钮不一样,针对这种需求,一般会采用硬编码的方式实现,其实可以通过一种简单的方式来实现页面按钮权限的控制:自定义Freemarker标签。下面看最终效果代码片段:

           <div class="btn-group billheader">

    <@auth paddingtext="  " >
<a class="btn" id="submit" href="#"><i class="icon-plus"></i>提交</a>
<a class="btn" id="audit" href="#"><i class="icon-lock"></i>审核</a>
<a class="btn" id="remove" href="#"><i class="icon-remove"></i>作废</a>
<a class="btn" id="print" href="#"><i class="icon-print"></i>打印</a>
<a class="btn" id="refresh" href="#"><i class="icon-refresh"></i>刷新</a>
</@auth>
</div>

FreeMarket标签实现代码:

/**

 * Description: TODO {页面控件授权标签}<br/>

 * 

 * @author Administrator

 * @date: 2015年3月11日 上午9:34:38

 * @version 1.0

 * @since JDK 1.7

 */

public class AuthDirective implements TemplateDirectiveModel {

    Logger logger = Logger.getLogger(AuthDirective.class);

    private MenuInfoService menuInfoServiceImpl;

    public void execute(Environment env, @SuppressWarnings("rawtypes") Map params, TemplateModel[] loopVars,

            TemplateDirectiveBody body) throws TemplateException, IOException {

        String spacetext = "";

        if (params.get("paddingtext") != null) {

            spacetext = ((SimpleScalar) params.get("paddingtext")).getAsString();

        }

        HttpServletRequest request = this.getRequest();

        String url = request.getServletPath();

        // 获取用户登录信息

        UserInfo userInfo = this.getLoginUserInfo(request);

        if (userInfo != null) {

            // 获取用户菜单页面

            MenuInfo menuInfo = this.menuInfoServiceImpl.getMenuInfo(url, userInfo.getRoleList());

            if (menuInfo != null) {

                logger.info(JSON.toJSONString(menuInfo));

                // 输出授权后html内容到页面

                body.render(new authFilterWriter(env.getOut(), menuInfo, spacetext));

            } else {

                env.getOut().write("");

            }

        } else {

            env.getOut().write("");

        }

    }

    private HttpServletRequest getRequest() {

        ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();

        HttpServletRequest request = attr.getRequest();

        return request;

    }

    private UserInfo getLoginUserInfo(HttpServletRequest request) {

        if (request != null && request.getAttribute("userInfo") != null) {

            UserInfo userInfo = (UserInfo) request.getAttribute("userInfo");

            return userInfo;

        } else {

            return null;

        }

    }

    private String parseHtml(String html, MenuInfo menuInfo, String spacetext) {

        StringBuilder sBuilder = new StringBuilder();

        List<CtrlInfo> ctrlInfos = menuInfo.getCtrlInfos();

        for (CtrlInfo ctrlInfo : ctrlInfos) {

            String autHtml = this.getAutHtml(ctrlInfo.getSelector(), html);

            sBuilder.append(autHtml.toLowerCase());

            sBuilder.append(spacetext);

        }

        return sBuilder.toString();

    }

    private String getAutHtml(String ctrlId, String html) {

        Parser parser = Parser.createParser(html, "UTF-8");

        NodeFilter filter = new HasAttributeFilter("id", ctrlId.toUpperCase());

        try {

            NodeList nodes = parser.extractAllNodesThatMatch(filter);

            if (nodes.size() > 0) {

                Node node = nodes.elementAt(0);

                return node.toHtml();

            } else {

                return "";

            }

        } catch (ParserException e) {

            e.printStackTrace();

            return "";

        }

    }

    public MenuInfoService getMenuInfoServiceImpl() {

        return menuInfoServiceImpl;

    }

    public void setMenuInfoServiceImpl(MenuInfoService menuInfoServiceImpl) {

        this.menuInfoServiceImpl = menuInfoServiceImpl;

    }

    /**

     * 输出流的包装器(转换大写字母)

     */

    private class authFilterWriter extends Writer {

        private final Writer out;

        private MenuInfo menuInfo;

        private String spacetext;

        authFilterWriter(Writer out, MenuInfo menuInfo, String spacetext) {

            this.out = out;

            this.menuInfo = menuInfo;

            this.spacetext = spacetext;

        }

        public void write(char[] cbuf, int off, int len) throws IOException {

            char[] transformedCbuf = new char[len];

            for (int i = 0; i < len; i++) {

                transformedCbuf[i] = Character.toUpperCase(cbuf[i + off]);

            }

            String noAuthHtml = new String(transformedCbuf);

            logger.info("授权前的html------ >" + noAuthHtml);

            // 通过解析标签中html内容,接口后台的权限配置数据过滤出授权后台html内容

            String rtnHtml = parseHtml(noAuthHtml, menuInfo, spacetext);

            logger.info("授权后的html------ >" + rtnHtml);

            out.write(rtnHtml.toCharArray());

        }

        public void flush() throws IOException {

            out.flush();

        }

        public void close() throws IOException {

            out.close();

        }

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: