<?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>Christian Schenk&#187; Realm</title>
	<atom:link href="http://www.christianschenk.org/blog/tag/realm/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.christianschenk.org</link>
	<description>Writing about my experiences with technology and all different kinds of projects and experiments</description>
	<lastBuildDate>Sun, 29 Aug 2010 09:08:16 +0000</lastBuildDate>
	
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Setup your own Tomcat security realm</title>
		<link>http://www.christianschenk.org/blog/setup-your-own-tomcat-security-realm/</link>
		<comments>http://www.christianschenk.org/blog/setup-your-own-tomcat-security-realm/#comments</comments>
		<pubDate>Thu, 07 Jun 2007 08:23:17 +0000</pubDate>
		<dc:creator>Christian Schenk</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[HowTo]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Realm]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Tomcat]]></category>

		<guid isPermaLink="false">http://www.christianschenk.org/blog/setup-your-own-tomcat-security-realm/</guid>
		<description><![CDATA[This post shows how to setup your own security realm with Tomcat]]></description>
			<content:encoded><![CDATA[<p>Recently I wanted to code my own security realm for fun. Without further ado, lets see how that works.</p>
<p><span id="more-25"></span></p>
<h2>Coding the realm</h2>
<p>Coding the realm is a snap because you can inherit from <code>RealmBase</code>. All you have to do is implement three methods: <code>getName</code>, <code>getPassword</code> and <code>getPrincipal</code>. The last two methods are the ones you should be interested in because they&#8217;ll get a user name as parameter and return either a password or a implementation of the <code>Principal</code> interface.</p>
<p>The servlet container will first make a call to <code>getPassword</code>. If authentication succeeded it&#8217;ll then call <code>getPrincipal</code> to check the user&#8217;s role. If the user hasn&#8217;t got a role which is allowed to access the inquired resource he&#8217;ll be refused to do so.</p>
<p>Let&#8217;s have a look at the code:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">org.christianschenk.testrealm</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.security.Principal</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.ArrayList</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.catalina.realm.GenericPrincipal</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.catalina.realm.RealmBase</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyRealm <span style="color: #000000; font-weight: bold;">extends</span> RealmBase <span style="color: #009900;">&#123;</span>
&nbsp;
  @Override
  <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #003399;">String</span> getName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getSimpleName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  @Override
  <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #003399;">String</span> getPassword<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> username<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;test123&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  @Override
  <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #003399;">Principal</span> getPrincipal<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> username<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">final</span> List<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> roles <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    roles.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;tomcat&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> GenericPrincipal<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, username, <span style="color: #0000ff;">&quot;test123&quot;</span>, roles<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>As you might guess from the code every user name will be allowed as long as the password is <code>test123</code>. Furthermore the user will have the role <code>tomcat</code>.</p>
<p>Compile the code, put it in a JAR and copy the JAR to <code>server/lib</code> inside your Tomcat.</p>
<h2>Configure the webapp</h2>
<p>Next we&#8217;ll add a context to the Tomcat server and put the realm inside it. If the need arises the realm can also go into the <code>Engine</code> or <code>Host</code> element and thus have a broader scope. Finally we&#8217;ll configure the webapp (<code>web.xml</code>) and declare some security requirements.</p>
<p>Put the following context into <code>conf/Catalina/localhost/realmtest.xml</code> or inside the <code>server.xml</code> and adjust the <code>docBase</code> attribute:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Context</span> <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;/realmtest&quot;</span> <span style="color: #000066;">debug</span>=<span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #000066;">reloadable</span>=<span style="color: #ff0000;">&quot;true&quot;</span></span>
<span style="color: #009900;">         <span style="color: #000066;">docBase</span>=<span style="color: #ff0000;">&quot;path/to/your/webapp/&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Realm</span> <span style="color: #000066;">className</span>=<span style="color: #ff0000;">&quot;org.christianschenk.testrealm.MyRealm&quot;</span> <span style="color: #000066;">debug</span>=<span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Context<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>I want to restrict access to the whole webapp with <a title="HTTP Authentication: Basic and Digest Access Authentication" href="http://www.ietf.org/rfc/rfc2617.txt">Basic</a> HTTP authentication and want users to have the role <code>tomcat</code>, so I put this into the webapp&#8217;s <code>web.xml</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;security-constraint<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;web-resource-collection<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;web-resource-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>The entire webapp<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/web-resource-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;url-pattern<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/*<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/url-pattern<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/web-resource-collection<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;auth-constraint<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;role-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>tomcat<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/role-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/auth-constraint<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/security-constraint<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;login-config<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;auth-method<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>BASIC<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/auth-method<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;realm-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>MyRealm<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/realm-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/login-config<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;security-role<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;role-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>tomcat<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/role-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/security-role<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<h2>Testing and Conclusion</h2>
<p>To test the setup start the Tomcat server and check <code>catalina.out</code> for errors. If everything went fine try to access your web application at <code>/realmtest</code> where you should be prompted to enter a user name and password. Type whatever user name you want and use <em>test123</em> for the password: your attempt to log in will be successful. If you try to enter another password, access should be denied.</p>
<p>As we have seen it&#8217;s easy and fun to code and setup your own realm for the Tomcat server. Before reinventing the wheel have a look at the <a title="Realm Configuration HOW-TO" href="http://tomcat.apache.org/tomcat-6.0-doc/realm-howto.html">Realm Configuration Howto</a> and check out the available realms. Probably it&#8217;s the <a title="JDBCRealm" href="http://tomcat.apache.org/tomcat-6.0-doc/realm-howto.html#JDBCRealm">JDBCRealm</a> you&#8217;re looking for: it&#8217;ll fetch credentials and roles from a database. If this isn&#8217;t sufficient, go ahead and write your own realm.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.christianschenk.org/blog/setup-your-own-tomcat-security-realm/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>
