Facebook authentication in Java
Using servlet filters and thread local variables to make your life easier
If you're a web developer who likes writing practical, quick, and simple utility applications for yourself and others to use, then Facebook is the dream platform. You don't have to write any user management, sign in, password change pages etc, it comes complete with advanced user management, including friends lists. There's a high chance that your target users already use it, which makes them more likely to adopt your application because they don't need to go and create yet another username/password on yet another website. It already has a theme and style sheets, and will present your application surrounded in menus and title bars. And it provides a large number of ways to publish content and send notifications when events occur. All these features mean that you, the developer, can spend more time doing what you wanted to do, that is, writing your application.
If Java is your chosen platform however, you may encounter some difficulties. Java developers tend to like following well established design patterns. We like to think of things as beans that have distinct purposes. We like to use frameworks such as Spring to manage the beans that contain logic, to glue our application together. We like our data to be stored in beans, and we access that data using getters and setters. The Facebook client API makes this difficult, for the following reasons:
- Its instantiation is dependent on parameters in an HTTP request. This means we can't use it as a typical Spring bean.
- Depending on which flavour you of client you use, it returns JSON objects, or DOM documents, not the Java beans we like to use.
This article will address the first issue, and in doing so make the second issue less of a worry.
James Roper is a software engineer specialising in JEE and open source development. James currently works for 