 <p><em>(I realise this question is old, but it's something that other people might want an answer to)</em></p> <p>BASIC Authentication doesn't really allow what you're asking for, but you can get something that works a bit like what you want, if you're willing to live with some "quirks".</p> <p>BASIC Authentication has 2 aspects that make it hard to control in this way</p> <ul> <li>It is stateless</li> <li>It is client-side</li> </ul> <p>Both of those aspects are features, but they make it hard to link BASIC authentication to Java sessions. That is why alternate login mechanisms (like Java FORM login exist)</p> <p><strong>BASIC Authentication is stateless</strong><br> That means that the client has absolutely no idea what is going on on the server-side, and it certainly has no way of linking a set of BASIC Authentication credentials to a cookie (which is what mostly controls the java session)<br> What happens in that the browser will simply send the username+password with <em>every</em> request, until it decides to stop (generally because the browser was closed, and a new browser session was created).<br> The browser doesn't know what the server is doing with the credentials. It doesn't know whether they're needed any more, or not, and it doesn't know when the server side has decided that a "new session" has started, it just keeps on sending that username+password with each request.</p> <p><strong>BASIC Authentication is Client-Side</strong><br> That dialog for the user/password is handled by the client. All the server says is "The URL you requested needs a username+password, and we've named this security realm 'xyz' ".<br> The server doesn't know whether the user is typing in a password each time, or whether the client has cached them. It doesn't know if there really is a user there, or whether the password was pulled out of a file.<br> The only thing the server can do is say "You need to give me a user+password before accessing this URL"</p> <p><strong>How to fake it</strong><br> Essentially, you need to detect (i.e. make an educated guess) when the browser is sending old credentials and then send the "Please give me a user+password" response (HTTP 401) again.</p> <p>The simplest way to do that is to have a filter in your application that detects the first time the user has logged in for that session, and sends a <code>401</code> response code. Something like:</p> <pre><code> if(session.getAttribute("auth") == null) { response.setStatus(401); response.setHeader("WWW-Authenticate", "basic realm=\"Auth (" + session.getCreationTime() + ")\"" ); session.setAttribute("auth", Boolean.TRUE); writer.println("Login Required"); return; } </code></pre> <p>In that example I've named the realm with the creation time of the session (although it's not very pretty - you'd probably want to format it differently). That means that the name of the security realm changes each time you invalidate the session, which helps prevent the client from getting confused (why is it asking me for a user+password again, for the same realm, when I just gave one?).<br> The new realm name will not confuse the servlet container, because it will never see it - the client response does not include the realm name.</p> <p>The trick though, it that you don't want the user to get asked for their password twice the first time they logon. And, out of the box, this solution will do that - once for the container to ask for it, and then again when your filter does.</p> <p><strong>How to avoid getting 2 login boxes</strong> There are 4 options.</p> <ol> <li>Do it all in code.</li> <li>Use a secondary session cookie to try and tell whether the user is logging in for the first time. </li> <li>Have your root URL be unsecured (but have it do nothing)</li> <li>Have all your app be unsecured, except for 1 page, and redirect users to that page if they are not logged in.</li> </ol> <p><strong>Do it in code</strong></p> <p>If you're happy to do all of the security inside your own servlet/filter, and get no help from the servlet container (Jetty) then that's not too hard. You simply turn off BASIC auth in your web.xml, and do it all in code. (There's a fair bit of work, and you need to make sure you don't leave any security holes open, but it's conceptually quite easy)<br> Most people don't want to do that.</p> <p><strong>Secondary Cookies</strong><br> After a user logins in to your application, you can set a cookie ("authenticated") that expires at the end of the browser session. This cookie is tied to the browser session and <strong>not</strong> to the Java session.<br> When you invalidate the Java session - <em>do not</em> invalidate the <em>"authenticated"</em> cookie.<br> If a user logs in to a <em>fresh</em> java session (i.e. <code>session.getAttribute("auth")==null</code>), but still has this <em>"authenticated"</em> then you know that they're re-using an existing browser session and are <em>probably</em> re-using existing HTTP authentication credentials. In that case you do the <code>401</code> trick to force them to give new credentials.</p> <p><strong>Unsecured root URL</strong><br> If your root URL is unsecured, and you know that this is the URL that your users always login to, then you can simply put your "auth"/<code>401</code> check at that URL, and it will solve the problem. But make sure you don't accidentally open a security hole.<br> This URL should not have any functionality other than redirecting users to the "real" app (that is secured)</p> <p><strong>Single secured URL</strong><br> Have 1 URL that is secured (e.g. "/login").<br> As well as your "auth"/<code>401</code> filter, have another filter (or additional code in the same filter) on all pages (other than login) that checks whether <code>request.getUserPrincipal()</code> is set. If not, redirect the user to "/login".</p> <p><strong>The <em>much</em> easier solution</strong><br> Just use a FORM login method instead of BASIC authentication. It's designed to solve this problem.</p>