Welcome!

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 =
MessageDigest.getInstance("MD5");
md.update(originalPwd.getBytes());
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:

CREATE TABLE Principals
(principalId varchar(50), password var char(25));
CREATE TABLE Roles
(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.

Conclusion
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.

Resources

  • 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

    Thanks

    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
    Recently, REAN Cloud built a digital concierge for a North Carolina hospital that had observed that most patient call button questions were repetitive. In addition, the paper-based process used to measure patient health metrics was laborious, not in real-time and sometimes error-prone. In their session at 21st Cloud Expo, Sean Finnerty, Executive Director, Practice Lead, Health Care & Life Science at REAN Cloud, and Dr. S.P.T. Krishnan, Principal Architect at REAN Cloud, discussed how they built...
    In his Opening Keynote at 21st Cloud Expo, John Considine, General Manager of IBM Cloud Infrastructure, led attendees through the exciting evolution of the cloud. He looked at this major disruption from the perspective of technology, business models, and what this means for enterprises of all sizes. John Considine is General Manager of Cloud Infrastructure Services at IBM. In that role he is responsible for leading IBM’s public cloud infrastructure including strategy, development, and offering m...
    In his session at 21st Cloud Expo, Raju Shreewastava, founder of Big Data Trunk, provided a fun and simple way to introduce Machine Leaning to anyone and everyone. He solved a machine learning problem and demonstrated an easy way to be able to do machine learning without even coding. Raju Shreewastava is the founder of Big Data Trunk (www.BigDataTrunk.com), a Big Data Training and consulting firm with offices in the United States. He previously led the data warehouse/business intelligence and B...
    With tough new regulations coming to Europe on data privacy in May 2018, Calligo will explain why in reality the effect is global and transforms how you consider critical data. EU GDPR fundamentally rewrites the rules for cloud, Big Data and IoT. In his session at 21st Cloud Expo, Adam Ryan, Vice President and General Manager EMEA at Calligo, examined the regulations and provided insight on how it affects technology, challenges the established rules and will usher in new levels of diligence arou...
    Nordstrom is transforming the way that they do business and the cloud is the key to enabling speed and hyper personalized customer experiences. In his session at 21st Cloud Expo, Ken Schow, VP of Engineering at Nordstrom, discussed some of the key learnings and common pitfalls of large enterprises moving to the cloud. This includes strategies around choosing a cloud provider(s), architecture, and lessons learned. In addition, he covered some of the best practices for structured team migration an...
    The 22nd International Cloud Expo | 1st DXWorld Expo has announced that its Call for Papers is open. Cloud Expo | DXWorld Expo, to be held June 5-7, 2018, at the Javits Center in New York, NY, brings together Cloud Computing, Digital Transformation, Big Data, Internet of Things, DevOps, Machine Learning and WebRTC to one location. With cloud computing driving a higher percentage of enterprise IT budgets every year, it becomes increasingly important to plant your flag in this fast-expanding busin...
    No hype cycles or predictions of a gazillion things here. IoT is here. You get it. You know your business and have great ideas for a business transformation strategy. What comes next? Time to make it happen. In his session at @ThingsExpo, Jay Mason, an Associate Partner of Analytics, IoT & Cybersecurity at M&S Consulting, presented a step-by-step plan to develop your technology implementation strategy. He also discussed the evaluation of communication standards and IoT messaging protocols, data...
    Smart cities have the potential to change our lives at so many levels for citizens: less pollution, reduced parking obstacles, better health, education and more energy savings. Real-time data streaming and the Internet of Things (IoT) possess the power to turn this vision into a reality. However, most organizations today are building their data infrastructure to focus solely on addressing immediate business needs vs. a platform capable of quickly adapting emerging technologies to address future ...
    22nd International Cloud Expo, taking place June 5-7, 2018, at the Javits Center in New York City, NY, and co-located with the 1st DXWorld Expo will feature technical sessions from a rock star conference faculty and the leading industry players in the world. Cloud computing is now being embraced by a majority of enterprises of all sizes. Yesterday's debate about public vs. private has transformed into the reality of hybrid cloud: a recent survey shows that 74% of enterprises have a hybrid cloud ...
    22nd International Cloud Expo, taking place June 5-7, 2018, at the Javits Center in New York City, NY, and co-located with the 1st DXWorld Expo will feature technical sessions from a rock star conference faculty and the leading industry players in the world. Cloud computing is now being embraced by a majority of enterprises of all sizes. Yesterday's debate about public vs. private has transformed into the reality of hybrid cloud: a recent survey shows that 74% of enterprises have a hybrid cloud ...
    DevOps at Cloud Expo – being held June 5-7, 2018, at the Javits Center in New York, NY – announces that its Call for Papers is open. Born out of proven success in agile development, cloud computing, and process automation, DevOps is a macro trend you cannot afford to miss. From showcase success stories from early adopters and web-scale businesses, DevOps is expanding to organizations of all sizes, including the world's largest enterprises – and delivering real results. Among the proven benefits,...
    @DevOpsSummit at Cloud Expo, taking place June 5-7, 2018, at the Javits Center in New York City, NY, is co-located with 22nd Cloud Expo | 1st DXWorld Expo and will feature technical sessions from a rock star conference faculty and the leading industry players in the world. The widespread success of cloud computing is driving the DevOps revolution in enterprise IT. Now as never before, development teams must communicate and collaborate in a dynamic, 24/7/365 environment. There is no time to wait...
    Cloud Expo | DXWorld Expo have announced the conference tracks for Cloud Expo 2018. Cloud Expo will be held June 5-7, 2018, at the Javits Center in New York City, and November 6-8, 2018, at the Santa Clara Convention Center, Santa Clara, CA. Digital Transformation (DX) is a major focus with the introduction of DX Expo within the program. Successful transformation requires a laser focus on being data-driven and on using all the tools available that enable transformation if they plan to survive ov...
    SYS-CON Events announced today that T-Mobile exhibited at SYS-CON's 20th International Cloud Expo®, which will take place on June 6-8, 2017, at the Javits Center in New York City, NY. As America's Un-carrier, T-Mobile US, Inc., is redefining the way consumers and businesses buy wireless services through leading product and service innovation. The Company's advanced nationwide 4G LTE network delivers outstanding wireless experiences to 67.4 million customers who are unwilling to compromise on qua...
    SYS-CON Events announced today that Cedexis will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 - Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Cedexis is the leader in data-driven enterprise global traffic management. Whether optimizing traffic through datacenters, clouds, CDNs, or any combination, Cedexis solutions drive quality and cost-effectiveness. For more information, please visit https://www.cedexis.com.
    SYS-CON Events announced today that Google Cloud has been named “Keynote Sponsor” of SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Companies come to Google Cloud to transform their businesses. Google Cloud’s comprehensive portfolio – from infrastructure to apps to devices – helps enterprises innovate faster, scale smarter, stay secure, and do more with data than ever before.
    SYS-CON Events announced today that Vivint to exhibit at SYS-CON's 21st Cloud Expo, which will take place on October 31 through November 2nd 2017 at the Santa Clara Convention Center in Santa Clara, California. As a leading smart home technology provider, Vivint offers home security, energy management, home automation, local cloud storage, and high-speed Internet solutions to more than one million customers throughout the United States and Canada. The end result is a smart home solution that sav...
    SYS-CON Events announced today that Opsani will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Opsani is the leading provider of deployment automation systems for running and scaling traditional enterprise applications on container infrastructure.
    SYS-CON Events announced today that Nirmata will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Nirmata provides a comprehensive platform, for deploying, operating, and optimizing containerized applications across clouds, powered by Kubernetes. Nirmata empowers enterprise DevOps teams by fully automating the complex operations and management of application containers and its underlying ...
    SYS-CON Events announced today that Opsani to exhibit at SYS-CON's 21st Cloud Expo, which will take place on October 31 through November 2nd 2017 at the Santa Clara Convention Center in Santa Clara, California. Opsani is creating the next generation of automated continuous deployment tools designed specifically for containers. How is continuous deployment different from continuous integration and continuous delivery? CI/CD tools provide build and test. Continuous Deployment is the means by which...