Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Restrict directory access in Apache Geronimo

Using J2EE declarative security

Tyler Anderson recieved both his B.S. in Computer Science in 2004 and his M.S. in Electrical and Computer Engineering in 2005 from Brigham Young University. Tyler has written and coded numerous articles and tutorials for IBM developerWorks and DevX.

Summary:  Developing secure applications and protecting data are priorities in today's environment. Learn how to add another layer of security to your system by restricting directory access in the Apache Geronimo application server using Java™ 2 Platform, Enterprise Edition (J2EE) declarative security. Using a simple Web application as your example, this article demonstrates how you can control directory access by adding security constraints in the web.xml file and by specifying what roles or users, if any, are allowed access.

Date:  01 Nov 2005
Level:  Introductory

Activity:  4608 views
Comments:  

Introduction

The development of the Apache Geronimo application server is steadily advancing. As a result, more enterprise application developers are starting to take advantage of its benefits -- including its recent official J2EE 1.4 certification.

As with any Web server, protecting your directory by restricting access to it from arbitrary Web browsers is an essential part of security. This article shows you how to create a simple Web application that allows users to visit, log in, and then gain access to other areas on the site using Apache Geronimo. The article is geared toward small- to medium-sized applications, because it implements simple security by using the properties file login module. Directory access is controlled by deploying GBeans that specify a security deployment plan on Geronimo. Your application extends this deployment plan in the geronimo-web.xml file and creates security constraints in your web.xml file. This article uses the Apache Geronimo M5 release and assumes that you have no prior knowledge of Geronimo.


Get started with Apache Geronimo

To use Geronimo, you need Java 2 Platform, Standard Edition (J2SE) 1.4.2_08 (or later, but before 1.5). After you have the Java code installed, download Geronimo M5 from Apache (see Resources for a link to the Web site). Select either the .zip file (for Microsoft® Windows or Linux® operating systems) or the .tar.gz file (for the Linux operating system).

After the download completes, decompress the .zip or .tar.gz file. Now you're ready to start the server.

Start the Geronimo server

Go to the root directory of your Geronimo distribution in a console window, and execute the server.jar file by typing:

java -jar bin/server.jar

The server starts to load all modules along with any other application modules (see Figure 1).


Figure 1. Starting Geronimo
starting geronimo

Now you're ready to create the application.


Introduce the application

This article contains an example application called XYZ Enterprise example application, which you can download. The application requires users to log in before they can access content. The site is composed of three easy-to-use Web pages. An index page at the root of the site acts as the main page of the Web site, a downloads area, and an area for administrators (hereafter called admin). First you need to learn the directory structure of the application.

Understand the directory structure

