This is possible, but AFAIK is not possible without (some) code. Here is a solution that does not affect deployed webapps in any way, but which also does not give you any fine-grained authorization, only authentication.
Tomcat 7 (and 6?) Has a great feature for authentication, although the web application does not have secure resources called preemtiveAuthentication
<Context preemptiveAuthentication="true"> <Valve className="org.apache.catalina.authenticator.BasicAuthenticator" /> </Context>
Pop, which is in your context, wherever it may be (perhaps you need to create $CATALINA_BASE/conf/Catalina/localhost/mywebapp.xml to protect mywebapp.war).
This will make any incoming request with anything in the authorization header to initiate authorization. Any request without an authorization header will still pass.
http://example.com:8080/mywebapp/ will work, and GET /http://user: password@example.com :8080/mywebapp/ will not (or it will check the username and password)
Thus, the remaining trick should disable the "every time" function, even for users who do not send any authorization header. That's where I had to go back to the valve.
Here is the code for the valve that sets the "Authorization" request header to "foo" if it is missing.
import javax.servlet.ServletException; import java.io.IOException; import org.apache.catalina.valves.ValveBase; import org.apache.catalina.connector.Request; import org.apache.catalina.connector.Response; public class ConditionallyAddFakeAuthorizationHeader extends ValveBase { public void invoke(Request request, Response response) throws IOException, ServletException { if (request.getCoyoteRequest().getMimeHeaders().getValue("authorization") == null) { request.getCoyoteRequest().getMimeHeaders().addValue("authorization").setString("foo"); } getNext().invoke(request, response); } }
Compile the file, give it a nice package if you want, and put it in the general path of the Tomcat class and add the change to mywebapp.xml as follows (add a new valve in front of the base authenticator!):
<Context preemptiveAuthentication="true"> <Valve className="ConditionallyAddFakeAuthorizationHeader"/> <Valve className="org.apache.catalina.authenticator.BasicAuthenticator" /> </Context>
And in your context, no request will be allowed through unless it is authenticated with respect to the area that you defined.