<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Coding Clarity &#187; http authentication</title>
	<atom:link href="http://chrisdail.com/tag/http-authentication/feed/" rel="self" type="application/rss+xml" />
	<link>http://chrisdail.com</link>
	<description>Writing simple, clear and readable code.</description>
	<lastBuildDate>Mon, 21 Jun 2010 12:22:25 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>URL Based HTTP Authentication</title>
		<link>http://chrisdail.com/2007/10/26/url-based-http-authentication/</link>
		<comments>http://chrisdail.com/2007/10/26/url-based-http-authentication/#comments</comments>
		<pubDate>Fri, 26 Oct 2007 18:21:00 +0000</pubDate>
		<dc:creator>chris</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[http authentication]]></category>
		<category><![CDATA[URL]]></category>

		<guid isPermaLink="false">http://chrisdail.com/2007/10/26/url-based-http-authentication/</guid>
		<description><![CDATA[I ran into a situation the other day where I needed to connect to a remote Web server from a server side application. This web server requires HTTP authentication. The user sets the Web Server location from a configuration file.
What I wanted to be able to do was have the user enter the URL in [...]]]></description>
			<content:encoded><![CDATA[<p>I ran into a situation the other day where I needed to connect to a remote Web server from a server side application. This web server requires HTTP authentication. The user sets the Web Server location from a configuration file.</p>
<p>What I wanted to be able to do was have the user enter the URL in the following format:</p>
<p>http://user:password@server.com/somewhere</p>
<p>The plan was to use Java&#8217;s URL connection library to make my connection to the website. I figured that the default implementation of the URL authentication would allow the user name and password to be supplied as part of the URL. To my surprise, I was greeted with a &#8220;401 Unauthorized&#8221; message.</p>
<p>The way to provide HTTP based authentication in Java is using the <a href="http://java.sun.com/javase/6/docs/api/java/net/Authenticator.html">java.net.Authenticator</a> mechanism. Java 5 provides a new method on the Authenticator called getRequestingURL(). This allows access to the URL that requested the authentication. By using this method, an Authenticator can get access to the user name and password embedded in the URL.</p>
<p>So, I created a simple Authenticator that uses the <a href="http://jakarta.apache.org/httpcomponents/httpclient-3.x/">Apache Commons HTTPClient</a> to parse the URL and pull out the authentication information.</p>
<p>Here is the code:</p>
<p><span style="font-family: courier new"></span></p>
<pre>public class HTTPAuthenticator extends Authenticator {</pre>
<pre>    protected PasswordAuthentication getPasswordAuthentication() {</pre>
<pre>       if (!getRequestingProtocol().equals("http") &amp;&amp;</pre>
<pre>               !getRequestingProtocol().equals("https")) {</pre>
<pre>           return null;</pre>
<pre>       }       URL url = getRequestingURL();</pre>
<pre>       try {</pre>
<pre>           // Get the Username and password from the URL</pre>
<pre>           HttpURL httpUrl = new HttpURL(url.toString());</pre>
<pre>           String username = httpUrl.getUser();</pre>
<pre>           char[] password = httpUrl.getRawPassword();</pre>
<pre>           if (username == null || password == null) {</pre>
<pre>               return null;</pre>
<pre>           }</pre>
<pre>           return new PasswordAuthentication(username, password);</pre>
<pre>       }</pre>
<pre>       catch (Exception e) {</pre>
<pre>           return null;</pre>
<pre>       }</pre>
<pre>    }</pre>
<pre>    public void init() {

        Authenticator.setDefault(this);

    }
}</pre>
]]></content:encoded>
			<wfw:commentRss>http://chrisdail.com/2007/10/26/url-based-http-authentication/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
