自定义错误页面 404

Customize error page 404

我是 运行 休息 api 服务器使用 tomcat。 我正在尝试拨打电话:

localhost:8080/.script.alert%28.XSS.%29.%2Fscript./users

我收到了这样的回复:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /�script�alert(�XSS�)�/script�/users was not found on this server.</p>
</body></html>

我想自定义此消息,使 url 不会出现在消息中。 我不确定是谁在生成响应,是 apache 吗? tomcat?球衣?

我已经尝试将错误映射添加到 web.xml,但没有成功。

这是Tomcat生成的错误页面。您可以禁用它并只发送状态代码而不是错误页面。为此,您需要将以下 属性 设置为 true

ServerProperties.RESPONSE_SET_STATUS_OVER_SEND_ERROR

Whenever response status is 4xx or 5xx it is possible to choose between sendError or setStatus on container specific Response implementation. E.g. on servlet container Jersey can call HttpServletResponse.setStatus(...) or HttpServletResponse.sendError(...).

Calling sendError(...) method usually resets entity, response headers and provide error page for specified status code (e.g. servlet error-page configuration). However if you want to post-process response (e.g. by servlet filter) the only way to do it is calling setStatus(...) on container Response object.

If property value is true the method Response.setStatus(...) is used over default Response.sendError(...).

Type of the property value is boolean. The default value is false.

您可以通过调用 property(key, value)ResourceConfig 子类构造函数中设置它。或者,如果您使用 web.xml,只需将其设置为 init-param。在 web.xml 中,您需要使用字符串版本

jersey.config.server.response.setStatusOverSendError

错误是在 apache 级别上发生的,而不是我认为的 tomcat 级别上的。 为了覆盖 apache 错误: How to Override Apache error pages