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();
}
}
}
<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();
}
}
}
相关文章推荐
- Eclipse启动时,长时间停留在loading description for...的状态
- java equals方法与toString方法代码
- Java中final局部变量的生命周期
- 《java编程思想》-第二章,一切都是对象
- Spring 启动报错 java.lang.IncompatibleClassChangeError: class org.springframework.co
- java读取属性配置文件工具类
- Spring 开发笔记
- Java NIO原理图文分析及代码实现
- android studio 项目 转换成eclipse
- java计算时间差的方法
- java基金会成立Set
- struts2 Properties
- spring
- java中如何导入03和07版的Excel文件数据,存入到数据库
- Java的内存分析
- java--实现将文字生成二维码图片,并在中间附上logo,下方附上文字
- Java回调函数机制
- 32位64位Eclipse和jdk对应关系
- 解决eclipse启动插件时PermGen space问题
- java.util.concurrent