Securing a JBoss Web Application
Join the DZone community and get the full member experience.Join For Free
this articles describes how to secure a java web application in jboss using basic authentication.
step1: edit web.xml in your application
edit the web.xml file in your webapp at the following location:
edit your web.xml and put the following contents (generally towards the bottom of the file)
<description>protects all resources</description>
<realm-name>authorized access only.</realm-name>
this is a way of telling the container to restrict all urls to any user with the role ‘ myrole ‘.
step 2: create jboss-web.xml in your application
edit or create the jboss-web.xml file in your webapp at the following location:
put the following contents:
this tells jboss to use application policy ‘ myappname’ for this application.
step 3: create application policy on jboss server
we now need to define the application policy ‘ myappname ‘ on jboss server.
edit the login-config.xml file in the jboss server directory at the following location:
edit the contents of login-config.xml and add an application policy as follows:
<!-- application policy for myappname -->
<login-module code="org.jboss.security.auth.spi.usersrolesloginmodule" flag="required">
this tells jboss to user ‘usersrolesloginmodule’ which uses property files to store users and roles.
step 4: create users on jboss server
now we create a new user with the role ‘myrole’.
create a new user
edit the users.properties file used by your application policy in step 3.
add a line to create a new user as follows.
finally, we assign the role ‘myrole’ to the user ‘myuser’. edit the following file
create a new role
edit the roles.properties file used by your application policy in step 3.
add a line to create a assign the role ‘myrole’ to ‘myuser’ as follows.
test your settings
restart the jboss server and deploy your application. when you access your application, you should see a basic authentication popup.
if your setup is correct, you should be able to login using ‘myuser’ and ‘mypassword’ as defined in the step 4.
this approach is equivalent to defining users, passwords and roles in tomcat-users.xml. while this is an easy approach and helps you get started, a real production web application should not store its passwords unencrypted on disk. we used basic authentication in this example. for production quality applications, you should use digest , form or client-cert.
Opinions expressed by DZone contributors are their own.