{"id":1641,"date":"2012-10-30T13:03:44","date_gmt":"2012-10-30T05:03:44","guid":{"rendered":"http:\/\/rmohan.com\/?p=1641"},"modified":"2012-10-30T14:49:02","modified_gmt":"2012-10-30T06:49:02","slug":"password-protected-directory-in-tomcat","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=1641","title":{"rendered":"password protected directory in tomcat"},"content":{"rendered":"<p>How to protect a web directory with a password? if we are using\u00a0Apache, we can do it easily with .htaccess. It will prompt user for credentials while entering the\u00a0directory. But how to protect a directory with password in tomcat\u00a0web server? In this post we will discuss how to do it with tomcat Realms. This example was tested in tomcat 7 and tomcat 6.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Steps :<\/strong><br \/> 1) Add user, password and role in conf\/tomcat-users.xml<br \/> 2) In the webapps\/examples\/WEB-INF\/web.xml specify role, method and urls.<br \/> 3) Restart Tomcat and check.<\/p>\n<p><strong>Step 1:<\/strong><br \/> in vi conf\/tomcat-users.xml<\/p>\n<blockquote>\n<p>&lt;tomcat-users&gt;<br \/> &lt;role rolename=&#8221;webadmin&#8221;\/&gt; \u00a0\/\/webadmin is the rolename of the users who can access the application<br \/> &lt;user username=&#8221;randeep&#8221; password=&#8221;randeep&#8221; roles=&#8221;webadmin&#8221;\/&gt;<br \/> &lt;\/tomcat-users&gt;<\/p>\n<\/blockquote>\n<p><strong>Step 2:<\/strong><\/p>\n<blockquote>\n<p>&lt;security-constraint&gt;<br \/> &lt;display-name&gt;Example Security Constraint&lt;\/display-name&gt;<br \/> &lt;web-resource-collection&gt;<br \/> &lt;web-resource-name&gt;application&lt;\/web-resource-name&gt;<br \/> &lt;url-pattern&gt;\/*&lt;\/url-pattern&gt; \/\/applicable toall urls in the application<br \/> &lt;http-method&gt;DELETE&lt;\/http-method&gt;<br \/> &lt;http-method&gt;GET&lt;\/http-method&gt;<br \/> &lt;http-method&gt;POST&lt;\/http-method&gt;<br \/> &lt;http-method&gt;PUT&lt;\/http-method&gt;<br \/> &lt;\/web-resource-collection&gt;<br \/> &lt;auth-constraint&gt;<br \/> &lt;role-name&gt;webadmin&lt;\/role-name&gt;<br \/> &lt;\/auth-constraint&gt;<br \/> &lt;\/security-constraint&gt;<\/p>\n<\/blockquote>\n<blockquote>\n<p>&lt;login-config&gt;<br \/> &lt;auth-method&gt;BASIC&lt;\/auth-method&gt; \/\/Authentication type<br \/> &lt;realm-name&gt;application&lt;\/realm-name&gt;<br \/> &lt;\/login-config&gt;<\/p>\n<\/blockquote>\n<p><strong>Step 3:<\/strong><br \/> Restart tomcat<br \/> \/etc\/init.d\/tomcat restart<br \/> or<br \/> bin\/shutdown.sh<br \/> bin\/startup.sh<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to protect a web directory with a password? if we are using Apache, we can do it easily with .htaccess. It will prompt user for credentials while entering the directory. But how to protect a directory with password in tomcat web server? In this post we will discuss how to do it with tomcat [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/1641"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1641"}],"version-history":[{"count":5,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/1641\/revisions"}],"predecessor-version":[{"id":1644,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/1641\/revisions\/1644"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1641"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1641"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1641"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}