Hitachi

uCosminexus Application Server Security Management Guide


6.3.2 Security implementation in EJB client applications

EJB client applications can authenticate users by using their user names and passwords as defined in the J2EE server. After a user authenticated by the EJB client application logs in, he or she can call the Enterprise Bean method for which his or her security role is configured.

Organization of this subsection

(1) Implementation procedure

Cosminexus offers APIs to implement security in EJB client applications. The prerequisites and procedure for implementing this security are shown below. For details about the functionality and syntax of the APIs, see 4. APIs Available for EJB Client Applications, in the uCosminexus Application Server API Reference Guide.

Before implementing security, make sure that the following prerequisites are satisfied:

To implement security in an EJB client application, follow these steps:

  1. Import a security API package.

    To use the security APIs, import the package shown below.

    import com.hitachi.software.ejb.security.base.authentication.*
  2. Obtain the LoginInfoManager object.

    Use a program that calls Enterprise Bean methods to obtain the LoginInfoManager object. To obtain the object, use the getLoginInfoManager method, which is a static method for the LogInfoManager object.

    LoginInfoManager  lm = LoginInfoManager.getLoginInfoManager();
  3. Log in with the user name and password.

    After obtaining the LoginInfoManager object, call the login method.

    lm.login(username, password);
  4. Call the Enterprise Bean method.

    After the login method succeeds, call the Enterprise Bean method.

  5. Log out.

    After calling the Enterprise Bean method, log out from the J2EE server by using the logout method.

    lm.logout();
Important note

To implement security in an EJB client application, you need to add HiEJBClientStatic.jar to the class path and compile the file.

(2) Sample program

Below is a sample program for calling the getAccountID method, where the Enterprise Bean is named account.

import com.hitachi.software.ejb.security.base.authentication.*;
    :
  try {
    LoginInfoManager lm = LoginInfoManager.getLoginInfoManager();
    String userName = System.getProperty("username");
    String password = System.getProperty("password");
    if(lm.login(userName , password)) {
      try {
        System.out.println("user:" + userName + "login success");
        Context ctx = new InitialContext();
        java.lang.Object obj = ctx.lookup(appUnitPath + "Account");
        AccountHome aHome =
           (AccountHome)PortableRemoteObject.narrow(obj,AccountHome.class);
        Account account  = aHome.create();
        account.getAccountID();
      } finally {
        lm.logout();
      }
    }
  } catch(NotFoundServerException e) {
    System.out.println("not found server");
  } catch(InvalidUserNameException e) {
    System.out.println("invalid user name");
  } catch(InvalidPasswordException e) {
    System.out.println("invalid password");
  } catch(Exception e) {
    e.printStackTrace();
  }