Tuesday, October 16, 2007

New Scaling Out Blog at www.scalingout.com

I started a new blog focused on Amazon Web Services and cloud computing over at www.scalingout.com. It will cover the latest news about the industry and showcase how people are using EC2, S3, etc. Check it out, subscribe, let me know your thoughts.

Tuesday, October 02, 2007

How to Add OpenID Support to your Java Application

Here is a quick and easy step by step to add OpenID support to your application. We are using joid because it's the lightest weight implementation with the least number of dependencies (2 jars).

  1. Download joid from http://code.google.com/p/joid/
  2. Copy joid.jar, log4j-*.jar, and tsik.jar to your lib directory (so they end up in WEB-INF/lib).
  3. Add OpenIdFilter to your web.xml (see below for how to add it)
  4. Add OpenId login form (see below for a sample jsp page)

After a user logs in, you can access the username that they're signed in as with:

String loggedInAs = OpenIdFilter.getCurrentUser(session);

Simple huh?

OpenIdFilter for web.xml

<description>Optional. Will store the identity url in a cookie under "openid.identity" if set to true.</description>
<description>Optional. Domain to store cookie based on RFC 2109. Defaults to current context.</description>

OpenID Login Form

<%@ page import="org.verisign.joid.consumer.OpenIdFilter" %>
<%@ page import="org.verisign.joid.util.UrlUtils" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
String returnTo = UrlUtils.getBaseUrl(request);

if (request.getParameter("signin") != null) {
try {
String id = request.getParameter("openid_url");
if (!id.startsWith("http:")) {
id = "
http://" + id;
String trustRoot = returnTo;

String s = OpenIdFilter.joid().getAuthUrl(id, returnTo, trustRoot);
} catch (Throwable e) {
An error occurred! Please press back and try again.
<head><title>A Page I Want to Login To</title></head>
This is a sample login page where a user enters their OpenID url to login.

String loggedInAs = OpenIdFilter.getCurrentUser(session);
if(loggedInAs != null){
<p align="center">
<span style=" background- padding:5px;">You are logged in as: <%=OpenIdFilter.getCurrentUser(session)%></span> - <a href="logout.jsp">Logout</a>

<div style='margin: 1em 0 1em 2em; border-left: 2px solid black; padding-left: 1em;'>
<form action="index.jsp" method="post">
<input type="hidden" name="signin" value="true"/>
<b>Login with your OpenID URL:</b> <input type="text" size="30" value=""
<input type="submit" value="Login"/><br/>
<i>For example: <tt>someone.bloghost.com</tt></i>

<strong>Don't have an OpenID?</strong> <a href="
https://pip.verisignlabs.com/" target="_blank">Go</a>
<a href="
http://www.myopenid.com/" target="_blank">get</a>
<a href="
https://myvidoop.com/" target="_blank">one</a>.