我的安全Glassfish 3.1.2有一个非常烦人的问题。 当我从我的独立客户端连接2个(或更多)用户时,当我尝试从EJB获取调用者主体时,我始终是最后一个连接用户:
sessionContext.getCallerPrincipal().getName();
你能否就这个问题给我一个帮助?
这是我的客户:
<pre>
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();
}
}
</pre>
这是我的EJB:
<pre>
@Stateless(mappedName = "MyService", name="MyServiceBean")
public class MyServiceBean implements MyServiceRemote{
@Resource
private SessionContext sessionContext;
public MyServiceBean () {
super();
}
public String getUserName() {
return sessionContext.getCallerPrincipal().getName();
}
}
</pre>