我在 web.xml 文件中配置了一个自定义错误页面,但页面中引用的图像显示为断开的链接。
自定义错误页面只是一个简单的 html 页面:
<!DOCTYPE html>
<html><head>
<meta charset="UTF-8"><title>401 Error</title></head>
<body>
<p style="font-size: 200%; text-align: center">HTTP Error 401: Not authorized to view sensitive data.<br/>
<img src="NoAccessImage.png" alt="401Error"><br/>
You must log in before viewing the requested page.</p>
</body></html>
此页面与其引用的图像文件一起存储在错误文件夹中。身份验证过滤器用于抛出 401 错误,如果我在未先登录的情况下尝试查看受保护的内容,则该页面会显示。但引用的图像丢失了。如果我只是将文件拖到网络浏览器中,它会正确显示,所以我认为这是一个上下文问题。我尝试将 img src 标签更改为"/error/NoAccessImage.png"
没有结果。
相关的 AuthenticationFilter 代码如下:
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
try {
boolean authorized = false;
HttpServletRequest r = (HttpServletRequest) request;
HttpSession session = ((HttpServletRequest)request).getSession(false);
String uri = r.getRequestURI();
if(uri.indexOf("/Login")>0) {
chain.doFilter(request, response);
return;
}
if (session != null) {
String school = (String) session.getAttribute("school");
if(school != null && school.length()>0 ) {
authorized = (school.equals(getURISchool(uri)));
}
}
if (authorized) {
chain.doFilter(request, response);
return;
} else {
((HttpServletResponse) response).sendError(401, "You must log in to view the schedule.");
}
} catch (IOException io) {
System.out.println("IOException raised in AuthenticationFilter");
}
}