Securing EJB Applications with Custom JBoss Login Modules

Source: Internet
Author: User
Tags jboss jboss server

JBoss comes out-of-the-box with a handful of great login modules, but lets face it, you need a custom one to integrate with your existing security infrastructure. this overview shows you how to secure your EJB application with a custom Server login module, and also to use a custom client login module to authenticate your EJB client applications.

JBoss makes use of the Java authentication and authorization Service API to authenticate and authorize users in your EJB applications. if you are not already familiar with creating JAAS login modules, I wocould recommend that you read my entry on creating JAAS login modules to get an understanding of how they work.

This overview will show you how to secure your EJB Applications in JBoss using JAAS, and how to authenticate your client applications to the JBoss server. I will assume that since you are at the level of creating custom login modules, that you already know how to set up your XML Descriptor file for role based authorization, and have already done so. if you haven't, you shoshould... otherwise, it makes It difficult to test your login module! We won't go over the steps of creating an EJB, but for this example we'll assume that we have a stateless session bean called HelloBean. the HelloBean's remote interface defines one method, hello (), that returns the String "Hello" to the client. helloBean's XML Descriptor is set up such that the caller must be in the role "ExampleRole ".

So here's the scenario, you have an existing security infrastructure that provides username and password based authentication. you want to perform client side authentication, and have the server lookup what roles the user is in from some database or other part of the existing security infrastructure. my entry on Creating JAAS Login Modules will show you how to set up the client side login module that performs client side authentication. everything in that entry holds true for this tutorial, with only a couple additional steps needed for JBoss. the first thing that is required is that in addition to your own custom login module, you also use JBoss's ClientLoginModule. to do this, your security domain configuration file shocould look like this:

exampleDomain {     net.eskeeter.jaasexample.CustomLoginModule required;     org.jboss.security.ClientLoginModule required;};

JBoss's ClientLoginModule will get the username and password from the callback handler you initialized the login context. if you look carefully at the implementation of the CustomCallbackHandler (in the jaas login module entry), you'll see that it only interacts with the user the first time it is called. since each login module in the context shares the same instance of the callback handler, the user is not prompted for their credentials twice. there are other (and probably "better") ways to accomplish this using the shared state map, but this way is usually tive as well.

So now that your client is authenticated, you need to get the principal and credentials to the server. note that if you do not need to perform further password authentication on the server side, there is no reason to pass the actual password to the server, although for JBoss, you do need to pass something to the server. in the implementation I wrote for jetia, I pass the hard-coded string "idontnee Dthepassword "to the server as the credentials. So how do you get this information to the server? It's easy. JBoss's clientloginmodule takes the username and password and puts them in the org. JBoss. security. securityassociation class. you get the information to the server by initializing your JNDI naming context with them as follows:

HashTable props = new HashTable();props.put( Context.SECURITY_PRINCIPAL,            SecurityAssociation.getPrincipal() );props.put( Context.SECURITY_CREDENTIALS,            SecurityAssociation.getCredential() ); InitialContext initialContext = new InitialContext( props );

From then on, when you use that naming context to lookup EJB Home and Remote references, that information can be obtained by the server in the caller's context. using a Service Locator pattern (Core J2EE Patterns, Prentice Hall, Alur, Crupi and Malks) simplifies this, since you only have one initial context in the whole application.

So that's all there is to the client side additions. by requiring JBoss's ClientLoginModule and initializing the JNDI naming context with the principal and credentials, you have fulfilled all your client side obligations for using custom JBoss login modules. implementing the server side login module is no more difficult than it was to do the client side.

To write the server side login module, all you need to do is implement the JAAS module, set up your EJB Application in a security domain, and tell JBoss to use your module for that domain. recall that our client's CustomLoginModule subclasses the JAAS javax. security. auth. login. loginModule class. the server side login module shocould subclass org. jboss. security. auth. spi. abstractLoginModule, which is a subclass of javax. security. auth. login. loginModule. the reason we subclass JBoss's class instead of directly extending the JAAS class is because JBoss expects to find the user and role principals arranged in a specific manner. using the AbstractLoginModule ensures that these are set up properly for JBoss. our CustomServerLoginModule looks like this:

package net.eskeeter.jbossjaasexample;import java.security.acl.Group;import javax.security.auth.login.LoginExceptionimport org.jboss.security.auth.api.AbstractLoginModule;public class CustomServerLoginModule   extends AbstractServerLoginModule{    private Principal identity;    public Principal getIdentity() {        return identity;    }    public Group[] getRoleSets() {        ...    }    public boolean login() throws LoginException {        ...    }} 

The getIdentity () and getRoleSets () methods are abstract in the AbstractServerLoginModule, and must be implemented by our server login module. the login () method has a default implementation, but you'll typically want to override this do anything you need to do on the server side. this login method is the very same as the one in the JAAS login module, and thus follows the same rules: return true if successful, false if this module shoshould be ignored, or throw a LoginException if an error occurs.

public boolean login() throws LoginException{    identity =       org.jboss.security.SecurityAssociation.getPrincipal();    if ( identity == null )    {         throw new LoginException(             "The principal was not found in " +            "the SecurityAssociation." );    }    loginOk = true;    return true;}

Our simple sets the identity principal, and returns true. the boolean loginOk is a protected member of the AbstractServerLoginModule class, and is checked by the commit () method to determine if the login () method succeeded. the getIdentity () method simply returns the principal that shocould be used as the identity of the subject on the server. the getRoleSets method returns an array of Groups of Principals, our sample implementation is as follows:

public Group[] getRoleSets() {    // Your implementation should lookup what roles    // the user, identified by the identity principal, is in     // from a database, or some group management     // system you have in place.    Group rolesGroup = new SimpleGroup( "Roles" );    rolesGroup.addMember(         new SimplePrincipal( "ExampleRole" ) );    return new Group[]{ rolesGroup };}

JBoss expects to find the user's roles in a group called "Roles ". if you have a need for them, you can add other groups as well. our simple implementation is not very dynamic, it always returns the Roles group with a single role principal, "ExampleRole ".

That's all there is to writing the custom login module for the server. all that's left to do now is tell the JBoss what security domain your EJB Application shoshould run in, and to use your server login module for that domain. setting your EJB Application's security domain is as easy as adding a tag to your jboss. xml descriptor file:

<jboss><security-domain>ExampleDomain</security-domain></jboss>

Note that this security domain does not have anything to do with the security domain you set up on the client. when you build your ejb jar file, put the JBoss. XML file along side your ejb-jar.xml file in the jar files META-INF directory. JBoss will process this file with the ejb-jar.xml descriptor file, appending any information found in it to the information found in the ejb-jar.xml file. to tell JBoss to use your login module, You need to edit the/Server/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.