HttpAuth: Java client code

HttpAuth is a daemon and framework for authenticating HTTP requests. Here's some java classes which will allow you to authenticate users via the daemon. In particular there are interfaces for both the Jetty Web Server as well as generic servlets.

This code is contained in the 'java' subdirectory of httpauth tarball.

Downloads

Binary: httpauth-client.jar
Source: httpauth-client-src.jar

Documentation

Jetty Web Server: Add the HttpAuth authenticator to your HttpContext:

...
HttpAuthConnectionSource pool = new HttpAuthConnectionPool("httpauth.example.com", 8020, "MyHandler");
JettyHttpAuthenticator authenticator = new JettyHttpAuthenticator(pool, null);
context.setAuthenticator(auth);
context.setRealm(new NullRealm());
...

Servlets: After receiving the request, authenticate it like so:

public void doGet(HttpServletRequest req, HttpServletResponse resp)
	throws ServletException, IOException
{
	String user;

	try
	{
		HttpAuthConnectionSource conn = new HttpAuthConnection("httpauth.example.com", 8020, "MyHandler");
		ServletHttpAuthenticator authenticator = new ServletAuthenticator(conn);
		user = authenticator.authenticate(req, resp);
		if(user == null)
		{
			// When authentication fails an appropriate response has been sent.
			return;
		}
	}
	catch(HttpAuthException e)
	{
		// Handle error
		...
	}

	// |user| will now be set appropriately
	...
}

Licensing and Support

BSD license. Contact me when bugs are found.

   [ httpauth | home page ]