Skip to main content

(Security Multi-user Client Problem) Bad remote CallerPrincipal when connecting 2 users from my client.

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
No replies
sam.ad
Offline
Joined: 2013-03-23
Points: 0

Hi,
I have a very annoying issue with my secured Glassfish 3.1.2.
When I connect 2 (or more) users from my EJB client, I am having always the last connected user when I do this in my EJB: sessionContext.getCallerPrincipal().getName();

Could you please give me a help on this issue?

Here is my client:

public static void main(String[] args) {
new Thread(new Runnable() {
  @Override
  public void run() {
     connectUser("userA", "pwdB");
  }
}).start();

new Thread(new Runnable() {
           @Override
   public void run() {
        connectUser("userB", "pwdB");
   }
}).start();
}

public static void connectUser(String userName, String password) {
try {
     ProgrammaticLogin login = new ProgrammaticLogin();
     login.login(userName, password.toCharArray());
     Context context = new InitialContext();
     MyServiceRemote myService = (MyServiceRemote) context.lookup("MyService");
     String remoteCaller = myService.getUserName();
     System.out.println("Local is (" + userName + "), Remote is (" + remoteCaller + ")");
  } catch (Exception e) {
     e.printStackTrace();
  }
}

Here is my EJB:

@Stateless(mappedName = "MyService", name="MyServiceBean")
public class MyServiceBean implements MyServiceRemote{
@Resource
private SessionContext sessionContext;
public MyServiceBean () {
super();
}
public String getUserName() {
return sessionContext.getCallerPrincipal().getName();
}
}