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?

    IoT & Smart Cities Stories
    The challenges of aggregating data from consumer-oriented devices, such as wearable technologies and smart thermostats, are fairly well-understood. However, there are a new set of challenges for IoT devices that generate megabytes or gigabytes of data per second. Certainly, the infrastructure will have to change, as those volumes of data will likely overwhelm the available bandwidth for aggregating the data into a central repository. Ochandarena discusses a whole new way to think about your next...
    CloudEXPO | DevOpsSUMMIT | DXWorldEXPO are the world's most influential, independent events where Cloud Computing was coined and where technology buyers and vendors meet to experience and discuss the big picture of Digital Transformation and all of the strategies, tactics, and tools they need to realize their goals. Sponsors of DXWorldEXPO | CloudEXPO benefit from unmatched branding, profile building and lead generation opportunities.
    All in Mobile is a place where we continually maximize their impact by fostering understanding, empathy, insights, creativity and joy. They believe that a truly useful and desirable mobile app doesn't need the brightest idea or the most advanced technology. A great product begins with understanding people. It's easy to think that customers will love your app, but can you justify it? They make sure your final app is something that users truly want and need. The only way to do this is by ...
    Digital Transformation and Disruption, Amazon Style - What You Can Learn. Chris Kocher is a co-founder of Grey Heron, a management and strategic marketing consulting firm. He has 25+ years in both strategic and hands-on operating experience helping executives and investors build revenues and shareholder value. He has consulted with over 130 companies on innovating with new business models, product strategies and monetization. Chris has held management positions at HP and Symantec in addition to ...
    Dynatrace is an application performance management software company with products for the information technology departments and digital business owners of medium and large businesses. Building the Future of Monitoring with Artificial Intelligence. Today we can collect lots and lots of performance data. We build beautiful dashboards and even have fancy query languages to access and transform the data. Still performance data is a secret language only a couple of people understand. The more busine...
    DXWorldEXPO LLC announced today that Big Data Federation to Exhibit at the 22nd International CloudEXPO, colocated with DevOpsSUMMIT and DXWorldEXPO, November 12-13, 2018 in New York City. Big Data Federation, Inc. develops and applies artificial intelligence to predict financial and economic events that matter. The company uncovers patterns and precise drivers of performance and outcomes with the aid of machine-learning algorithms, big data, and fundamental analysis. Their products are deployed...
    Cell networks have the advantage of long-range communications, reaching an estimated 90% of the world. But cell networks such as 2G, 3G and LTE consume lots of power and were designed for connecting people. They are not optimized for low- or battery-powered devices or for IoT applications with infrequently transmitted data. Cell IoT modules that support narrow-band IoT and 4G cell networks will enable cell connectivity, device management, and app enablement for low-power wide-area network IoT. B...
    The hierarchical architecture that distributes "compute" within the network specially at the edge can enable new services by harnessing emerging technologies. But Edge-Compute comes at increased cost that needs to be managed and potentially augmented by creative architecture solutions as there will always a catching-up with the capacity demands. Processing power in smartphones has enhanced YoY and there is increasingly spare compute capacity that can be potentially pooled. Uber has successfully ...
    SYS-CON Events announced today that CrowdReviews.com has been named “Media Sponsor” of SYS-CON's 22nd International Cloud Expo, which will take place on June 5–7, 2018, at the Javits Center in New York City, NY. CrowdReviews.com is a transparent online platform for determining which products and services are the best based on the opinion of the crowd. The crowd consists of Internet users that have experienced products and services first-hand and have an interest in letting other potential buye...
    When talking IoT we often focus on the devices, the sensors, the hardware itself. The new smart appliances, the new smart or self-driving cars (which are amalgamations of many ‘things'). When we are looking at the world of IoT, we should take a step back, look at the big picture. What value are these devices providing. IoT is not about the devices, its about the data consumed and generated. The devices are tools, mechanisms, conduits. This paper discusses the considerations when dealing with the...