How to restrict access to web applications in Tomcat

By ITworld tips  Add a new comment

You can create a realm in Tomcat, a container-managed authentication mechanism that allows you to protect all or part of your webapp by requiring a username and password before requests can be processed. To create a realm, you take the following steps:

1. In your Tomcat instance's conf/server.xml file, configure the element to require authentications for requests destined for your webapp or host, and configure the element to tell Tomcat where to look for user accounts and password information.

[ See also: How to configure Tomcat to always require HTTPS ]

2. In your webapp's WEB-INF/web.xml file, configure the security settings, including which URIs to secure, which authentication method to use (BASIC, DIGEST, FORM, or CLIENT-CERT), and whether to always use HTTPS.

Example:

By default, Tomcat includes a UserDatabase resource preconfigured in conf/servlet.xml:


    <Resource name="UserDatabase" auth="Container"
        type="org.apache.catalina.UserDatabase"
        description="User database that can be updated and saved"

    factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
        pathname="conf/tomcat-users.xml" />

This resource stores and retrieves user account information in conf/tomcat-users.xml. To declare a realm that uses this resource, you add a element, typically just below the element that configures your webapp:


    <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
        resourceName="UserDatabase"/>

Next, you add a element to the to link the context to the realm:


    <Context path="" docBase="/opt/webapps/secretweb">
        <!-- Link to the user database we will get roles and users from. -->
        <ResourceLink name="users" global="UserDatabase"
            type="org.apache.catalina.UserDatabase"/>
    </Context>

Tomcat is now configured to use the realm UserDatabaseRealm. Next, you configure your webapp's web.xml file like this:


    <security-constraint>
        <web-resource-collection>
            <web-resource-name>Top Secret Stuff</web-resource-name>
            <url-pattern>/*</url-pattern>
        </web-resource-collection>
        <auth-constraint>
            <role-name>secretagent</role-name>
        </auth-constraint>
    </security-constraint>
    <login-config>
        <auth-method>BASIC</auth-method>
        <realm-name>Top Secret Stuff</realm-name>
    </login-config>
    <security-role>
        <description>Roles that each qualify a user to authenticate.
        </description>
        <role-name>secretagent</role-name>
    </security-role>

This configuration specifies that any request destined for the webapp causes Tomcat to send a BASIC authentication challenge, which requires users to authenticate with a username and password. It also restricts access to users whose accounts have the "secretagent" role. You can grant users this role by configuring conf/tomcat-users.xml as follows:


<tomcat-users>
  <role rolename="secretagent"/>
  <user name="greg" password="007" roles="secretagent"/>
  <user name="ed" password="mycat" roles="secretagent"/>
  <user name="ken" password="mule" roles="secretagent"/>
</tomcat-users>

After you have finished configuring realms, resources, security, and users, restart Tomcat and try accessing the webapp. You should be prompted for a username and password.

For more information on how realms work and how to configure them, see http://tomcat.apache.org/tomcat-6.0-doc/realm-howto.html. For information on how the default configuration works, see the MemoryRealm page at http://tomcat.apache.org/tomcat-6.0-doc/realm-howto.html#MemoryRealm.

Do you have a question on installing, configuring, or deploying Apache Tomcat? Submit it here.

___________________

Today's tip was provided by sdozen12 on behalf of MuleSoft

Want to cash in on your IT savvy? Send your tip to tips@itworld.com. If we post it, we'll send you a $25 Amazon e-gift card.

 

ITworld LIVE

DevelopmentWhite Papers & Webcasts

Webcast On Demand

How to Distribute Apps to Your Mobile Workforce

When considering enterprise app deployment, you may find some unexpected challenges and a number of options that range from simple distribution to running your own enterprise market. How can you determine the best approach for your organization? MOTODEV for Enterprise can help you understand and evaluate current enterprise deployment technologies and learn best practices that support your choice.

Sponsor: Motorola Mobility

Webcast On Demand

Authentication, Certificates and VPNs

MOTODEV for Enterprise can help get you up to speed quickly on key topics such as how to enable secure access to a company intranet from outside the firewall. This webinar provides a clear explanation of terms and technologies and what they can do for your enterprise app development.

Sponsor: Motorola Mobility

Webcast On Demand

Improving Enterprise App Quality with MOTODEV App Validator

MOTODEV for Enterprise supports quality app development for businesses, government, and institutions with technical resources and tools such as the MOTODEV App Validator, a free static analysis testing tool.

Sponsor: Motorola Mobility

White Paper

HR Analytics: Driving Return on Human Capital Investments

In today's economy, it's critical for organizations to make employee retention and development a major business focus, to ensure that valuable employees are not lost as the economy improves. With advanced BI solutions, organizations can be supported by workforce analytics to drive return on human capital investment and to see the value the workforce delivers to organizational performance. This white paper demonstrates how the increased power of having metrics and analytic insight can align core HR business processes with organizational goals and strategies and help ensure organizations make the right business decisions today for tomorrow.

White Paper

Positioning the CIO as a Powerful Business Partner with IT Portfolio Governance

In this whitepaper, learn how you can become a visionary portfolio manager and transform IT into a streamlined revenue and profit center.

See more White Papers | Webcasts

Ask a question

Ask a Question