Skip to content

Commit 9372d15

Browse files
author
Dave Syer
committed
Explicitly set status on raw unwrapped response
The ErrorPageFilter wasn't setting the response status in the case that there was an error page mapped to the current request (i.e. for all autoconfigured apps). N.B. this only affects non-embedded apps. Fixes gh-1320
1 parent 6c5f8f9 commit 9372d15

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

spring-boot/src/main/java/org/springframework/boot/context/web/ErrorPageFilter.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
@Component
5757
@Order(Ordered.HIGHEST_PRECEDENCE)
5858
class ErrorPageFilter extends AbstractConfigurableEmbeddedServletContainer implements
59-
Filter, NonEmbeddedServletContainerFactory {
59+
Filter, NonEmbeddedServletContainerFactory {
6060

6161
private static Log logger = LogFactory.getLog(ErrorPageFilter.class);
6262

@@ -123,19 +123,20 @@ else if (!request.isAsyncStarted()) {
123123

124124
private void handleErrorStatus(HttpServletRequest request,
125125
HttpServletResponse response, int status, String message)
126-
throws ServletException, IOException {
126+
throws ServletException, IOException {
127127
String errorPath = getErrorPath(this.statuses, status);
128128
if (errorPath == null) {
129129
response.sendError(status, message);
130130
return;
131131
}
132+
response.setStatus(status);
132133
setErrorAttributes(request, status, message);
133134
request.getRequestDispatcher(errorPath).forward(request, response);
134135
}
135136

136137
private void handleException(HttpServletRequest request,
137138
HttpServletResponse response, ErrorWrapperResponse wrapped, Throwable ex)
138-
throws IOException, ServletException {
139+
throws IOException, ServletException {
139140
Class<?> type = ex.getClass();
140141
String errorPath = getErrorPath(type);
141142
if (errorPath == null) {

spring-boot/src/test/java/org/springframework/boot/context/web/ErrorPageFilterTests.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,28 @@ public void notAnError() throws Exception {
6363
assertTrue(this.response.isCommitted());
6464
}
6565

66+
@Test
67+
public void unauthorizedWithErrorPath() throws Exception {
68+
this.filter.addErrorPages(new ErrorPage("/error"));
69+
this.chain = new MockFilterChain() {
70+
@Override
71+
public void doFilter(ServletRequest request, ServletResponse response)
72+
throws IOException, ServletException {
73+
((HttpServletResponse) response).sendError(401, "UNAUTHORIZED");
74+
super.doFilter(request, response);
75+
}
76+
};
77+
this.filter.doFilter(this.request, this.response, this.chain);
78+
assertThat(this.chain.getRequest(), equalTo((ServletRequest) this.request));
79+
HttpServletResponseWrapper wrapper = (HttpServletResponseWrapper) this.chain
80+
.getResponse();
81+
assertThat(wrapper.getResponse(), equalTo((ServletResponse) this.response));
82+
assertTrue(this.response.isCommitted());
83+
assertThat(wrapper.getStatus(), equalTo(401));
84+
// The real response has to be 401 as well...
85+
assertThat(this.response.getStatus(), equalTo(401));
86+
}
87+
6688
@Test
6789
public void responseCommitted() throws Exception {
6890
this.filter.addErrorPages(new ErrorPage("/error"));

0 commit comments

Comments
 (0)