File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Tomcat and the fly likes Tomcat Custom Realm Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Tomcat Custom Realm" Watch "Tomcat Custom Realm" New topic

Tomcat Custom Realm

Carlos Lacerda
Ranch Hand

Joined: Apr 09, 2003
Posts: 34
Hi, some knows where meeting an example of as to create a JDBCrealm or DataSourceRealm or one Realm custom ?
I have my rules to effect login in a necessary DB therefore to customize.


Carlos Lacerda, Brasil
[SCJP 1.4-5.0] [SCJD] [SCWCD] [SCEA] [OCA-OAS 10g]
Muhammed Patel

Joined: Feb 26, 2009
Posts: 5
I am trying to do the same thing.

Have a look at the following link:

cheers ,

In my world they are all ponies and eat rainbows and poop butterflies
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Tomcat comes along with excellent documentation.

Start here:
Muhammed Patel

Joined: Feb 26, 2009
Posts: 5
Thanks Bauke.

I had a look at that documentation first. Only problem was i was developing a custom implementation of the RealmBase class as our authentication structure is a little different and did not find my answer there.

Buts it's ok now i have just figured it out.

Thanks again
Ofri Dagan

Joined: Mar 21, 2010
Posts: 10
I'm trying to write my own Realm to authenticate my users. I have written a class extending org.apache.catalina.realm.RealmBase, compiled to a .jar file and put it in the /lib directory. Then I added this to server.xml:

<Realm className=""

Tomcat doesn't seem to "see" my new jar... When I start Tomcat I get ClassNotFoundException

Can someone tell me what is the reason Tomcat can't find MyRealm?
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42959
Tomcat does not have a "/lib" directory, so where did you put the jar file? If you meant the WEB-INF/lib directory of a web app, then be aware that Realm implementations need to go into Tomcat's classpath, not your web app's classpath. Try the "common" or "shared" directories instead.
Ofri Dagan

Joined: Mar 21, 2010
Posts: 10
mmm actually tomcat 6 does ships with a lib folder.
I finally figured I had something wrong with the way I created my jar. It works now.
Thanks, and sorry...
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17257

And another zombie lurches back to life!

Ulf, you're thinking Tomcat 5 and earlier. They got rid of all that common/shared lib stuff in Tomcat6. now it's just TOMCAT_HOME/lib.

For the benefit of anyone wondering, by the way, it's easy to subclass the Tomcat database realm class. I did it because I needed Windows-style authentication (case-insensitive user name, case-sensitive password). My subclass folds the userid to lower case before querying the database.

An IDE is no substitute for an Intelligent Developer.
Zarkasih Adlan

Joined: Mar 24, 2011
Posts: 1
Thanks guys for this thread! I had to extend the DataSourceRealm just to change one line in the digest() method because my organisation uses a very obscure encoding method. I followed all the instructions on the manual (and in this thread), but was stuck for days due to a ClassNotFoundError. But, alas it's my own stupid mistake. I added this to my context.xml:

<Realm className="com.mycompany.CustomDataSourceRealm" debug="0"
dataSourceName="jdbc/mydb" digest="MD5" localDataSource="true"
userTable="table1" userNameCol="id" userCredCol="cred"
userRoleTable="table2" roleNameCol="role" />

I'm a newbie and never had to create a jar before. So, what I did was jar -cvf CustomDataSourceRealm.jar CustomDataSourceRealm.class. This didn't capture the class package information, hence the class not found. So what resolved the issue was simply to jar -cvf CustomDataSourceRealm com, where com is the package folder such that the class is in com/mycompany/CustomDataSource.class. Yes, a very basic mistake, I'm putting it out here so other people won't repeat my mistake.

Next, up I'm trying to configure the container in such a way that it takes in a third parameter for authentication. I know I need to mess with mbeans-descriptor.xml abit, I'm not to sure if I need to extend GenericPrincipal. But that's another question for another thread. Any pointers would help too.
I agree. Here's the link:
subject: Tomcat Custom Realm
It's not a secret anymore!