1 概述
需求:
项目现场将系统部署到外网可访问,但是希望限制只能够是手机、Pad等移动设备可访问,通过PC不能访问。
注意:本示例来源于实际项目,原始版本是V6,可作为参考性质,也许是不能运行的。
实现方案:
1,添加系统选项配置限制访问的地址,请见新增系统选项;
2,使用过滤器拦截用户请求,如果发现请求的地址不是上面系统选项配置的,并且设备是PC,就重定向到一个提示页面,提示只能通过移动设备使用,过滤器的配置请见扩展插件开发基础(内含目录及配置文件介绍)中的插件生命文件extension.xm的说明l。
最终效果截图如下:
相应地址访问就会找不到该网址
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 { // TODO Auto-generated method stub System.out.println("该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.info("ip:"+ip+",value:"+value); if (ip.equals(value)) { String requestHeader = request.getHeader("user-agent"); if (LimitAddrFilter.isMobileDevice(requestHeader)) { System.out.println("使用手机浏览器"); filterChain.doFilter(servletRequest, servletResponse); } else { System.out.println("使用web浏览器"); response.sendRedirect(request.getContextPath()+"/404/404.jsp"); return; } } else { filterChain.doFilter(servletRequest, servletResponse); } }