Skip to main content

Using Database login Module in JBoss

This post is a detailed description on how to use the database login module in JBoss with a J2EE Application.

Scenario


There is a EJB and we want to restrict the access to this ejb's method to an authenticated user having a particular role. The EJB is accessed from a standalone Java Client using Remote Lookup.


Implementation


The EJB is HelloSSB and the roles allowed are admin and user.
First step is to write the EJB
The source code is given below. This is a stateless session bean.


package com.prem.ejb;

import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import javax.annotation.security.RolesAllowed;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.jboss.annotation.security.SecurityDomain;

@Stateless
@SecurityDomain ("helloworld")
public class HelloSSB implements HellioIntf{
@PersistenceContext(unitName = "EntApp")
EntityManager em;
@Resource SessionContext ctx;
@RolesAllowed ({"admin","user"} )
public void hello() {
System.out.println(ctx.getCallerPrincipal().getName());
if(ctx.isCallerInRole("admin")) {
System.out.println("hello admin");
Book book=new Book();
//book.setId("1");
book.setName("J2EE blue Prints");
em.persist(book);
}else if(ctx.isCallerInRole("user")) {
System.out.println("hello User");
}else {
System.out.println("UNAUTHORISED ");
}
}
@PreDestroy public void close() {
System.out.println(" in predestroy ");
}
}



Here the annotation @SecurityDomain ("helloworld") indicates that this EJB is secured by the helloworld security domain.@RolesAllowed ({"admin","user"} ) indicates that the the method hello is accessible only to admin and user roles.
With this the EJB is ready. You can ignore the entity bean used in the EJB.
Edit the jboss.xml and provide a JNDI name for the EJB.

Example


<?xml version="1.0" encoding="UTF-8"?>
<jboss>
<enterprise-beans>
<session>
<ejb-name>HelloSSB</ejb-name>
<jndi-name>com.prem.Hello</jndi-name>
</session>
</enterprise-beans>
</jboss>



Now we need to configure the security domain in JBoss.
Edit the login-config.xml in the conf directory and add an entry like this.


<application-policy name="helloworld" >

<authentication>
<login-module flag="required" code="org.jboss.security.auth.spi.DatabaseServerLoginModule">
<module-option name="dsJndiName">java:/mysql</MODULE-OPTION>
<module-option name="principalsQuery">select passwd from users where userid=?</MODULE-OPTION>
<module-option name="rolesQuery">select role,'Roles' from user_roles where userid=?</MODULE-OPTION>
</LOGIN-MODULE>
</authentication>
</APPLICATION-POLICY>




This sets up a security domain in JBoss which will use the DataBaseServerLoginModule to authenticate and authorize any requests to the resource protected by this domain.
The above steps completes the server side setup.
Now we have to write the client and we want to propagate the client side login credentials to the server.For this jboss provides a ClientLoginModule. This login module does not do any authentication (client side ), it just passes the login information to the server.
Code for the client is given below

package com.prem.client;

import java.util.Iterator;
import java.util.Properties;
import javax.naming.*;
import org.jboss.security.auth.callback.SecurityAssociationHandler;
import javax.security.auth.callback.*;
import javax.security.auth.login.*;
import java.security.Principal;
import com.prem.ejb.HellioIntf;

public class EJBClient {
static String user="prem";
static String password="prem";
public static void main(String[] args) throws Exception {

SecurityAssociationHandler handler = new SecurityAssociationHandler();
Principal userPrincipal = new Principal() {
public String getName() {
return user;
}
};
handler.setSecurityInfo( userPrincipal, password);
LoginContext loginContext = new LoginContext( "hello", ( CallbackHandler ) handler );
loginContext.login();
String jndiName="com.prem.Hello";
final Properties p = new Properties();
Context ic = new InitialContext();
System.out.println("about to look up jndi name " + jndiName);
Object obj = ic.lookup(jndiName);
System.out.println("lookup returned " + obj.getClass());
HellioIntf foo = (HellioIntf) obj;
foo.hello();
}
}
For this client to work it has to use the ClientLoginModule and its configured through the JAAS config file
Content of the JAAS config file is given below.

hello {org.jboss.security.ClientLoginModule required;};



Now add all jars in jboss client folder to the classpath.Also add jbosssx jar to the classpath.
This completes the entire process.
Do create the tables mentioned in the login-config.xml mentioned above before testing the program



Comments

Anonymous said…
This comment has been removed by a blog administrator.
Anonymous said…
This comment has been removed by a blog administrator.
Anonymous said…
This comment has been removed by a blog administrator.
Anonymous said…
This comment has been removed by a blog administrator.
Unknown said…
This comment has been removed by a blog administrator.
Anonymous said…
This comment has been removed by a blog administrator.
Anonymous said…
This comment has been removed by a blog administrator.
Anonymous said…
This comment has been removed by a blog administrator.
Anonymous said…
This comment has been removed by a blog administrator.
Anonymous said…
This comment has been removed by a blog administrator.
Anonymous said…
This comment has been removed by a blog administrator.
Anonymous said…
This comment has been removed by a blog administrator.
Anonymous said…
This comment has been removed by a blog administrator.
Anonymous said…
This comment has been removed by a blog administrator.
Anonymous said…
This comment has been removed by a blog administrator.
Anonymous said…
This comment has been removed by a blog administrator.
Anonymous said…
This comment has been removed by a blog administrator.
Anonymous said…
This comment has been removed by a blog administrator.
Anonymous said…
This comment has been removed by a blog administrator.
Anonymous said…
This comment has been removed by a blog administrator.
Anonymous said…
This comment has been removed by a blog administrator.
Anonymous said…
This comment has been removed by a blog administrator.
Anonymous said…
This comment has been removed by a blog administrator.
Anonymous said…
Hi - I am certainly delighted to find this. great job!
Anonymous said…
Hi - I am definitely glad to find this. great job!
Anonymous said…
Hello. Facebook takes a [url=http://casino2013.webs.com/]online blackjack[/url] take a chance on 888 casino apportion: Facebook is expanding its efforts to institute real-money gaming to millions of British users after announcing a deal with the online gambling proprietorship 888 Holdings.And Bye.

Popular posts from this blog

Design Patterns

A note on Design patterns Many times we can see ourselves looking for design patterns to fit the design of a requirement. Of course if the requirement is simple enough to apply any existing design pattern we should do so. This will save us valuable time. In my opinion it is better not to worry about which pattern to apply.But concentrate on the three basic Object oriented principles. 1) Encapsulate what varies 2) Prefer composition over inheritance 3) Program to an interface not to an implementation. If we approach the design with these principles in mind , we don't have to search for patterns. Pattern will emerge itself. Once we apply these principles, we can definitely look whether any of the existing patterns can solve the issue better than the way we thought of. This way our thinking process will improve tremendously and at times we may end up creating a solution which may be better than any available design patterns.

Load Balancing

Why do you need to load balance ? Here i will put forward the various options available for web load balancing. Before we go into that , let us first examine why do we need load balancing after all.Consider you develop a website and is available to the public.If the application is served from a single webserver then there is a high chance that the machine will be overwhelmed if the usage of the application increases. This may result in users experiancing unusually high response times or in worse case the machine can come to a halt.So you need some mechanism to distribute the load across different machines,without the client having to swich address. There are many widely used approaches. The most common ones are DNS based load balancing Software load balancing Hardware load balancing DNS based Load Balancing Here the DNS server is configured in such a way that it returns different IP addresses for various requests to the DNS server for a particular domain name. The DNS server may contai...