<?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>Ashish's Tech Blog &#187; Spring Security</title>
	<atom:link href="http://www.ashishpaliwal.com/blog/category/spring-security/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ashishpaliwal.com/blog</link>
	<description>From Programmer, For Programmers</description>
	<lastBuildDate>Tue, 17 Aug 2010 12:04:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>[Spring Security] – Implementing Custom UserDetails using Hibernate</title>
		<link>http://www.ashishpaliwal.com/blog/2009/02/spring-security-%e2%80%93-implementing-custom-userdetails-using-hibernate/</link>
		<comments>http://www.ashishpaliwal.com/blog/2009/02/spring-security-%e2%80%93-implementing-custom-userdetails-using-hibernate/#comments</comments>
		<pubDate>Sat, 14 Feb 2009 11:25:31 +0000</pubDate>
		<dc:creator>ashish</dc:creator>
				<category><![CDATA[Spring Security]]></category>
		<category><![CDATA[Custom User Details]]></category>

		<guid isPermaLink="false">http://www.ashishpaliwal.com/blog/?p=248</guid>
		<description><![CDATA[Recently was experimenting with creating a Custom UserDetails in Spring Security. Unfortunately, it took me a while before I could implement. In this post shall share my experience and how I developed it. Assumption I shall concentrate only on the configuration of UserDetails service. To see it in Action, you can take Spring petclinic tutorial [...]]]></description>
			<content:encoded><![CDATA[<p>Recently was experimenting with creating a Custom UserDetails in Spring Security. Unfortunately, it took me a while before I could implement. In this post shall share my experience and how I developed it.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6961884887741817";
/* 468x15, created 1/25/10 */
google_ad_slot = "2118617107";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<p><strong>Assumption</strong></p>
<p>I shall concentrate only on the configuration of UserDetails service. To see it in Action, you can take Spring petclinic tutorial and add the configuration to it.</p>
<p>Let’s briefly lay down the steps needed to implement a Custom UserDetails</p>
<ul>
<li><span> </span>Implement a Custom User Class that implements UserDetails interface (This class can be customized to fit your needs)</li>
<li><span> </span>Implement Custom UserDetailsService to based on Hibernate</li>
<li>Configuring in the Spring context</li>
</ul>
<p><strong>Implementing Custom UserDetails</strong></p>
<p>For simplicity perspective, I shall be omitting Hibernate related configuration from this section</p>
<pre class="java5">&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> java.<span style="color: #006600;">io</span>.<a href="http://java.sun.com/j2se/1.5.0/docs/api/java/io/Serializable.html"><span style="color: #aaaadd; font-weight: bold;">Serializable</span></a>;
<span style="color: #000000; font-weight: bold;">import</span> java.<span style="color: #006600;">util</span>.<a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/ArrayList.html"><span style="color: #aaaadd; font-weight: bold;">ArrayList</span></a>;
<span style="color: #000000; font-weight: bold;">import</span> java.<span style="color: #006600;">util</span>.<a href="http://www.google.com/search?sitesearch=java.sun.com&amp;q=allinurl%3Aj2se%2F1+5+0%2Fdocs%2Fapi+List"><span style="color: #aaaadd; font-weight: bold;">List</span></a>;
<span style="color: #000000; font-weight: bold;">import</span> java.<span style="color: #006600;">util</span>.<a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/Set.html"><span style="color: #aaaadd; font-weight: bold;">Set</span></a>;
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> org.<span style="color: #006600;">springframework</span>.<span style="color: #006600;">security</span>.<span style="color: #006600;">GrantedAuthority</span>;
<span style="color: #000000; font-weight: bold;">import</span> org.<span style="color: #006600;">springframework</span>.<span style="color: #006600;">security</span>.<span style="color: #006600;">GrantedAuthorityImpl</span>;
<span style="color: #000000; font-weight: bold;">import</span> org.<span style="color: #006600;">springframework</span>.<span style="color: #006600;">security</span>.<span style="color: #006600;">userdetails</span>.<span style="color: #006600;">UserDetails</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> CustomUser <span style="color: #000000; font-weight: bold;">implements</span> <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/io/Serializable.html"><span style="color: #aaaadd; font-weight: bold;">Serializable</span></a>, UserDetails <span style="color: #66cc66;">&#123;</span>
         <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #993333;">long</span> serialVersionUID = 1L;
         <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">long</span> id;
         <span style="color: #000000; font-weight: bold;">private</span> <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html"><span style="color: #aaaadd; font-weight: bold;">String</span></a> password;
         <span style="color: #000000; font-weight: bold;">private</span> <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html"><span style="color: #aaaadd; font-weight: bold;">String</span></a> username;
         <span style="color: #000000; font-weight: bold;">private</span> <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/Set.html"><span style="color: #aaaadd; font-weight: bold;">Set</span></a> roles;
&nbsp;
       <span style="color: #000000; font-weight: bold;">public</span> CustomUser<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
       <span style="color: #66cc66;">&#125;</span>
&nbsp;
       <span style="color: #000000; font-weight: bold;">public</span> <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/Set.html"><span style="color: #aaaadd; font-weight: bold;">Set</span></a> getRoles<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
           <span style="color: #000000; font-weight: bold;">return</span> roles;
       <span style="color: #66cc66;">&#125;</span>
&nbsp;
      <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> setRoles<span style="color: #66cc66;">&#40;</span><a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/Set.html"><span style="color: #aaaadd; font-weight: bold;">Set</span></a> roles<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">roles</span> = roles;
      <span style="color: #66cc66;">&#125;</span>
&nbsp;
     <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">boolean</span> accountNonExpired;
     <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">boolean</span> accountNonLocked;
     <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">boolean</span> credentialsNonExpired;
     <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">boolean</span> enabled;
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> GrantedAuthority<span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> getAuthorities<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <a href="http://www.google.com/search?sitesearch=java.sun.com&amp;q=allinurl%3Aj2se%2F1+5+0%2Fdocs%2Fapi+List"><span style="color: #aaaadd; font-weight: bold;">List</span></a> list = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/ArrayList.html"><span style="color: #aaaadd; font-weight: bold;">ArrayList</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
            <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span>Authority role : roles<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                  list.<span style="color: #006600;">add</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> GrantedAuthorityImpl<span style="color: #66cc66;">&#40;</span>role.<span style="color: #006600;">getAuthority</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
            <span style="color: #66cc66;">&#125;</span>
           <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #66cc66;">&#40;</span>GrantedAuthority<span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>list.<span style="color: #006600;">toArray</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> GrantedAuthority<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>Take a special note of getAuthorities(). This function from UserDetails interface needs to return all the authorities for the given user. You can store Roles in whichever format you desire, and can translate them here. For simplicity sake, here is my Authorities class.</p>
<pre class="java5"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Authority <span style="color: #000000; font-weight: bold;">implements</span> <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/io/Serializable.html"><span style="color: #aaaadd; font-weight: bold;">Serializable</span></a> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #993333;">long</span> serialVersionUID = 1L;
        <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">long</span> id;
        <span style="color: #000000; font-weight: bold;">private</span> <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html"><span style="color: #aaaadd; font-weight: bold;">String</span></a> username;
        <span style="color: #000000; font-weight: bold;">private</span> <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html"><span style="color: #aaaadd; font-weight: bold;">String</span></a> authority;
<span style="color: #66cc66;">&#125;</span></pre>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6961884887741817";
/* 468x15, created 1/25/10 */
google_ad_slot = "2118617107";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<p><strong>Implementing Custom UserDetailsService</strong></p>
<p>Here using spring comes very handy. We extend HibernateDaoSupport and get most of the desired Hibernate functionality. Lets take a look at the class.</p>
<pre class="java5"> 
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> java.<span style="color: #006600;">util</span>.<a href="http://www.google.com/search?sitesearch=java.sun.com&amp;q=allinurl%3Aj2se%2F1+5+0%2Fdocs%2Fapi+List"><span style="color: #aaaadd; font-weight: bold;">List</span></a>;
<span style="color: #000000; font-weight: bold;">import</span> org.<span style="color: #006600;">springframework</span>.<span style="color: #006600;">dao</span>.<span style="color: #006600;">DataAccessException</span>;
<span style="color: #000000; font-weight: bold;">import</span> org.<span style="color: #006600;">springframework</span>.<span style="color: #006600;">orm</span>.<span style="color: #006600;">hibernate3</span>.<span style="color: #006600;">support</span>.<span style="color: #006600;">HibernateDaoSupport</span>;
<span style="color: #000000; font-weight: bold;">import</span> org.<span style="color: #006600;">springframework</span>.<span style="color: #006600;">security</span>.<span style="color: #006600;">userdetails</span>.<span style="color: #006600;">UserDetails</span>;
<span style="color: #000000; font-weight: bold;">import</span> org.<span style="color: #006600;">springframework</span>.<span style="color: #006600;">security</span>.<span style="color: #006600;">userdetails</span>.<span style="color: #006600;">UserDetailsService</span>;
<span style="color: #000000; font-weight: bold;">import</span> org.<span style="color: #006600;">springframework</span>.<span style="color: #006600;">security</span>.<span style="color: #006600;">userdetails</span>.<span style="color: #006600;">UsernameNotFoundException</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> HibernateUserDetailsService <span style="color: #000000; font-weight: bold;">extends</span> HibernateDaoSupport <span style="color: #000000; font-weight: bold;">implements</span> UserDetailsService <span style="color: #66cc66;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">public</span> UserDetails loadUserByUsername<span style="color: #66cc66;">&#40;</span><a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html"><span style="color: #aaaadd; font-weight: bold;">String</span></a> username<span style="color: #66cc66;">&#41;</span>
                         <span style="color: #000000; font-weight: bold;">throws</span> UsernameNotFoundException, DataAccessException <span style="color: #66cc66;">&#123;</span>
              <a href="http://www.google.com/search?sitesearch=java.sun.com&amp;q=allinurl%3Aj2se%2F1+5+0%2Fdocs%2Fapi+List"><span style="color: #aaaadd; font-weight: bold;">List</span></a> results = getHibernateTemplate<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">find</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;from CustomUser where username = ?&quot;</span>,
                                                                        <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?sitesearch=java.sun.com&amp;q=allinurl%3Aj2se%2F1+5+0%2Fdocs%2Fapi+Object"><span style="color: #aaaadd; font-weight: bold;">Object</span></a><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#123;</span>username<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
              <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>results.<span style="color: #006600;">size</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> &amp;lt; <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                     <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> UsernameNotFoundException<span style="color: #66cc66;">&#40;</span>username + <span style="color: #ff0000;">&quot;not found&quot;</span><span style="color: #66cc66;">&#41;</span>;
               <span style="color: #66cc66;">&#125;</span>
               <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #66cc66;">&#40;</span>UserDetails<span style="color: #66cc66;">&#41;</span> results.<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
       <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre>
<p>Well, its that simple. We just implement loadUserByUsername(String username) function and inside that we query for the specified user using the HibernateTemplate</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6961884887741817";
/* 468x15, created 1/25/10 */
google_ad_slot = "2118617107";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<p><strong>Configuration</strong></p>
<p>It’s time to tell Spring Security to use our custom UserDetails and UserDetailsService. Ensure that petclinic application is using Hibernate Context file (applicationContext-hibernate.xml). Check the same in web.xml. Let’s take a look at what’s the configuration we need to do</p>
<pre class="xml">&nbsp;
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;b:bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;userDetailsService&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;com.ap.user.HibernateUserDetailsService&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
		<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;b:property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;hibernateTemplate&quot;</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;hibernateTemplate&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/b:bean<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;authentication-provider</span> <span style="color: #000066;">user-service-ref</span>=<span style="color: #ff0000;">&quot;userDetailsService&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/authentication-provider<span style="font-weight: bold; color: black;">&gt;</span></span></span>
 </pre>
<p>Add the bean definition and update the authentication-provider configuration to use our UserDetailsservice. Restart and Spring Security shall use our Custom UserDetails.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6961884887741817";
/* 468x15, created 1/25/10 */
google_ad_slot = "2118617107";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<p><strong>Troubleshooting</strong></p>
<p>It took me a while before I could implement this, and though of sharing the mistakes, so that you don’t repeat them</p>
<p>•<span> </span>Test User/Authority Hibernate class in isolation – I did it initially and my test suite was not complete as to detect that all the authorities were being set for the user. This let me to dig into the Spring Security code, add traces and realizing that it’s my mistake.</p>
<p>•<span> </span>Ensure that you maintain roles with a prefix ROLE_ (case-sensitive). If you choose otherwise, you have to set the prefix as desired for authorization.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6961884887741817";
/* 468x15, created 1/25/10 */
google_ad_slot = "2118617107";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ashishpaliwal.com/blog/2009/02/spring-security-%e2%80%93-implementing-custom-userdetails-using-hibernate/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
