|
Database Authentication and Authorization in J2EE Container Managed SecurityAn Oracle JDeveloper Article
IntroductionThis article explains how to leverage database tables for storing account information for user authentication and authorization of Java EE applications in JDeveloper 10.1.3.2 and OC4J 10.1.3.2. Container-managed security allows web applications to delegate authentication
and authorization decisions to the executing web container. In Oracle Containers
for J2EE (OC4J) the authentication and authorization decision is handled by
the OracleAS JAAS Provider (referred to in previous releases as the Oracle JAZN framework). Web applications running in OC4J
can be configured to authenticate users against Oracle Internet Directory (OID),
third-party LDAP, CoreId, This JDeveloper article explains how-to use the DBTableOraDataSourceLoginModule JAAS LoginModule in OC4J to implement container-managed authentication and authorization based on a database security provider. For guidance on how to create your own JAAS LoginModule that then can be configured for OC4J container managed security, see the Login Module Developer's Guide. Container Managed Security in OC4JIn container-managed security the application delegates authentication and authorization decisions to the J2EE container. The benefit of container-managed security in contrast to a pure JAAS approach (Java Authentication and Authorization Service) approach is that the application logic is not mixed with authentication and authorization code, which ensures portability.
Container-managed security protects web resources by their URL. If a
user requests a protected web resource, the J2EE container checks whether the
user is authenticated and otherwise delegates the request to the integrated
authentication service. The Oracle JAAS Provider authenticates users based
on a username and password pair or a client side x509 certificate. The
default authentication provider is either file-based, (
|
![]() |
Figure 2: Visual editor for the system-jazn-data.xml file of the embedded OC4J |
But how does embedded OC4J know that it should use DBTableOraDataSourceLoginModule for authentication and not the default Realm LoginModule? The application.xml file, located in the JDEVELOPER_HOME\jdev\system\oracle.j2ee.10.1.3.40.66\embedded-oc4j\config directory, can contain two properties as a switch that indicates that
the JAAS LoginModule provider should be used instead of the default Realm LoginModule.
<orion-application ...>
...
<jazn provider="XML">
<property name="role.mapping.dynamic" value="true"/>
<property name="custom.loginmodule.provider"
value="true"/>
</jazn>
...
</orion-application ...>
The role.mapping.dynamic property defines that J2EE security roles should
be read from the authenticated user subject, allowing the LoginModule
to add role grants to the authenticated user. The custom.loginmodule.provider property tells OC4J to use a custom JAAS LoginModule for authentication.
The application.xml file cannot be edited from within JDeveloper and
instead needs to be edited manually.
Note: Configuring the application.xml to authenticate
with custom LoginModules enforces this authentication type to be used
for all projects run by this JDeveloper instance. For this reason it is
recommended to set up a JDeveloper installation to build and test applications
that require JAAS LoginModule authentication.
The data-sources.xml file is used to define the J2EE data-source referenced
by the LoginModule to access the database tables for authentication.
The LoginModule configuration shown earlier has the following LoginModule
option defined
<option>
<name>data_source_name</name>
<value>jdbc/authschemaDS</value>
</option>
According to this configuration, the datasource name jdbc/authschemaDS is resolved by the LoginModule at runtime to connect to the database.
The data-source.xml file is expected to be part of the JDeveloper view layer
project an should be created manually as follows:
data-source.xml file that works with JDeveloper version 10.1.3 and
above.Finishing the wizard, creates a file data-sources.xml in the project's
META-INF directory. To populate the data-source.xml file with the
database connection expected by the LoginModule, create a named database connect
in JDeveloper.
jdbc/" and without the "DS"
strings in the Connection Name field on the first wizard
page. In the example used in this article, the name is "authschema".To populate the data-source.xml file with this information, choose
Tools | Embedded OC4J Server Preferences from the JDeveloper
menu. In the menu, expand the Current Workspace node
and select the Data Sources entry. Press the Refresh
Now button on the right hand side to copy all database connections
that exist in JDeveloper into the data-sources.xml file. From the list
of data sources created below the Data Sources entry,
delete all data source definitions that are not required for this application.
![]() |
Figure 3: data-sources.xml editing option |
In order to protect the database password, JDeveloper uses OC4J password indirection
for entries in the data-sources.xml file. Passwords in the data-sources.xml file may look similar to password="->DataBase_User_TNyMa-DJkD88CAzvVVLM8EX__YiKXy2P".
The indirected password points to a user account created in the project specific
jazn-data.xml file, located in the application - or workspace - root directory.
The jazn-data.xml file is not shown in the JDeveloper project hierarchy.
To add the jazn-data.xml file to the JDeveloper
project, so that it is visible to the application developer, do as follows:
jazn-data.xml file that works for JDeveloper version 10.1.3 and above.To populate the created jazn-data.xml file with the indirected
passwords from the data-sources.xml file, choose Tools
| Embedded OC4J Server Preferences from the JDeveloper menu. In the
menu, expand the Current Workspace node and select the Data
Sources entry. Press the Refresh Now button on the
right hand side to copy all database connections that exist in JDeveloper into
the data-sources.xml file. At the same time, the indirected passwords
are created in the jazn-data.xml file.
Note: If the password accounts aren't created, delete the data source entries shown under Data Sources node before pressing the refresh button.
Deploying an application that uses DBTableOraDataSourceLoginModule for
J2EE authentication is easier to a standalone OC4J instance than to the
embedded instance because JDeveloper automates many of the steps for you. The configuration
files that needs to be added or edited are
orion-application.xmldata-sources.xmlsystem-jazn-data.xmlThe orion-application.xml deployment descriptor is needed to define the
DBTableOraDataSourceLoginModule configuration. To create an orion-application.xml file in Oracle JDeveloper:
data-source.xml file that is required for JDeveloper version 10.1.3 and
above.Unlike the embedded OC4J server, in which the application name for the JAAS LoginModule is current-workspace-app, the application name for the standalone OC4J instance is the name of the deployed J2EE application. The application name can be defined when creating the deployment profile or within the View layer project's properties. To open the properties dialog, select the project node and choose Project Properties from the context menu. Select the J2EE Application entry to specify the J2EE application name and the name of the J2EE web context root.
![]() |
| Figure 4: J2EE Application definition in the View layer project properties |
In the example shown above, the application name of the deployed application
is secure-web-app. Edit the orion-application.xml file
to contain the LoginModule configuration:
<?xml version = '1.0' encoding = 'windows-1252'?>
<orion-application xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://xmlns.oracle.com/oracleas/schema/orion-application-10_0.xsd">
<jazn location="./jazn-data.xml" provider="XML"/>
<data-sources path="./data-sources.xml"/>
<jazn-loginconfig>
<application>
<name>secure-web-app</name>
<login-modules>
<login-module>
<class>oracle.security.jazn.login.module.db.DBTableOraDataSourceLoginModule</class>
<control-flag>required</control-flag>
<options>
<option>
<name>data_source_name</name>
<value>jdbc/authschemaDS</value>
</option>
<option>
<name>table</name>
<value>application_users</value>
</option>
<option>
<name>usernameField</name>
<value>username</value>
</option>
<option>
<name>passwordField</name>
<value>passwd</value>
</option>
<option>
<name>groupMembershipTableName</name>
<value>application_roles</value>
</option>
<option>
<name>groupMembershipGroupFieldName</name>
<value>role_name</value>
</option>
</options>
</login-module>
</login-modules>
</application>
</jazn-loginconfig>
</orion-application>
The LoginModule configuration itself hasn't changed compared to the embedded
OC4J usage, except that the configuration is added to the orion-application.xml and not to the system-jazn-data.xml file. Upon deployment, OC4J performs
the system-jazn-data.xml configuration and also sets the required properties:
<property name="role.mapping.dynamic" value="true"/>
<property name="custom.loginmodule.provider" value="true"/>
Note that t his configuration can be undone automatically when undeploying the J2EE web application. Furthermore standalone OC4J instances can run J2EE web application using both custom JAAS LoginModules for authentication and applications and the default Realm LoginModule authentication at the same time.
The DBTableOraDataSourceLoginModule is configured with a data source
reference to connect to the database. The data-sources.xml file can be
deployed with the J2EE web application or created after deployment using
the Enterprise Manager web application console of OC4J, which you can
access from http://server:8888/em.
Creating a data-sources.xml file in Oracle JDeveloper is explained
earlier in this document.
If the data-source.xml file is configured with password indirection,
which is the Oracle JDeveloper default, then the user account must be
added to the system-jazn-data.xml file of the OC4J instance. The system-jazn-data.xml file is located in the \j2ee\home\config directory of the OC4J instance.
There are two options available to add the account in OC4J.
In this scenario, you open the data-sources.xml file created in Oracle
JDeveloper and locate the data-source definition of the data source used
in the LoginModule configuration. In the example configuration above,
this is jdbc/authschemaDS.
The content of the data-sources.xml file in this example is
<connection-pool name="jdev-connection-pool-authschema"
disable-server-connection-pooling="false">
<connection-factory factory-class="oracle.jdbc.pool.OracleDataSource"
user="authschema"
password="->DataBase_User_H3qWButn9PYa0q-iROlsk52Spi7YzAG_"
url="jdbc:oracle:thin:@localhost:1621:ORCL"/>
</connection-pool>
<managed-data-source name="jdev-connection-managed-authschema"
jndi-name="jdbc/authschemaDS" connection-pool-name="jdev-
connection-pool-authschema"/>
<native-data-source name="jdev-connection-native-authschema"
jndi-name="jdbc/authschemaCoreDS" url="jdbc:oracle:thin:@localhost:1621:ORCL"
user="authschema" password="->DataBase_User_bsvQ9GfeZmRJ6W2XwC84fJaswBKa2prG"
data-source-class="oracle.jdbc.pool.OracleDataSource"/>
Note that the jdbc/authschemaDS entry points to the jdev-connection-pool-authschema,
which has its password redirected to the DataBase_User_H3qWButn9PYa0q-iROlsk52Spi7YzAG_ user account.
You can either keep this reference or change it to an existing username
in the system-jazn-data.xml file of the target OC4J instance. Depending
on your configuration in JDeveloper, the user account equivalent to DataBase_User_H3qWButn9PYa0q-iROlsk52Spi7YzAG_ contained in the project specific jazn-data.xml file, or the <workspace name>-jazn-data.xml in the root directory
of your JDeveloper workspace in the file system.
It is also possible to change the user account name after deployment using the EnterpriseManager web console of OC4J.
![]() |
| Figure 5: EnterpriseManager console in OC4J to define the password indirection for a data source |
This article explained how to configure the DBTableOraDataSourceLoginModule for the embedded and standalone OC4J. However, if things don't work as expected,
follow these steps to help identify the source of the problem.
The DBTableOraDataSourceLoginModule is a closed system in that it is
shipped without the source code. During application development it is
important for the developer to understand what the LoginModule does to
identify problems and their cause.
For the embedded OC4J container, edit the j2e-logging.xml file located
in the JDEVELOPER_HOME\jdev\system\oracle.j2ee.10.1.3.40.66\embedded-oc4j\config directory and add
<logger name="oracle.j2ee.security" level="FINE"
useParentHandlers="false">
<handler name="oc4j-handler"/>
<handler name="console-handler"/>
</logger>
![]() |
| Figure 6: Log messages printed to the JDeveloper console |
Logging for the standalone instance of OC4J is configured in the j2ee-logging.xml file located in the OC4J_HOME\j2ee\home\config directory. The log entries
are written in an XML format to the log.xml file located in the OC4J_HOME\j2ee\home\log\oc4j directory.
<logger name="oracle.j2ee.security.oc4j" level="FINE"
useParentHandlers="false">
<handler name="oc4j-handler"/>
<handler name="console-handler"/>
</logger>
Please refer to the OC4J security documentation for more information about configuring logging in OC4J.
To ensure the J2EE datasource used by the DBTableOraDataSourceLoginModule to connect to the database work, open a SQL*Plus session, connect as System
and type:
select username from v$session;
The resulting username list should contain an instance of the database
schema the DBTableOraDataSourceLoginModule connects to.
Too many failed attempts to connect to the database schema will eventually
lock the schema. You can verify this by opening SQL*Plus and connecting to
the schema. If the account is locked, connect as System and type:
alter user authschema account unlock;
This unlocks the account for further testing.
Database authentication and authorization in container-managed J2EE security
is possible through the OC4J DBTableOraDataSourceLoginModule or a custom
database JAAS LoginModule. Developers that extend existing non-J2EE applications
to Java usually have the requirement to leverage an existing database infrastructure
for authentication and authorization. The Oracle Application Server security
infrastructure in OC4J allows developers to configure standard JAAS LoginModules
to be used at design time and runtime instead of the default XML file-based or LDAP directory-based
provider. DBTableOraDataSourceLoginModule is a LoginModule provided by Oracle
that authenticates and authorizes users against a defined database schema in a secure
way by connecting to the database with J2EE data sources that protect the database
password through password indirection.
[1] Java™
Authentication and Authorization Service (JAAS) - Login Module Developer's
Guide
[2] Declarative
J2EE authentication and authorization with JAAS
[3] Free
JAAS book in PDF
[4] DBTableOraDataSourceLoginModule
[5] Custom
JAAS LoginModule packaging and deployment in OC4J
[6] otn.oracle.com/products/jdev
[7] OC4J logger documentation
Note: A bug in DBTableOraDataSourceLoginModule of OC4J 10.1.3.2 demands that the username column names are identical in the two tables. When this bug is fixed, the username column in the user table can be a different name than the name used for the same information in the roles table.