views:

189

answers:

2

I have a requirement to perform HTTP authentication logic within a servlet application, rather than delegating this task to the container.

Specifically, I need a way of taking the headers of an HttpServletRequest which contains HTTP auth headers, and having them decoded into a data structure representing the supplied credentials, which the application can then process. Both basic and digest auth should be supported.

I could write this by hand, it wouldn't be too much of a chore, the RFCs are all well documented, but I'd quite like to use an off-the shelf library to do it for me.

My first thought was Spring Security, but from what I can tell this delegates this task to the container (I'm a bit unclear on that, it's a complicated code base).

Anyone know of any others?

A: 

I don't know of a framework off hand, but unless you're using BASIC authentication, you might not be able to get the password for the user.

If you are using BASIC authentication, it's pretty trivial to Base64Decode the Authentication header.

Sam Barnum
It's straightforward, yes, but not trivial, the formatting requirements for the headers is fiddly and error-prone. I don't need to recover the plaintext password, I just need to decode and encode the headers structures.
skaffman
+1  A: 
  • For BASIC, it is very easy to implement - just read the header, base64 decode it and split it on the ':' character. You can also use use Spring's BasicProcessingFilter, and supply your instance of AuthenticationManager.
  • With Digest, you cannot get the password from the request (that's the whole point...). Implementing all the details is not a trivial task, even thought the protocol is well documented. Therefore I'd go with Spring's DigestProcessingFilter. In this case you need to supply the UserDetailsService who provides the user's password based on the username (for the digest).
David Rabinowitz
Excellent, those filters are exactly what I needed. Thanks.
skaffman