struts2提供了debug功能,可以通过struts/webconsole.html或static/webconsole.html进行OGNL表达式调试,而这个页面无论是否将struts.devMode配置项设为true都可以访问,总感觉有点不爽。
下面对这个功能进行改进,只有当struts.devMode配置项设为true时才允许访问webconsole.html,在org.apache.struts2.dispatcher.ng下的ExecuteOperations类进行调整。
public class ExecuteOperations {
private Dispatcher dispatcher;
private boolean devMode = false;//新增加一个变量,判断是否为开发模式,默认为false
@Deprecated
public ExecuteOperations(ServletContext servletContext, Dispatcher dispatcher) {
this.dispatcher = dispatcher;
//------------------获取Struts2的常量配置项struts.devMode 的值-------------------------------
devMode = Boolean.valueOf(dispatcher.getContainer().getInstance(String.class, StrutsConstants.STRUTS_DEVMODE));
}
public ExecuteOperations(Dispatcher dispatcher) {
this.dispatcher = dispatcher;
//------------------获取Struts2的常量配置项struts.devMode 的值-------------------------------
devMode = Boolean.valueOf(dispatcher.getContainer().getInstance(String.class, StrutsConstants.STRUTS_DEVMODE));
}
/**
* Tries to execute a request for a static resource
* @return True if it was handled, false if the filter should fall through
* @throws IOException
* @throws ServletException
*/
public boolean executeStaticResourceRequest(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
// there is no action in this request, should we look for a static resource?
String resourcePath = RequestUtils.getServletPath(request);
if ("".equals(resourcePath) && null != request.getPathInfo()) {
resourcePath = request.getPathInfo();
}
//-------------------------------------------------------------------------------
/**
* 如果不为开发模式,直接返回false,这里返回false时,会继续向下执行其它过滤器,
* 返回true不向下执行,它表示由本方法自己向页面输出内容,
* 详细见org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter类
* 这里直接过滤所有的内置资源路径,在使用了dojo标签时,由于会用到内置的静态资源(如css、js、图片等)会存在加载问题
* 如果只过滤debug资源,侧需要检查resourcePath里面的路径信息,根据resourcePath里面所包含的路径信息作处理
*/
if(!devMode){
return false;
}
/*
//只过滤webconsole.html页面
if(!devMode && null!=resourcePath && resourcePath.indexOf("webconsole.html")!=-1){
return false;
}
*/
//-------------------------------------------------------------------------------
StaticContentLoader staticResourceLoader = dispatcher.getContainer().getInstance(StaticContentLoader.class);
if (staticResourceLoader.canHandle(resourcePath)) {
staticResourceLoader.findStaticResource(resourcePath, request, response);
// The framework did its job here
return true;
} else {
// this is a normal request, let it pass through
return false;
}
}
/**
* Executes an action
* @throws ServletException
*/
public void executeAction(HttpServletRequest request, HttpServletResponse response, ActionMapping mapping) throws ServletException {
dispatcher.serviceAction(request, response, mapping);
}
}