Listing 1

final static InitialContext   iniCtx = new InitialContext();
// Will prompt the user for a name and credentials
// using a GUI dialog box.
CallbackHandler   handler = new WSGUICallbackHandlerImpl();
LoginContext      logCtx = new LoginContext("WSLogin", handler);

// Single signon allows to access the container without
// needing to reauthenticate.
logCtx.login();
Subject   subject = logCtx.getSubject();

PrivilegedAction   bean1Action = new PrivilegedAction() {
public Object run() {
    try {
        Object   homeProxy = iniCtx.lookup("ejb/bean1");
        Bean1Home   bean1Home = (Bean1Home)
            PortableRemoteObject.narrow(homeProxy, Bean1Home.class);
        Bean1Remote   bean1 = helloHome.create();
        bean1.businessMethod1(...);
        ...
    }
    catch (CreateException ce)  { ... }
    catch (RemoteException re)  { ... }
}
};
PrivilegedAction   bean2Action = new PrivilegedAction() { ... }

// Access components in the application server on behalf of 'subject'
com.ibm.websphere.security.auth.WSSubject.doAs(subject, bean1Action);
com.ibm.websphere.security.auth.WSSubject.doAs(subject, bean2Action);

// End the logon session.
logCtx.logout();


Listing 2

InitialContext   iniCtx = new InitialContext();
// No use logging in, authentication will occur in the transport
// anyway and no effort to propagate credentials established in
// the lines commented out below will be made!
//
// UsernamePasswordHandler   handler =
//         new UsernamePasswordHandler(username, password);
// LoginContext   logCtx =
//         new LoginContext("client-login-module-name", handler);
// logCtx.login();

Service   srv = (javax.xml.rpc.Service)
    iniCtx.lookup("java:comp/env/service/Bean1Service");
Bean1ServiceEndpoint   bean1Stub = (Bean1ServiceEndpoint)
    srv.getPort(Bean1ServiceEndpoint.class);

// Can use either 1) Java EE standard stub properties to specify
// authentication data for HTTP Basic Authentication, or
// 2) Vendor specific extensions to specify a cerificate
// for HTTPS Mutual Authentication

Stub   stub = (Stub) port;

// Portable code for HTTP Basic Authentication.
stub._setProperty("javax.xml.rpc.security.auth.username", "Name");
stub._setProperty("javax.xml.rpc.security.auth.password", "Password");

// JBoss specific code for HTTPS Mutual Authentication.
// stub._setProperty("org.jboss.webservice.keyStore", keyStore);
// stub._setProperty("org.jboss.webservice.keyStorePassword",
//                   "keyStorePassword");
// stub._setProperty("org.jboss.webservice.keyStoreType", "JKS");
// stub._setProperty("org.jboss.webservice.trustStore", trustStore);
// stub._setProperty("org.jboss.webservice.trustStorePassword",
//                   "trustStorePassword");
// stub._setProperty("org.jboss.webservice.trustStoreType", "JKS");

bean1Stub.businessMethod1(...);
...
// End the logon session.
// logCtx.logout();