|
29 | 29 |
|
30 | 30 | import java.io.IOException;
|
31 | 31 | import java.io.InputStream;
|
| 32 | +import java.io.PrintWriter; |
32 | 33 | import java.security.Principal;
|
33 | 34 | import java.util.LinkedHashSet;
|
34 | 35 | import java.util.List;
|
|
37 | 38 | import static com.google.common.io.ByteStreams.copy;
|
38 | 39 | import static com.google.common.io.ByteStreams.nullOutputStream;
|
39 | 40 | import static com.google.common.net.HttpHeaders.WWW_AUTHENTICATE;
|
| 41 | +import static com.google.common.net.MediaType.PLAIN_TEXT_UTF_8; |
40 | 42 | import static java.util.Objects.requireNonNull;
|
41 | 43 | import static javax.servlet.http.HttpServletResponse.SC_UNAUTHORIZED;
|
42 | 44 |
|
@@ -102,7 +104,18 @@ public void doFilter(ServletRequest servletRequest, ServletResponse servletRespo
|
102 | 104 | if (messages.isEmpty()) {
|
103 | 105 | messages.add("Unauthorized");
|
104 | 106 | }
|
105 |
| - response.sendError(SC_UNAUTHORIZED, Joiner.on(" | ").join(messages)); |
| 107 | + // The error string is used by clients for exception messages and |
| 108 | + // is presented to the end user, thus it should be a single line. |
| 109 | + String error = Joiner.on(" | ").join(messages); |
| 110 | + |
| 111 | + // Clients should use the response body rather than the HTTP status |
| 112 | + // message (which does not exist with HTTP/2), but the status message |
| 113 | + // still needs to be sent for compatibility with existing clients. |
| 114 | + response.setStatus(SC_UNAUTHORIZED, error); |
| 115 | + response.setContentType(PLAIN_TEXT_UTF_8.toString()); |
| 116 | + try (PrintWriter writer = response.getWriter()) { |
| 117 | + writer.write(error); |
| 118 | + } |
106 | 119 | }
|
107 | 120 |
|
108 | 121 | private static ServletRequest withPrincipal(HttpServletRequest request, Principal principal)
|
|
0 commit comments