Weblogic Authors: Yeshim Deniz, Elizabeth White, Michael Meiner, Michael Bushong, Avi Rosenthal

Related Topics: Weblogic

Weblogic: Article

Writing a Custom JAAS LoginModule to Support Secure DataBase Authentication

Writing a Custom JAAS LoginModule to Support Secure DataBase Authentication

With the arrival of BEA WebLogic Server 7.0, a new security architecture with improved and expanded security features was introduced. The primary goal of that new architecture is to let you plug in security providers from different security vendors and to let them cooperate seamlessly with your J2EE applications.

A security provider is a module that handles some specific security tasks, like authentication and authorization. However, instead of buying a security provider you can develop your own. When there are no third-party providers that meet your security needs, this can be your only effective solution. I ran into this situation when I needed to authenticate users based on information in a relational database. Since there was no standard WebLogic provider that did the job, and no suitable third-party alternative, I decided to develop my own database authentication-provider. In fact, it wasn't that difficult, as I will show you in this article.

Authentication in WebLogic
Authentication is probably the most important part of security. It is the process that proves the identity of a user based on some credentials, in most cases a password. The default WebLogic 7 authentication provider uses an embedded LDAP server to store users and groups as a replacement of the well-known file realm in WebLogic 6. In addition, WebLogic 7 provides a set of LDAP authentication providers for accessing external LDAP stores like OpenLDAP or iPlanet. And what happened with the RDBMS security realm that was present in WebLogic 6? Well, it is still possible to use the RDBMS security realm, but you will have to boot WebLogic Server in so called "compatibility mode," meaning that the server provides backward compatibility with the WebLogic 6.x security realms by using a special kind of authentication provider: a realm adapter authentication provider. However, the use of these realm adapter providers is deprecated; hence, to make your projects future-proof it's better to use an authentication provider that fits into WebLogic's new security architecture.

Secure Passwords
Prior to guiding you through the different steps in developing your own authentication provider, it's important to discuss how you can use a relational database as an authentication information store with the help of encryption. After all, the most important part is the storage of passwords. Storing these passwords in plain text in the database isn't the right way to go. It's better to obscure the passwords with a messagedigest algorithm. A messagedigest is a secure, one-way hash function: a function that calculates a fixed-length hash value of an arbitrary-sized original value and can be computed only one way. Once the algorithm has been applied to the password, it's impossible to go backwards and find the original password - viewing the messagedigest alone will not reveal the contents of the original message and changing even a single bit on the message will result in a totally different output value. The only way is to pass in random input until you get the original password - a brute-force attack. Your authentication provider doesn't need to know the original passwords at all - it only needs to apply the hash function on any password given by a user and compare it with the stored hashed password of that user. If both are the same, the user supplied the correct password and can be authenticated.

Putting the Messagedigest to Work
In Java there's an easy way to calculate messagedigests by using the java.security.MessageDigest class. The following code snippet shows you how to apply an MD5 messagedigest algorithm (a 128-bit digest) on a password string:

MessageDigest md =
byte[] digestedPwdBytes = md.digest();

Messagedigests are also used to create a checksum, a unique ID for a piece of text (also called a digital fingerprint). This is what happens if you sign a jar file: a checksum is calculated from the contents of the JAR file, encrypted, and stored in the manifest.mf file in base64 encryted format. Base64 is a way to encode arbitrary binary data so that the result contains only printable characters (note that base64-encoded data takes one-third more space than the data before conversion). Since the messagedigest algorithm outputs its response as a byte array, we can use base64 encoding to transform the hash bytes into a string so that we can store it in a varchar field in the database. There are many base64 encoders available, but the easiest way is to use the one that's included in the weblogic.jar library: weblogic.apache.xerces.utils.Base64. The use of this class is trivial, as shown in the next code sample:

String digestedPwdString =
new String(Base64.encode(digestedPwdBytes));

Security Provider Architecture
Now let's go back to the Authentication provider itself. As I mentioned before, an Authentication provider is one kind of security provider in WebLogic 7.0. To understand the structure of an Authentication provider, it's important to discuss the general architecture of all security providers. Each provider is a module that can be plugged into a security realm. It is developed by implementing the appropriate security service provider interfaces (SSPIs) from the weblogic.security.spi package and by creating an MDF file to generate an MBean type used to configure and manage the provider (see Figure 1).

MBeans, or Managed Beans, are a construct of JMX, the Java Management eXtensions package. JMX provides support for application and network management in Java. An MBean is a specific type of JavaBean that can be managed in a JMX-compliant application. In WebLogic, MBean types are created by the WebLogic MBeanMaker utility on the basis of an MBean Definition File (MDF), an XML file that describes the MBean type. This XML file consists of properties used to configure the security provider, e.g., name, description, and implementation classes. These properties can be exposed in the server's administration console, where you can view and edit them.

