This post goes into the nitty grittys of setting up user authentication in Jetty using realms. I had a tough time getting it done. So thought would share the details here.

Let us say that only users who have the role user assigned to them should be able to access the page foo.jsp. Let us now go about doing this in Jetty. Your web.xml should have this.



<web-resource-name>File Upload</web-resource-name>


Your jetty.xml should have the following

<Set name=”UserRealms”>
<Array type=””>
<New class=””>
<Set name=”name”>Auth</Set>
<Set name=”config”><SystemProperty name=”jetty.home” default=”.”/>               /etc/</Set>
<Set name=”refreshInterval”>0</Set>

Let us add one user having user name foo and password bar whose role is admin to file. Add the following line to file.

foo: bar,user

That is it. You are all set to go. We have put the password in plain text format. But a MD5 hash of the password can also be used. The realm name in web.xml file should exactly match the one in jetty.xml file. In our case it is Auth. This is how Jetty maps the realms. Also the role names in web.xml file should exactly match the one in file. In our case, it is user. Be careful to see that they are of the same case. If, in web.xml, you have defined the role as User and in properties file you have given it as user, it will not work.

In web.xml, do not include the context name of the application in url pattern. Say, your web application context name is SomeWorldChangingApp. If you have put the url in web.xml as below


Will not work. The correct pattern is


Jetty also provides JDBC realm which I have not explored.