<div dir="ltr"><div>Hi,<br><br></div>When an authentication mechanism (e.g. a JASPIC SAM) did not actually authenticate following a call to HttpServletRequest#authenticate, Undertow closes the stream.<br><br>This happens in io.undertow.servlet.spec.HttpServletRequestImpl.authenticate(HttpServletResponse) via the following code fragment:<br>
<br>if (sc.authenticate()) {<br><div>        ...<br>} else {<br>    // Not authenticated and response already sent.<br>    HttpServletResponseImpl responseImpl = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY).getOriginalResponse();<br>
    responseImpl.closeStreamAndWriter();<br>    return false;<br>}<br><br></div><div>I&#39;m not 100% sure why it closes the response stream (or writer) here. The Javadoc for HttpServletRequest#authenticate doesn&#39;t say this happens and it&#39;s not in the Servlet spec either.<br>
<br></div><div>Most importantly perhaps, it&#39;s not what JBoss AS 7/EAP 6 does. I don&#39;t think any other server actually does this (but have to investigate to be sure).<br><br></div><div>It looks like Undertow assumes that the authentication mechanism always sets all headers and commits the response, but the contract as expressed in its Javadoc seems weaker:<br>
<br>&quot;Use the container login mechanism configured for the ServletContext to authenticate the user making this request.<br><br>This method <b>may</b> modify and commit the argument HttpServletResponse.&quot;<br></div>
<div><br></div><div>(note the phrasing &quot;may&quot;)<br></div><div><br></div><div>As with many things in Servlet it&#39;s not entirely clear whether for the case that the method returns &quot;false&quot; the response &quot;must&quot; have been modified and committed, but whether this is the case or not, I don&#39;t think it says anywhere that after a call to this method nothing can be written to the response any more.<br>
<br></div><div>Furthermore, this also doesn&#39;t behave very well when a (wrapped) response is passed in, as it closes the original response, not the one passed in.<br><br></div><div>Kind regards,<br>Arjan</div></div>