Tuesday, October 9, 2012

Basic authentication mechanism in JBOSS

Name implies that this is the basic authentication mechanism type in JBOSS. When we trying to access to secured resource, then application will ask about username and password. If username and password are correct, then user can access to the resource. Lets see how to enable basic authentication for a resource.

First create an enterprise application like earlier discussed. Then we have to do some configurations. So first open the web.xml file under configuration files of testApp-war. Then add following code within <web-app> tag.

<web-resource-name>Admin Pages</web-resource-name>
<description>Only allow users from following roles</description>
    <realm-name>My Secure Content Authentication</realm-name>

In the URL pattern tag you have to specify the URL of the secured webpage. You can specify more than
one URL pattern. A URL pattern beginning with ‘/’ and ending with ‘/*’ specifies a path mapping and ‘*.’
Specifies an extension mapping. String containing only ‘/’ specifies default server while all other strings
denote the exact matches.
<role-name> is the allowed rolls for secured resource.
Then we have to edit jboss-web.xml file also. That config file also locate in the previous directory. Open the jboss-web.xml file and add following code inside <jboss-web> tag.


Now we need to create a security domain in login-config.xml file which located under following directory.


Then add following application policy into login-config.xml

<application-policy name = "testPolicy">
          <login-module code="org.jboss.security.auth.spi.UsersRolesLoginModule"
             flag = "required">
           <module-option name="usersProperties">props/testuser.properties</module-option>
           <module-option name="rolesProperties">props/testrole.properties</module-option>

Now we need to create two more properties file to achieve Basic authentication. Therefore go to following directory and create to properties files called testuser.properties and testrole.properties.


then add following username names and passwords to testuser.properties file.


akila is the username and the password is akila.

Then add following username with the role inside the testrole.properties file.


akila is the username and admin is the role of akila.

now we need to test basic authentication. So go to web browser and request to protected resource. Now you will see a pop-up box that asking about user name and password like below. Then enter your username and password. Now you can access your protected resource. 


No comments:

Post a Comment