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

<filter>
<filter-name>OpenIdFilter</filter-name>
<filter-class>org.verisign.joid.consumer.OpenIdFilter</filter-class>
<init-param>
<description>Optional. Will store the identity url in a cookie under "openid.identity" if set to true.</description>
<param-name>saveInCookie</param-name>
<param-value>true</param-value>
</init-param>
<!--
<init-param>
<param-name>cookieDomain</param-name>
<param-value>www.mydomain.com</param-value>
<description>Optional. Domain to store cookie based on RFC 2109. Defaults to current context.</description>
</init-param>
-->
</filter>
<filter-mapping>
<filter-name>OpenIdFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

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);
response.sendRedirect(s);
} catch (Throwable e) {
e.printStackTrace();
%>
An error occurred! Please press back and try again.
<%
}
return;
}
%>
<html>
<head><title>A Page I Want to Login To</title></head>
<body>
<h1>Login</h1>
<p>
This is a sample login page where a user enters their OpenID url to login.
</p>

<%
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>
</p>
<%
}
%>

<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=""
name="openid_url"/>
<input type="submit" value="Login"/><br/>
<i>For example: <tt>someone.bloghost.com</tt></i>
</form>
</div>

<p>
<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>.
</p>

</body>
</html>

13 comments:

Justen Stepka said...

If you are interested in running an OpenID server local during developing, check out Atlassian's Crowd which can be setup as a standalone OpenID server in less time than the download:

Travis Reeder said...

Responding to this comment.

You can get tsik in joid subversion: http://joid.googlecode.com/svn/trunk/lib/.

banana said...
This post has been removed by the author.
banana said...

Please help me!!!
I run your tutorial, but in joid-1.0.2.jar don't have UrlUtils class and OpenIDFilter. Could you help me find it where?
Thank you very much!

Travis Reeder said...

Banana: I'll see what I can do about getting a 1.0.3 jar built and put into downloads.

commando said...

can i download 1.0.3 with link?

Travis Reeder said...

Downloads are here: Downloads are here: http://code.google.com/p/joid/downloads/list

commando said...

i run tutorial, but it erro ,are you help me? thanks very much
ERRO
init:
deps-module-jar:
deps-ear-jar:
deps-jar:
Created dir: C:\Documents and Settings\Private\OpenID\build\web\WEB-INF\classes
Created dir: C:\Documents and Settings\Private\OpenID\build\web\META-INF
Copying 1 file to C:\Documents and Settings\Private\OpenID\build\web\META-INF
Copying 6 files to C:\Documents and Settings\Private\OpenID\build\web
library-inclusion-in-archive:
library-inclusion-in-manifest:
compile:
compile-jsps:
Created dir: C:\Documents and Settings\Private\OpenID\build\generated\src
Created dir: C:\Documents and Settings\Private\OpenID\build\generated\classes
Compiling 1 source file to C:\Documents and Settings\Private\OpenID\build\generated\classes
C:\Documents and Settings\Private\OpenID\build\generated\src\org\apache\jsp\index_jsp.java:6: package org.verisign.joid.consumer does not exist
import org.verisign.joid.consumer.OpenIdFilter;
C:\Documents and Settings\Private\OpenID\build\generated\src\org\apache\jsp\index_jsp.java:7: package org.verisign.joid.util does not exist
import org.verisign.joid.util.UrlUtils;
C:\Documents and Settings\Private\OpenID\build\generated\src\org\apache\jsp\index_jsp.java:48: cannot find symbol
symbol : variable UrlUtils
location: class org.apache.jsp.index_jsp
String returnTo = UrlUtils.getBaseUrl(request);
^
C:\Documents and Settings\Private\OpenID\build\generated\src\org\apache\jsp\index_jsp.java:58: cannot find symbol
symbol : variable OpenIdFilter
location: class org.apache.jsp.index_jsp
String s = OpenIdFilter.joid().getAuthUrl(id, returnTo, trustRoot);
^
C:\Documents and Settings\Private\OpenID\build\generated\src\org\apache\jsp\index_jsp.java:81: cannot find symbol
symbol : variable OpenIdFilter
location: class org.apache.jsp.index_jsp
String loggedInAs = OpenIdFilter.getCurrentUser(session);
^
C:\Documents and Settings\Private\OpenID\build\generated\src\org\apache\jsp\index_jsp.java:87: cannot find symbol
symbol : variable OpenIdFilter
location: class org.apache.jsp.index_jsp
out.print(OpenIdFilter.getCurrentUser(session));
6 errors
C:\Documents and Settings\Private\OpenID\nbproject\build-impl.xml:364: The following error occurred while executing this line:
C:\Documents and Settings\Private\OpenID\nbproject\build-impl.xml:149: Compile failed; see the compiler error output for details.
BUILD FAILED (total time: 2 seconds)

Travis Reeder said...

I'm going to guess that the 1.0.3 jar is not on your classpath since it's not even finding the packages.

commando said...

yes. where i can find libary ?

Travis Reeder said...

Read a few comments up for download link.

commando said...

thanks very much,i don't understand
"
After a user logs in, you can access the username that they're signed in as with:

String loggedInAs = OpenIdFilter.getCurrentUser(session);

Simple huh?
"
are you explain? i run tutorial but result "An error occurred! Please press back and try again. "
i learning about OpenID for java and i make project wed use openid.

commando said...

hi, I am a Student. I learning IT at Nong Lam university ho chi minh city of Viet Nam. I make project have useing Openid. i run tutorial your Joid but result "An error occurred! Please press back and try again. " and don't understand
"
After a user logs in, you can access the username that they're signed in as with:

String loggedInAs = OpenIdFilter.getCurrentUser(session);

Simple huh?
"
are you explain? are you help me?
thanks you very much. see you a soon
my email address : TuanCommando@gmail.com