NOTE: Updated in November 2013.
In part 2 of this tutorial, I demonstrated how to use Apache Shiro (formerly JSecurity and also called Ki) to add basic security to a web application. In part 3, I show how to configure Shiro to secure different parts of the web application based on a user's role.
One of Shiro's many features is an ability to restrict areas of a web application to be available not just to authenticated (logged in) users but to authenticated users that have a specific role such as admin.
Part 3 Example Application
You can download the part 3 example project, which is an archived Maven project. This example is based on the example project built in the tutorial parts 1 and 2. Be sure you read parts 1 and 2 of the tutorial (links at the bottom of this page) and that you've successfully configured the Derby database (see part 1).
In part 3's example project, I reconfigured the web folders into a secure folder (secure/index.jsp) and an admin folder (admin/index.jsp and admin/users.jsp). Users who are logged in and have a role of user can access the JSPs in the secure folder, but cannot access the JSPs in the admin folder. Only users logged in and have a role of admin can view the pages in the admin folder.
Unzip the downloaded role security file and then import the Maven project into Eclipse.
Be sure to update the path to the securityDB in context.xml before building the .war file or running the Maven goal mentioned below.
You can use the Maven Tomcat plugin (see reference below for how to install Maven if you've don't already have Maven) to run the web application. Just open a command window and navigate to where you unzipped the rolesecurity.zip download. Make sure you're in the rolesecurity directory. Then do the following:
mvn -e clean tomcat7:run-war
Once you see [INFO] Started Servlet Engine in the command window, open your web browser and go to this URL: http://localhost:8080/rolesecurity/ . You should see the contents of the index.jsp. To stop the Tomcat server type control-c in the command window.
Since this web application has security based on user roles you should NOT be able to open the web pages that are in the admin folder (admin/index.jsp and admin/users.jsp) without first logging in as user email@example.com and bruce (password). If you log in using user firstname.lastname@example.org and sue (password), you can only view the pages in the secure folder.
If you try to visit a web page for which you don't have the correct role, you'll be redirected to /unauthorized.jsp.
Configuring Shiro To Use Role Security
To add security based on roles, I did the following:
Add a database table named user_roles with columns named username and role_name to the Derby database. I inserted records into this table for my two users (email@example.com and firstname.lastname@example.org). Bruce has a role of admin and a role of user. Sue only has a role of user. Giving Bruce both roles enables him to log in and visit pages located in both the secure and admin folders.
Because I'm following Shiro's defaults for where it looks for user roles when using a realm based on a database, my table had to be named user_roles and the columns in the table had to be named username and role_name. If your project cannot follow Shiro's defaults, you can configure Shiro to use your projects conventions (see the Shiro references below).
Note Shiro will automatically query the user_roles table when a a user logs in to determine what roles to associate with the authenticated user.
Then I had to change the configuration for IniShiroFilter (see part 2) in web.xml. Here's the code that changed:
roles.unauthorizedUrl = /unauthorized.jsp
/secure/** = authc, roles[user]
/admin/** = authc, roles[admin]
The changes to part 2's web.xml are in red.
The roles.unauthorizedUrl filter specifies where the IniShiroFilter should forward the user to if he tries to access a web page but doesn't have the correct role.
The line /secure/** = authc, roles[admin] tell the IniShiroFilter to restrict the JSPs in the /secure/ folder to only users logged and that have a role of user. The line after that one does the same for the admin folder, but a logged in user must have a role of admin to visit those pages. Note you CANNOT do: /secure/** = authc, roles[user,admin] to indicate that users with either the admin or the user role can view pages in the secure folder.
In my Servlet GetAllUsers, I now needed to verify that the user making the request is logged in and has the role of admin. If you examine the code in the doPost method of GetAllUsers you'll see that I just needed to call the Subject class's hasRole method. This method returns true if the Subject has the role passed in as a parameter, otherwise it returns false.
It was simple to refactor the part 2 web application to use Shiro's role security. Please note that I'm only scratching the surface of Shiro's capabilities. Consult the references below for much more information. When you download Shiro, you'll receive several sample applications that use more advanced features. Also be sure to review the JavaDoc for the Shiro classes as they contain much useful information.
In part 4 of the tutorial, I'll explain how to use some of the custom tags provided by Shiro.
- An Introduction to Shiro (formerly JSecurity) – A Beginner's Tutorial Part 2, http://www.brucephillips.name/blog/index.cfm/2009/4/5/An-Introduction-to-Ki-formerly-JSecurity--A-Beginners--Tutorial-Part-2
- Role Security Example Application, http://www.brucephillips.name/jsecurity_examples/rolesecurity_mvn.zip
- Apache Shiro http://shiro.apache.org/
- Apache Shiro API, http://shiro.apache.org/static/current/apidocs/
- Apache Shiro Mailing Lists, http://shiro.apache.org/mailing-lists.html
- Presentation on JSecurity to the Charlotte Java Users Group, http://www.jsecurity.org/files/JSecurity.pdf
- Apache Derby, http://db.apache.org/derby/
- Apache Tomcat, http://tomcat.apache.org/
- Jetty, http://jetty.mortbay.org/jetty5/index.html
- Apache Software Foundation, Apache incubator, http://incubator.apache.org/projects/ki.html
- Maven: The Definitive Guide, http://www.sonatype.com/books/maven-book/reference/public-book.html
- Developing with Eclipse and Maven, http://www.sonatype.com/books/m2eclipse-book/reference/index.html