JAAS for Authentication
In developing my own authentication provider, the sample security providers on BEA's dev2dev site turned out to be very useful. I used them as a starting point to work out my database authentication provider and I recommend that you take a close look at them if you are planning to do some security provider development yourself. If you look at the sample authentication provider, you will see the use of the Java Authentication and Authorization Service (JAAS) in the provider's implementation classes. WebLogic uses JAAS internally for authentication as well as for remote fat-client authentication. The authentication provider authenticates users in a JAAS loginmodule (javax.security.auth.spi.LoginModule). Each authentication provider needs to have exactly one LoginModule, but you can configure multiple Authentication providers in one security realm. This way you have multiple LoginModules that can perform different kinds of authentication for a user. The authentication mechanism itself is based on users and groups: a user represents a person, a group represents a category of persons. After authentication, WebLogic assigns a principal to both users and groups. The principals are stored in a JAAS subject during the commit() method of the loginmodule. Any principal that is going to represent a WebLogic Server user or group needs to implement the WLSUser or WLSGroup interface. The sample Authentication provider on dev2dev uses the weblogic.security.principal.WLSUserImpl to represent the user into a subject, and the weblogic.security.principal.WLSGroupImpl to represent the groups that contain the user. That's sufficient for the database provider too, so you can reuse the same mechanism.

Closer Look at the LoginModule
The only things you need to change in the sample loginmodule are the details in checking the correctness of username and password in the login() method. Obviously the security framework calls this method when a user has to be authenticated. Authentication has to be proven here by checking the user's password. The sample Authentication provider delegates to the SampleAuthenticatorDatabase class to retrieve user and group information from a persistent store - in this case, a plain text file. You have to do two things. The first one is changing the password check in the login() method. Remember that you should store a hash value of the password, so instead of checking if a given password matches the password in the database, you should apply the messagedigest algorithm on the given password first, and then compare it with the stored hash value. Second, you have to adapt the SampleAuthenticatorDatabase (or better, use your own class like RDBMSAuthenticatorDatabase) to retrieve user and group info from a relational database instead of a text file. If you don't need hierarchical roles, it's sufficient to create two simple tables in the database:

(principalId varchar(50), password var char(25));
(principalId varchar(50), role varchar(50));

Next you need to add some simple database access code in the authenticator's database class. However, there's a snake in the grass here: since WebLogic uses the Authentication provider to also authenticate the user that boots the server, you can't use a javax.sql.DataSource as the factory for database connection objects because the naming service isn't started yet. You have to use a DriverManager to obtain a connection to the database. Once the server is started, it's no problem to use a DataSource, and you should probably do it at that time to benefit from connection pooling.

Implement the SSPI
The last Java class you have to write is the implementation of the weblogic.security.spi.AuthenticationProvider SSPI. This class represents the runtime implementation of the authentication provider. Here you need to specify the authenticator's database and LoginModule implementation classes. For the SampleAuthenticationProviderImpl, these are SampleLoginModule and SampleAuthenticatorDatabase. Obviously you need to change these into your own classnames. After successful authentication, a principal validation provider is needed to sign the principals and ensure their authenticity between programmatic server invocations. The SampleAuthenticationProviderImpl reuses the standard WebLogic principal validator for this purpose, and that's sufficient for the database authentication provider too.

Configuration and Deployment
That's all for the Java classes. The last thing you need is the MDF file. Again, you can easily adapt the SampleAuthenticator.xml file and supply some additional properties as <MBeanAttribute> tags, e.g., database URL, drivername, username, password, query to retrieve principal info, query to retrieve role info. The MBean that is generated from this MDF has to be supplied as an argument in the constructor of the authenticator's database class, so you can use the properties of the MBean to configure the class. The next step is to generate the MBean using WebLogic's MBeanMaker. The sample security provider's archive contains example ANT scripts showing you how to do this with the weblogic.management.commo.WebLogicMBeanMaker class. The same class is used to build the authentication provider's MJF file (MBean Jar File). Finally, you need to copy that Java archive into the /lib/mbeantypes directory relative to the home dir of your WebLogic Server installation. After a reboot of the server, you can install the authentication provider by navigating in the admin console to Security - Realms - <yourRealm> - Providers - Authentication Providers. The name of the newly created provider should be visible now, e.g. "configure new RDBMS Authenticator" (see Figure 2). Set the right properties and reboot the server. This will activate the authentication provider. Remember that WebLogic uses it immediately upon booting up the server, so make sure there's a user in the Administrator's role present in your database or else you won't be able to start the server.

