页面树结构
转至元数据结尾
转至元数据起始

1 概述

需求:

将系统部署到外网后,移动端可以通过外网访问,但PC不能通过外网访问。

注意:本示例来源于实际项目,原始版本是V6,可作为参考性质,也许是不能运行的。

实现方案:

1,添加系统选项配置限制访问的地址,请见新增系统选项

2,使用过滤器拦截用户请求,如果发现请求的地址不是上面系统选项配置的,并且设备是PC,就重定向到一个提示页面,提示只能通过移动设备使用,过滤器的配置请见扩展插件开发基础(内含目录及配置文件介绍)中的插件声明文件extension.xml的说明;


“限制访问的地址”项,设置为Smartbi部署所在的IP地址后,即可达到上述要求。

最终效果截图如下:

PC通过外网访问时,会显示下面的图片。

2 编写修改代码

2.1 在ItemLimitAddr.js文件中添加系统选项配置限制访问的地址:

var ItemLimitAddr = function() {
	this.itemName = "限制访问的地址";
	this.dbKey = "NEW_SYSTEM_CONFIG_ItemLimitAddr";
};
lang.extend(ItemLimitAddr, 'freequery.config.configitem.AbstractSystemConfigItem');
// 进行初始化化动作并返回一个 tr 元素
ItemLimitAddr.prototype.init = function() {
	this.tr = document.createElement("tr");
	this.tr.height = "30";
	this.td1 = document.createElement("td");
	this.td1.align = "left";
	this.td1.width = "200px";
	this.td1.innerHTML = this.itemName + ":";
	this.tr.appendChild(this.td1);
	this.td2 = document.createElement("td");
	this.td2.innerHTML = "<input type='text' name='newConfigItemLimitAddr' id='txt' class='_txt' style='width:100%'>";
	this.tr.appendChild(this.td2);
	this.txt = domutils.findElementByClassName([ this.tr ], "_txt");
	return this.tr;
}
// 检查配置信息是否合法
ItemLimitAddr.prototype.validate = function() {
	return true;
}
// 保存配置并返回是否保存成功 ,对于从系统配置表里的获取数据的配置项来说,返回一个对象
ItemLimitAddr.prototype.save = function() {
	debugger;
	if (!this.validate())
		return false;
	var obj = {
		key : this.dbKey,
		value : this.txt.value
	};
	return obj;
}
// 对于从系统配置表里的获取数据的配置项来说,需要在初始化后根据配置信息来显示
ItemLimitAddr.prototype.handleConfig = function(systemConfig) {
	debugger;
	for ( var i in systemConfig) {
		var config = systemConfig[i];
		if (config && config.key == this.dbKey) {
			
			this.txt.value = config.value;
			break;
		}
	}
};

2.2 编写ConfigurationPatch.js添加配置:

var ConfigurationPatch = {
	extensionPoints : {
		SystemConfig : {
			configItem : [ {
				tabName : "公共设置",
				groupName : "公共设置",
				itemNumber : 99999,
				className : "bof.ext.configitems.ItemLimitAddr"
			} ]
		}
	}
};


2.3 编写过滤器拦截处理:

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
		throws IOException, ServletException {
	LOG.debug("该url正在过滤。。。。。");
	HttpServletRequest request = (HttpServletRequest) servletRequest;
	HttpServletResponse response = (HttpServletResponse) servletResponse;
	servletRequest.setCharacterEncoding("UTF-8");
	// 获取系统选项的"限制访问的地址"的值
	ConfigClientService cs = ConfigClientService.getInstance();
	ISystemConfig sc = cs.getSystemConfig("NEW_SYSTEM_CONFIG_ItemLimitAddr");
	String value = null;
	if (sc != null) {
		value = sc.getValue();
	}
	// 获取访问的ip地址
	String ip = request.getLocalAddr();
	LOG.debug("ip:" + ip + ",value:" + value);
	if (ip.equals(value)) {
		String requestHeader = request.getHeader("user-agent");
		if (LimitAddrFilter.isMobileDevice(requestHeader)) {
			LOG.debug("使用手机浏览器");
			filterChain.doFilter(servletRequest, servletResponse);
		} else {
			LOG.debug("使用web浏览器");
			response.sendRedirect(request.getContextPath() + "/error/error404.jsp");
			return;
		}
	} else {
		filterChain.doFilter(servletRequest, servletResponse);
	}
}

3 相关资源(EPPR-8002)

该示例的示例代码,与限制用户登陆移动设备的数量整合在一起,请前去下载。

  • 无标签

3 评论

  1. 梁志恒 发表:

    有些关键代码没有贴出来(例如:系统选项配置部分的代码)

    如果配上效果图会更清晰明了。

    现在只能下载扩展包看代码来了解了具体实现了。

     

  2. 梁健仓 发表:

    原示例存在名为“404”的目录,由于目录名会在JSP转Servlet过程中作为包名,会导致500错误。