概述
Web应用出错时通常抛出403,404,500等异常,JBoss中部署的web应用如果没有考虑错误页面的处理,出错时错误页面如下所示:
如上在出错页面上包括两部分信息:
- 页头显示JBossWeb的版本信息
- 页面中显示JBossWeb相关的版本信息
如何隐藏
我们分三步:
第一:重写JBossWeb的org.apache.catalina.valves.ErrorReportValve,如下为一个例子:
package org.jboss.web.values; import java.io.IOException; import java.io.Writer; import org.apache.catalina.connector.Request; import org.apache.catalina.connector.Response; import org.apache.catalina.valves.ErrorReportValve; import org.apache.catalina.util.RequestUtil; public class CustomErrorReportValve extends ErrorReportValve { protected void report(Request request, Response response, Throwable throwable) { int statusCode = response.getStatus(); if ((statusCode < 400) || (response.getContentCount() > 0)) { return; } String message = RequestUtil.filter(response.getMessage()); if (message == null) { message = ""; } StringBuffer sb = new StringBuffer(); sb.append("<html><head><title>"); sb.append(sm.getString("errorReportValve.statusHeader", "" + statusCode, message)); sb.append("</title>"); sb.append("<style><!--"); sb.append(org.apache.catalina.util.TomcatCSS.TOMCAT_CSS); sb.append("--></style> "); sb.append("</head><body>"); sb.append("<h1>"); sb.append(sm.getString("errorReportValve.statusHeader", "" + statusCode, message)); sb.append("</h1>"); sb.append("</body></html>"); try { try { response.setContentType("text/html"); response.setCharacterEncoding("utf-8"); } catch (Throwable t) { if (container.getLogger().isDebugEnabled()) container.getLogger().debug("status.setContentType", t); } Writer writer = response.getReporter(); if (writer != null) { // If writer is null, it‘s an indication that the response has // been hard committed already, which should never happen writer.write(sb.toString()); } } catch (IOException e) { ; } catch (IllegalStateException e) { ; } } }
第二:将重写的ErrorReportValve,编译打包生成一个jar包,比如我们此处编译后jar包为 CustomErrorReportValve.jar。
第三:配置JBoss
JBoss 7/WildFly 8配置如下
1. 将CustomErrorReportValve.jar 放置到您应用
WEB-INF/lib目录
<valve>配置如下:2.创建
WEB-INF/jboss-web.xml,添加
<jboss-web> <valve> <class-name>com.redhat.jboss.support.CustomErrorReportValve</class-name> </valve> </jboss-web>
JBoss 7 之前版本配置如下:
1。将CustomErrorReportValve.jar 放置到
$JBOSS_HOME/server/$PROFILE/lib
2. 编辑
$JBOSS_HOME/server/$PROFILE/deploy/jboss-web.deployer/server.xml中
添加Host元素,
errorReportValveClass属性指向重写的ErrorReportValve如下:
<Host name="localhost" errorReportValveClass="org.jboss.web.values.CustomErrorReportValve" >
隐藏后效果
隐藏后效果
隐藏后效果如下图所示: