Wednesday, January 23, 2008

Simple login for a web application running under JBoss

Description
How can I realize a simple login for a web application running under JBoss?

Solution
We assume that the username for the login is admin, the password also and the role is Administrator.
  1. Insert the following tags in the file web.xml in the directory WEB-INF of the web application:

    <security-constraint>
    <web-resource-collection>
    <web-resource-name>WebApplication</web-resource-name>
    <url-pattern>/*</url-pattern>
    <http-method>GET</http-method>
    <http-method>POST</http-method>
    </web-resource-collection>
    <auth-constraint>
    <role-name>Administrator</role-name>
    </auth-constraint>
    </security-constraint>

    <login-config>
    <auth-method>BASIC</auth-method>
    <realm-name>Web Application</realm-name>
    </login-config>

    <security-role>
    <description>The role required to access the WebApplication.</description>
    <role-name>Administrator</role-name>
    </security-role>

  2. Create a file jboss-web.xml in WEB-INF directory of the web application with the following content:

    <security-domain>java:/jaas/WebApplication</security-domain>
  3. Create a file user.properties in the JBoss server directory conf\props with the following content:

    admin=admin
  4. Create a file roel.properties in the JBoss server directory conf\props with the following content:

    admin=Administrator
  5. Add the entry below to the file login-configuration.xml in the JBoss server directory conf.

    <application-policy name="WebApplication"> <authentication> <login-module code="org.jboss.security.auth.spi.UsersRolesLoginModule" flag="required"> <module-option name="usersProperties">props/users.properties</module-option> <module-option name="rolesProperties">props/roles.properties</module-option> </login-module> </authentication> </application-policy>

No comments: