1 Reply Latest reply on Oct 31, 2007 2:00 AM by alllle

    How to setup context when accessing SLSB from MDB?

    alllle

      I am consistently getting this exception with my deployed application.

      I have a MDB that accesses other local EJB. I coded the MDB to lookup the other EJB using the InitialContext with its own username and password. The code looks like this:

      Hashtable<String, Object> env = new Hashtable<String, Object> ();
      env.put(Context.SECURITY_PRINCIPAL, username);
      env.put(Context.SECURITY_CREDENTIALS, password);
      env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.security.jndi.JndiLoginInitialContextFactory");
      
      InitialContext context = new InitialContext(env);
      


      However, when the LoginModule.login() method is invoked by the JBoss AS on behalf of the MDB code during the process of invoking the EJB methods, the LoginModule does not see the username and password specified in the InitialContext. It gets null values and hence failed to proceed with the EJB invocation.

      With the similar code, I can invoke the local EJB from Servlet code and I don't know what is the problem when used inside the MDB. Please help if you have any idea!

      Thanks,



        • 1. Re: How to setup context when accessing SLSB from MDB?
          alllle

          Problem solved after trace into JBoss code.

          The reason is that I created the InitialContext at the time when the MDB is created and try to reuse it when the onMessage() is invoked. What happens behind the scene is that JBoss saves the username / password into a ThreadLocal variable in server environment when the InitialContext is created. This ThreadLocal variable is not there anymore when the onMessage() is invoked because it is a different thread. Hence, the security information is lost and causes the authentication exception.

          I now create a new InitialContext object when processing the onMessage() method. Seems working fine.