The application has a directory structure that holds all the elements of the application. Create a directory called secApp. This is the root directory of your application. Then create the following subdirectories to secApp:

  • downloads (the downloads area)
  • restricted (the admin area)
  • verify (contains the login form and the invalid user/password page
  • WEB-INF (contains .xml files needed by Geronimo for your application

The remainder of this article covers the contents of each of these directories.

Start page

The start page -- the first page shown when a visitor comes to the Web site -- points to other sections and activities available for users. Create a file called index.html at the root directory of your application (./secApp/index.html). Add HTML code to it, as shown in Listing 1.


Listing 1. The start page
<html><head><title>TylerCo.</title></head>
<body>
<h1>Welcome to TylerCo.</h1>
There's a lot to do on this Web site.<br>
The user's downloads area is <a href="downloads/">here</a>.<br>
Administrators can log in <a href="restricted/">here</a>.
</body>
</html>

This page displays two links that take users to the downloads area, and it also provides a link for admin users to log in. See Figure 2 for example browser output.


Figure 2. Displaying the start page
Displaying the start page

Now let's look at the downloads area.

Downloads area

The downloads area allows users to view pages or download available content. Create a file called index.html, and place it in the downloads directory. Add the HTML code, as shown in Listing 2.


Listing 2. The downloads area
<html><head><title>Welcome to TylerCo.</title></head>
<body>
<h1>DOWNLOADS AREA</h1>
Welcome to the downloads area for registered users only!<br>
Go back to the <a href="../">main page</a>.
</body>
</html>

This page can be extended to contain links of files for download or other pages for users to view. A link to return to the main page is also displayed. See Figure 3 for example browser output of the downloads area.


Figure 3. Displaying the downloads area
Displaying the downloads area

Now let's look at the admin area.

Admin area

The admin area exists to represent an area that would allow only users with administrative access to enter and perform any admin-related activities, like adding or approving new content for users. Create another file called index.html, and place it in the restricted directory (see Listing 3).


Listing 3. The admin page
<html><head><title>ADMIN</title></head>
<body>
<h1>HELLO ADMIN</h1>
What would you like to do?<br>
Go back to the <a href="../">main page</a>.
</body>
</html>

This page can be extended to contain links that allow administrators to add, edit, approve, or remove content from the downloads area. See Figure 4 for example browser output of the downloads area.


Figure 4. Displaying the admin page
Displaying the admin page

Next, find out how to set up security.


Set up security

Geronimo secures directory access to sensitive data through a series of files. The first, a deployment plan defined by my-realm.xml, specifies a deployment plan your application adheres to in securing access. The web.xml file lists constraints and security roles that exist in your application as well as the roles that are allowed access to a certain directory. The geronimo-web.xml file defines which users or group of users comprise the security roles defined in the web.xml file.

GBeans

A GBean is Geronimo's unique way of packaging objects, managing their life cycles, and expressing dependencies. In fact, the Geronimo kernel is totally independent of the specific modules loaded in the server. This modular approach makes Geronimo a highly scalable application server. A GBean is the interface that connects your deployment plan to Geronimo's kernel. GBeans have state, and they communicate and interact with other GBeans and the kernel, which is how your application authenticates users. Upon submitting a username and password, Geronimo interacts with your GBeans to authenticate a visiting user.

Next you configure your deployment plan using GBeans.

Configure a deployment plan

Your deployment plan is deployed and connected to Geronimo's kernel using GBeans. The file my-realm.xml defines three GBeans that coordinate authentication type, login usage, and the security realm users log in to. Create the file my-realm.xml, place it in the WEB-INF directory, and begin to define it, as shown in Listing 4.


Listing 4. The login properties GBean
<?xml version="1.0" encoding="UTF-8"?>
<configuration
    xmlns="http://geronimo.apache.org/xml/ns/deployment-1.0"
    configId="com/ibm/geronimo/security/myRealm"
    parentId="org/apache/geronimo/Security"
    >

    <gbean name="my-login"
        class="org.apache.geronimo.security.jaas.LoginModuleGBean">
        <attribute name="loginModuleClass">
org.apache.geronimo.security.realm.providers.PropertiesFileLoginModule
        </attribute>
        <attribute name="serverSide">true</attribute>
        <attribute name="options">
            usersURI=var/security/my_users.properties
            groupsURI=var/security/my_groups.properties
        </attribute>
        <attribute name="loginDomainName">my-realm</attribute>
    </gbean>
...

First you specify the configId of the deployment plan in the configuration tag, which extends the org.apache.geronimo.Security class specified by the parentId. Your application links to this deployment plan in the geronimo-web.xml file.

The first GBean (my-login) specifies the registered users and groups that are allowed directory access as defined in their respective property files. These files are checked when a user logs in and enters a username and password. This GBean is linked to the second GBean (my-realm, shown in Listing 5) through the loginDomainName attribute, specified in Listing 4.


Listing 5. The login domain name GBean (the realm)
...
    </gbean>
    <gbean name="my-realm"
      class="org.apache.geronimo.security.realm.GenericSecurityRealm">
        <attribute name="realmName">my-realm</attribute>
        <reference name="LoginModuleConfiguration">
            <name>my-login</name>
        </reference>
        <reference name="ServerInfo">
            <module>org/apache/geronimo/System</module>
            <name>ServerInfo</name>
        </reference>
        <reference name="LoginService">
            <module>org/apache/geronimo/Security</module>
            <name>JaasLoginService</name>
        </reference>
    </gbean>
...

This my-realm GBean specifies the domain that users log in to and defines the System and Security modules. It links itself to the next GBean (my-login again) through the LoginModuleConfiguration, specified in Listing 6.


Listing 6. The login configuration GBean
...
    </gbean>
    <gbean name="my-login"
      class="org.apache.geronimo.security.jaas.JaasLoginModuleUse">
        <attribute name="controlFlag">REQUIRED</attribute>
        <reference name="LoginModule">
            <name>my-login</name>
        </reference>
    </gbean>
</configuration>

This last GBean (my-login) specifies login configuration properties. controlFlag specifies that for authentication to succeed, a valid result must be returned. This GBean links to the first GBean (same names) in Listing 4 through the LoginModule reference.

Next up is configuring security in the web.xml file.

Declarative J2EE security: web.xml

Now that you have the GBeans set up, you need to tie them to your application. Create a file called web.xml, place it in the WEB-INF directory, and begin to define it, as shown in Listing 7.


Listing 7. Specifying security roles
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
             http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
         version="2.4">

    <security-role>
      <role-name>registered-users</role-name>
    </security-role>

    <security-role>
      <role-name>administrators</role-name>
    </security-role>
...

This sets up the web.xml file by specifying the XML namespaces. However, security roles, shown in bold, are the focus here. There are two roles in your application: registered-users and administrators. Users in each role are specified later in the geronimo-web.xml file. Next, define the security constraints on directories, as shown in Listing 8.


Listing 8. Specifying security constraints
...
      <role-name>administrators</role-name>
    </security-role>

    <security-constraint>
      <web-resource-collection>
        <web-resource-name>Registered Users</web-resource-name>
        <url-pattern>/downloads/*</url-pattern>
      </web-resource-collection>
      <auth-constraint>
        <role-name>registered-users</role-name>
        <role-name>administrators</role-name>
      </auth-constraint>
    </security-constraint>
    
    <security-constraint>
      <web-resource-collection>
        <web-resource-name>Administrators Only!</web-resource-name>
        <url-pattern>/restricted/*</url-pattern>
      </web-resource-collection>
      <auth-constraint>
        <role-name>administrators</role-name>
      </auth-constraint>
    </security-constraint>
...

There are two constraints: one for the downloads directory and one for the restricted directory. The downloads directory gives both registered-users and administrators access, as defined within auth-constraint tags. Next you define the forms used for logging in, as shown in Listing 9.


Listing 9. Specifying login forms
...
      </auth-constraint>
    </security-constraint>

    <login-config>
      <auth-method>FORM</auth-method>
      <realm-name>Jetty Realm</realm-name>
      <form-login-config>
         <form-login-page>/verify/login.html</form-login-page>
         <form-error-page>/verify/loginError.html</form-error-page>
      </form-login-config>
    </login-config>
</web-app>

The form-login-page tag specifies the login page, while the form-error-login tag specifies the page to redirect users to when a username/password combination fails to authenticate.

Declarative J2EE security: geronimo-web.xml

You now have the three GBeans and the realm set up, and your security roles and constraints are defined. Now you need to define the role to user/group mappings. Create a file called geronimo-web.xml, place it in the WEB-INF directory, and define it, as shown in Listing 10.


Listing 10. Specifying role to user/group mappings
<?xml version="1.0" encoding="UTF-8"?>
<web-app
    xmlns="http://geronimo.apache.org/xml/ns/j2ee/web-1.0"
    xmlns:sec="http://geronimo.apache.org/xml/ns/security-1.0"
    configId="com/ibm/geronimo/security/myApp"
    parentId="com/ibm/geronimo/security/myRealm">

    <context-root>/security</context-root>
    <context-priority-classloader>false</context-priority-classloader>
    <security-realm-name>my-realm</security-realm-name>

    <security>
        <default-principal realm-name="my-realm">
            <principal class=
"org.apache.geronimo.security.realm.providers.GeronimoUserPrincipal"
                name="nobody"/>
        </default-principal>
        <role-mappings>
            <role role-name="registered-users">
                <realm realm-name="my-realm">
                    <principal class=
"org.apache.geronimo.security.realm.providers.GeronimoGroupPrincipal" 
                      name="registeredUsers" designated-run-as="true"/>
                </realm>
            </role>
            <role role-name="administrators">
                <realm realm-name="my-realm">
                    <principal class=
"org.apache.geronimo.security.realm.providers.GeronimoUserPrincipal" 
                        name="admin"/>
                    <principal class=
"org.apache.geronimo.security.realm.providers.GeronimoUserPrincipal" 
                        name="root"/>
                </realm>
            </role>
        </role-mappings>
    </security>
</web-app>

First, take a look at the configId and parentId values. The configId specifies the name of your application, com/ibm/geronimo/security/myApp, and extends the realm you configured earlier: com/ibm/geronimo/security/myRealm. Next you set up the context, which is the directory where your application is placed once deployed on Geronimo. Then the name of the realm that you're going to use is identified in the <security-realm-name> tag, which you already defined in the my-realm.xml file, and the mappings are defined in the <security> tag.

The default user mapping -- nobody -- is defined first. Then the role mappings are defined. Recall that you defined two security roles in the web.xml file. The first mapping maps all users in the registeredUsers group to the registered-users role. The second and last mapping maps the admin and root users to the administrators role. Next you'll define the users associated with the registeredUsers group as well as each user and their passwords.

Properties files

Remember the user and group properties files that you referenced in Listing 4? The usernames and passwords that your application references when someone logs in are going to be stored in a users.properties and a groups.properties file. The users.properties file stores the usernames and passwords, while the groups.properties file stores the group names and the users associated with each group.

Now you need to define the properties. First, create a file called my_users.properties, and place it at the following location, rooted at your Geronimo distribution: ./var/security/my_users.properties. Define it as shown in Listing 11.


Listing 11. Defining users and their passwords
admin=imtheboss
root=icandeleteanything
bob=by
joe=schmoe
kris=sy
john=doe
nobody=nobody

You have now specified the users and the passwords that are validated against when users try to log in later. Groups of users can also be defined. Create a file, my_groups.properties, and place it in the same directory as the my_users.properties file you just created:

bosses=admin,root
registeredUsers=bob,joe,kris,john
nobody=nobody

The registeredUsers group is the group of interest, as it is the group associated with the registered-users role in your application. Thus, bob, joe, kris, and john are the registered users that can gain access to the downloads directory of your application.

Next you define the login pages.


Define the login pages

In Listing 9 you specified the forms used when a user requests access to either of the restricted directories. Now you get to define them.

Login page

Users are redirected to the login page when they click the downloads area link on the start page. Create a file, login.html, place it in the verify directory, and define it, as shown in Listing 12.


Listing 12. Login page
<html>
<h1>LOGIN</h1>
<form method="POST" action="j_security_check">
Username:<br>
<input size="20" name="j_username">
<br><br>
Password:<br>
<input size="20" name="j_password" type="password">
amp;nbsp;amp;nbsp;<input name="submit" type="submit" value="Login">
</form>
</html>

This form is written using J2EE security. A form with the j_security_check as the action tells Geronimo that authentication is form based. j_username specifies the username of the user requesting a security check, and j_password is the password. Submitting the form sends the data to Geronimo, which connects to your GBeans and authenticates the user. See Figure 5 for example browser output.


Figure 5. Displaying the login page
Displaying the login page

Next, define the login error page.

Login error page

This is the page that users are redirected to when their user/password combination fails to authenticate. Create a page, loginError.html, place it in the same directory as the login.html file. Then verify and define it, as shown in Listing 13.


Listing 13. Login error page
<html>
<h1>ERROR</h1>
UNABLE TO VERIFY:<br>
INVALID username or password.
</html>

A simple page is shown to users, communicating that they entered an invalid user/password combination (see Figure 6).


Figure 6. Displaying the error page
Displaying the error page

Congratulations, your application is complete! Next you test it by deploying it on Geronimo.


Test the application

Test your application by first deploying the security plan onto the server and then by deploying your application.

Deploying the deployment plan

Deploying and connecting the security deployment plan to Geronimo allows your application to later extend and use the security configuration contained therein. Go to the root directory of your Geronimo installation in a new console window. Type the following to deploy the deployment plan:

java -jar bin/deployer.jar --user system --password manager
     deploy ../secApp/WEB-INF/my-realm.xml

This command executes the deployer.jar file by passing in the default user and password combination and by executing the deploy command, passing in your my-realm.xml file as the file to deploy. Geronimo indicates a successful deployment by outputting:

Deployed com/ibm/geronimo/security/myRealm

It has taken the configId of your deployment plan, and deployed it on Geronimo! You can now use it in your application.

Deploying the application

Next you get to deploy and test your application on Geronimo. First, however, you need to archive it in a .war file. Open another console window, and go to the root directory of your application, the secApp directory. Create a .war file of your application by typing the following command:

jar cvf security.war downloads verify WEB-INF restricted index.html

This places all the files of your application into a .war file, security.war, which Geronimo reads. Deploy your application on Geronimo by typing:

java -jar bin/deployer.jar --user system --password manager
     deploy ../secApp/security.war

Your application is now deployed on Geronimo. Geronimo communicates successful deployment by outputting something similar to:

Deployed com/ibm/geronimo/security/myApp @
http://your-a9279112e3:8080/security

The application is deployed and ready for testing.

Testing the application

Test you application by visiting the Web site you just created. Open a Web browser, and point it to the URL http://localhost:8080/security/. The start page should load, as shown in Figure 2.

Now click the link that takes you to the user's downloads area. The login page should now load, as shown in Figure 5. Enter a username with password, as defined in the my_user.properties file you created earlier (see Figure 7).


Figure 7. Logging in
Logging in

After you click Login, the downloads page loads. Click the link to return to the main page. Now click the link that takes you to the administrators login area. Example browser output is shown in Figure 8.


Figure 8. Forbidden
Forbidden

Bob is allowed access to the downloads area, but because he isn't the admin or root user, he is forbidden access to the security directory. The registered-users role is working!

To test the administrators role, you need to log out by closing and reopening your browser. Doing so resets all the login information. Now you can test the administrators role. Click the link that takes you to the administrator login area (you may have to reload the page by pressing Shift and clicking Reload). This takes you to the same login page, except this time you enter a different username/password combination (see Figure 9).


Figure 9. Logging in as root
Logging in as root

The admin page loads as expected. Now go back to the main page, and go to the downloads area. The downloads area page also loads as expected. The administrators role is working!


Summary

Congratulations! You've learned how to create and deploy an application on the Apache Geronimo application server, securing directory content from unauthorized access. By allowing access only to certain users, you build in another level of security to your operations.



Download

DescriptionNameSizeDownload method
XYZ Enterprise example applicationsource.zip4 KB HTTP | Download Director

Information about download methods


Resources

Learn

Get products and technologies

Discuss

About the author

Tyler Anderson recieved both his B.S. in Computer Science in 2004 and his M.S. in Electrical and Computer Engineering in 2005 from Brigham Young University. Tyler has written and coded numerous articles and tutorials for IBM developerWorks and DevX.

Report abuse help

Report abuse

Thank you. This entry has been flagged for moderator attention.


Report abuse help

Report abuse

Report abuse submission failed. Please try again later.


developerWorks: Sign in


Need an IBM ID?
Forgot your IBM ID?


Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

Choose your display name

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.

 


Rate this article

Comments

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Open source, WebSphere
ArticleID=97495
ArticleTitle=Restrict directory access in Apache Geronimo
publish-date=11012005
author1-email=tyleranderson5@yahoo.com
author1-email-cc=troy@backstopmedia.com

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

For articles in technology zones (such as Java technology, Linux, Open source, XML), Popular tags shows the top tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), Popular tags shows the top tags for just that product zone.

For articles in technology zones (such as Java technology, Linux, Open source, XML), My tags shows your tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), My tags shows your tags for just that product zone.

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Try IBM PureSystems. No charge.

Special offers