Although primarily intended as a framework to plug in third-party security providers, WebLogic's new security architecture can be used to develop your own custom security provider too. It's not that hard to create, deploy, and configure your own authentication provider, as I've demonstrated in this article. Basic knowledge of JAAS and JMX, some good samples, and a bit of encryption magic is all you need. The last thing is perhaps the most important, because even the best authentication provider turns out to be worthless when combined with an insecure authentication store.


  • BEA WebLogic Server 7.0 Security Web site: http://edocs.bea.com/wls/docs70/security.html
  • BEA dev2dev code direct: WebLogic 7.0.1 Sample Security Providers download: http://dev2dev.bea.com/code/codedirect.jsp
  • Java Management Extensions home page: http://java.sun.com/products/JavaManagement
  • Java Authentication and Authorization Service home page: http://java.sun.com/products/jaas
  • More Stories By Tim Pijpops

    I am presently working as a Software Architect for Egova, a Belgian company specialized in e-government projects. My duties include architecting and designing J2EE solutions. I am particularly interested in J2EE, Web Services, Security and Business Intelligence. I own a Master's degree in Informatics.

    Comments (3) View Comments

    Share your thoughts on this story.

    Add your comment
    You must be signed in to add a comment. Sign-in | Register

    In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.

    Most Recent Comments
    Gail 03/28/03 12:54:00 PM EST

    Cesar & Ricardo,

    We don't have these answers. You can, however, post your questions to [email protected] or one of the product management newsgroups, where they may be better able to answer you.

    Ricardo trovato 03/28/03 12:37:00 PM EST

    Hi dears,

    I would like to know,when the weblogic server will be able to integration with LDAP v3


    Ricardo Trovato

    Cesar Alpendre 03/28/03 12:31:00 PM EST

    Do you know when the BEA's WebLogic Server authentication provider will be able to LDAP Realm version 3?

    @ThingsExpo Stories
    Cloud-enabled transformation has evolved from cost saving measure to business innovation strategy -- one that combines the cloud with cognitive capabilities to drive market disruption. Learn how you can achieve the insight and agility you need to gain a competitive advantage. Industry-acclaimed CTO and cloud expert, Shankar Kalyana presents. Only the most exceptional IBMers are appointed with the rare distinction of IBM Fellow, the highest technical honor in the company. Shankar has also receive...
    Business professionals no longer wonder if they'll migrate to the cloud; it's now a matter of when. The cloud environment has proved to be a major force in transitioning to an agile business model that enables quick decisions and fast implementation that solidify customer relationships. And when the cloud is combined with the power of cognitive computing, it drives innovation and transformation that achieves astounding competitive advantage.
    Personalization has long been the holy grail of marketing. Simply stated, communicate the most relevant offer to the right person and you will increase sales. To achieve this, you must understand the individual. Consequently, digital marketers developed many ways to gather and leverage customer information to deliver targeted experiences. In his session at @ThingsExpo, Lou Casal, Founder and Principal Consultant at Practicala, discussed how the Internet of Things (IoT) has accelerated our abilit...
    In his session at Cloud Expo, Alan Winters, U.S. Head of Business Development at MobiDev, presented a success story of an entrepreneur who has both suffered through and benefited from offshore development across multiple businesses: The smart choice, or how to select the right offshore development partner Warning signs, or how to minimize chances of making the wrong choice Collaboration, or how to establish the most effective work processes Budget control, or how to maximize project result...
    To get the most out of their data, successful companies are not focusing on queries and data lakes, they are actively integrating analytics into their operations with a data-first application development approach. Real-time adjustments to improve revenues, reduce costs, or mitigate risk rely on applications that minimize latency on a variety of data sources. In his session at @BigDataExpo, Jack Norris, Senior Vice President, Data and Applications at MapR Technologies, reviewed best practices to ...
    Data is the fuel that drives the machine learning algorithmic engines and ultimately provides the business value. In his session at Cloud Expo, Ed Featherston, a director and senior enterprise architect at Collaborative Consulting, discussed the key considerations around quality, volume, timeliness, and pedigree that must be dealt with in order to properly fuel that engine.
    In his keynote at 19th Cloud Expo, Sheng Liang, co-founder and CEO of Rancher Labs, discussed the technological advances and new business opportunities created by the rapid adoption of containers. With the success of Amazon Web Services (AWS) and various open source technologies used to build private clouds, cloud computing has become an essential component of IT strategy. However, users continue to face challenges in implementing clouds, as older technologies evolve and newer ones like Docker c...
    "Akvelon is a software development company and we also provide consultancy services to folks who are looking to scale or accelerate their engineering roadmaps," explained Jeremiah Mothersell, Marketing Manager at Akvelon, in this SYS-CON.tv interview at 21st Cloud Expo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA.
    Two weeks ago (November 3-5), I attended the Cloud Expo Silicon Valley as a speaker, where I presented on the security and privacy due diligence requirements for cloud solutions. Cloud security is a topical issue for every CIO, CISO, and technology buyer. Decision-makers are always looking for insights on how to mitigate the security risks of implementing and using cloud solutions. Based on the presentation topics covered at the conference, as well as the general discussions heard between sessio...
    To Really Work for Enterprises, MultiCloud Adoption Requires Far Better and Inclusive Cloud Monitoring and Cost Management … But How? Overwhelmingly, even as enterprises have adopted cloud computing and are expanding to multi-cloud computing, IT leaders remain concerned about how to monitor, manage and control costs across hybrid and multi-cloud deployments. It’s clear that traditional IT monitoring and management approaches, designed after all for on-premises data centers, are falling short in ...
    Organizations planning enterprise data center consolidation and modernization projects are faced with a challenging, costly reality. Requirements to deploy modern, cloud-native applications simultaneously with traditional client/server applications are almost impossible to achieve with hardware-centric enterprise infrastructure. Compute and network infrastructure are fast moving down a software-defined path, but storage has been a laggard. Until now.
    DXWorldEXPO LLC announced today that the upcoming DXWorldEXPO | CloudEXPO New York event will feature 10 companies from Poland to participate at the "Poland Digital Transformation Pavilion" on November 12-13, 2018.
    Digital Transformation is much more than a buzzword. The radical shift to digital mechanisms for almost every process is evident across all industries and verticals. This is often especially true in financial services, where the legacy environment is many times unable to keep up with the rapidly shifting demands of the consumer. The constant pressure to provide complete, omnichannel delivery of customer-facing solutions to meet both regulatory and customer demands is putting enormous pressure on...
    The best way to leverage your CloudEXPO | DXWorldEXPO presence as a sponsor and exhibitor is to plan your news announcements around our events. The press covering CloudEXPO | DXWorldEXPO will have access to these releases and will amplify your news announcements. More than two dozen Cloud companies either set deals at our shows or have announced their mergers and acquisitions at CloudEXPO. Product announcements during our show provide your company with the most reach through our targeted audienc...
    In an era of historic innovation fueled by unprecedented access to data and technology, the low cost and risk of entering new markets has leveled the playing field for business. Today, any ambitious innovator can easily introduce a new application or product that can reinvent business models and transform the client experience. In their Day 2 Keynote at 19th Cloud Expo, Mercer Rowe, IBM Vice President of Strategic Alliances, and Raejeanne Skillern, Intel Vice President of Data Center Group and ...
    More and more brands have jumped on the IoT bandwagon. We have an excess of wearables – activity trackers, smartwatches, smart glasses and sneakers, and more that track seemingly endless datapoints. However, most consumers have no idea what “IoT” means. Creating more wearables that track data shouldn't be the aim of brands; delivering meaningful, tangible relevance to their users should be. We're in a period in which the IoT pendulum is still swinging. Initially, it swung toward "smart for smart...
    @DevOpsSummit at Cloud Expo, taking place November 12-13 in New York City, NY, is co-located with 22nd international CloudEXPO | first international DXWorldEXPO and will feature technical sessions from a rock star conference faculty and the leading industry players in the world.
    DXWorldEXPO LLC announced today that All in Mobile, a mobile app development company from Poland, will exhibit at the 22nd International CloudEXPO | DXWorldEXPO. All In Mobile is a mobile app development company from Poland. Since 2014, they maintain passion for developing mobile applications for enterprises and startups worldwide.
    In his keynote at 19th Cloud Expo, Sheng Liang, co-founder and CEO of Rancher Labs, discussed the technological advances and new business opportunities created by the rapid adoption of containers. With the success of Amazon Web Services (AWS) and various open source technologies used to build private clouds, cloud computing has become an essential component of IT strategy. However, users continue to face challenges in implementing clouds, as older technologies evolve and newer ones like Docker c...
    DXWorldEXPO LLC announced today that ICC-USA, a computer systems integrator and server manufacturing company focused on developing products and product appliances, will exhibit at the 22nd International CloudEXPO | DXWorldEXPO. DXWordEXPO New York 2018, colocated with CloudEXPO New York 2018 will be held November 11-13, 2018, in New York City. ICC is a computer systems integrator and server manufacturing company focused on developing products and product appliances to meet a wide range of ...